Run a script using msgbus
when new primitive added
Run the following script in the text editor.
Add any mesh primitive, the origin is adjusted to the bottom of its bounding box as long as the following conditions are met.
- Object mode.
- Object is a mesh.
- The active operator is a mesh primitive add.
Code put together from
Set origin to bottom center of multiple objects
Adjusted such that the object is not moved globally so the mesh origin is at scene cursor location, and is the centre of the bottom face of bounding box. (For Suzanne is outside the mesh) Alternatively running this one after adding objects does the trick, but not "automatically".
How to get an event when an object is selected?
Very lacking in docs re bpy.msgbus
, not sure for what version was introduced, possibly should assert(bpy.msgbus)
to throw an error if not available for your version.
import bpy
from mathutils import Vector, Matrix
handle = object()
# Triggers when an object is made active
subscribe_to = bpy.types.LayerObjects, "active" #
def origin_to_bottom(ob):
me = ob.data
local_verts = [Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
me.transform(Matrix.Translation(-o))
def notify_test(context):
if (context.object.type == 'MESH'
and getattr(context.active_operator, "bl_idname", "").startswith("MESH_OT_primitive_")
and context.mode == 'OBJECT'):
print("Setting origin to bottom")
origin_to_bottom(context.object)
bpy.msgbus.subscribe_rna(
key=subscribe_to,
owner=handle,
args=(bpy.context,),
notify=notify_test,
)
bpy.msgbus.publish_rna(key=subscribe_to)
Note: put this together as a proof of concept. Have not tested greatly.
Could modify to only work for Cube, Cylinder, Cone not Monkey Sphere.
Having the origin at centre of lowest face would be Ok for cube not UV cylinder, Cone with ngon fill, not fan etc.
Alternatively could rewrite, or override (ie register another with same name), any / all the add mesh operators in python.