I have an object located in the origin (0,0,0). I wonder how I can translate it along the z-axis where the lowest -z value of the object will be translated to z value 0 as shown below:
-
$\begingroup$ Loop over the vertices, find the one with the lowest z value, move the object up -whatever_the_previous_got_you :-) I'll post an answer in a few... $\endgroup$– JakeDFeb 15, 2017 at 12:42
-
$\begingroup$ related blender.stackexchange.com/a/42110/15543 in that it moves the origin to bottom. $\endgroup$– batFINGERFeb 15, 2017 at 14:21
1 Answer
Here is a simple script that accomplishes this...let me know if you have any questions.
import bpy
# get a reference to the active object
obj = bpy.context.object
# get the minimum z-value of all vertices after converting to global transform
lowest_pt = min([(obj.matrix_world * v.co).z for v in obj.data.vertices])
# transform the object
obj.location.z -= lowest_pt
Note
For 2.8 replace *
with @
for matrix multiplication.
lowest_pt = min([(obj.matrix_world @ v.co).z for v in obj.data.vertices])
-
2$\begingroup$ Can also find the lowestZ in one line instead of a loop thus:
lowestZ = min( [ ( obj.matrix_world * v.co ).z for v in obj.data.vertices ] )
$\endgroup$– TLouskyFeb 15, 2017 at 13:04 -
2$\begingroup$ Adding object location to local vert coordinate is not converting to global location. See comment above. $\endgroup$ Feb 15, 2017 at 14:25
-
1$\begingroup$ @TLousky Thanks! Your method also works correctly with rotation and scale that I wasn't thinking about when I posted originally. I have edited my answer to include this technique... $\endgroup$– JakeDFeb 15, 2017 at 14:50
-
1$\begingroup$ @batFINGER I was fooled into thinking this b/c I was not using a rotated or scaled object. $\endgroup$– JakeDFeb 15, 2017 at 14:50
-
$\begingroup$ @pycoder what if the object was an armature? It doesn't work in that case as this solution only works for mesh as it uses the mesh vertices $\endgroup$– TakMar 5, 2017 at 2:49