Aligning the origin to the bottom for all selected objects using the Blender Python API

The geometry of objects in Blender is always built around the origin – a point that is taken as the starting point in the object’s local coordinate system. And if the origin does not lie in the plane of the object’s base, it can be difficult to place this object on the surface of another object. We can quickly set the origin to the bottom for all selected objects in the scene, writing a script using the Blender Python API.

To “drop” the origin to the bottom for each of the selected objects, we can move the object itself down by the height of its origin, and raise all its vertices up by the same height. This way, the real location of the object’s geometry will not change, and its origin will be shifted to its base.

Loop through all the selected objects:

First, let’s change the coordinates of all the vertices of the object, moving them up.

To understand how much exactly we need to shift the vertices, we need to find the lowest vertex, which lies at the bottom of the object, and subtract the coordinate of this vertex along the Z-axis from the current coordinate of the origin along the same axis.

Current coordinate of the origin along the Z axis:

Since transformations, for example – rotation, may not have been applied to the object yet, we need to get global coordinate values ​​for the vertices.

To do this, multiply the coordinate of the vertex by the world matrix of the object.

Now we can find the vertex with the smallest Z coordinate.

And get the vector for the vertex offset.

Since the object may not have transformations applied, we cannot simply move the vertices by the resulting vector. We need to take into account the influence of the transformation matrix.

So first, let’s form a matrix for moving the vertices from our vector.

Save a copy of the world transformation matrix of the object to exclude intermediate influence. And also its inverted version.

Now we can finally start moving vertices.

For each vertex of our object: first, we reset the influence of the transformation matrix, then apply our offset along the Z axis, and then return the influence of world transformations back.

Now we can see how all the vertices of the object have shifted up along the Z-axis so that they are at the level (or higher) of the origin.

All that remains is to shift the origin (the entire object) itself, down to return the geometry to its original place.

Now, all the origins for all the selected objects have shifted to the plane of their bases.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments