So im getting started in blender python, so i wrote this quick code to Move an objects Base to the ground level and change its pivot to the Cursor location
So my problem is that, if the cursor is somewhere other than the world center, then i need to Center the 3D cursor to the World center and then Move the object pivot to Cursor.
Im really having issues coding that one line of code in between to change 3D cursor to world Center.
Tried all codes and snippets from previous forums but nothings quite working or understandable.
One thing I noticed, your code only seems to work if object is centered of world and if your object is like 2 x 2 x 2. I put this together and I think it does what you want ,but dimensions or location doesn’t matter. You can comment each line out to see what it’s doing.
import bpy
def CenterOrigin():
#Get active object
act_obj = bpy.context.active_object
#Get cursor
cursor = bpy.context.scene.cursor
#Get original cursor location
original_cursor_location = (cursor.location[0], cursor.location[1], cursor.location[2])
#Make sure origin is set to geometry for cursor z move
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
#Set cursor location to object location
cursor.location = act_obj.location
#Get cursor z move
half_act_obj_z_dim = act_obj.dimensions[2] / 2
cursor_z_move = cursor.location[2] - half_act_obj_z_dim
#Move cursor to bottom of object
cursor.location[2] = cursor_z_move
#Set origin to cursor
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
#Reset cursor back to original location
cursor.location = original_cursor_location
#Assuming you're wanting object center to grid
bpy.ops.object.location_clear(clear_delta=False)
CenterOrigin()