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:
Asked
Active
Viewed 1,470 times
6
-
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... – JakeD Feb 15 '17 at 12:42
-
related http://blender.stackexchange.com/a/42110/15543 in that it moves the origin to bottom. – batFINGER Feb 15 '17 at 14:21
1 Answers
6
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])
-
2Can 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 ] )– TLousky Feb 15 '17 at 13:04 -
2Adding object location to local vert coordinate is not converting to global location. See comment above. – batFINGER Feb 15 '17 at 14:25
-
1@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... – JakeD Feb 15 '17 at 14:50
-
1@batFINGER I was fooled into thinking this b/c I was not using a rotated or scaled object. – JakeD Feb 15 '17 at 14:50
-
@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 – Tak Mar 05 '17 at 02:49
-
@Tak That's a completely different problem, and something that I can't really think of a good reason to need off the top of my head. Would you be calculating based on the center of bones? The heads and tails? I think this would be better suited for a different question that explains these things clearly and gives an example of when you may want to use it. – JakeD Mar 05 '17 at 03:18
-
@pycoder http://blender.stackexchange.com/questions/74936/move-object-without-changing-origin – Tak Mar 05 '17 at 03:20
-
The line 7 doesn't work in Blender 2.8: TypeError: Element-wise multiplication: not supported between 'Matrix' and 'Vector' types
What should we write instead? Thank you.
– Danyl Bekhoucha Jan 11 '19 at 18:51 -
@DanylBekhoucha You should ask that as a separate question if you need an answer. I've not looked into porting my add-ons to Blender 2.8 yet, so I don't know off the top of my head. – JakeD Jan 11 '19 at 22:27
