How would I save the Z height from one force touch and run a palletizing layer off that height?

I have a palletizing program that I am trying to optimize. It takes boxes does a Z touch for the force and then drops the boxes at the force. I would like to make it so that it can do a single Z height check on each layer and save that to place the rest of the boxes on that layer. I am fairly new to robotics and was just given this project so I appreciate any help.

You can use the script command get_actual_tcp_pose() when the robot is at the z height you want (after force touch). The third item in the pose is the z height.

position = get_actual_tcp_pose()
zheight = position[2}

newpose = p[x,y,zheight,rx,ry,rz]

Then use newpose in a move to a variable waypoint.

Thank you! I will try using that.