Setting Origin to World Centre using Blender Python

Hey Guys,

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

import bpy

def CenterOrigin():
    bpy.ops.transform.translate(value=(-0, 0, 1), orient_type='GLOBAL')
    bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
    
CenterOrigin()

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.

So any tips or suggestions would be Helpful :slight_smile:

Hey try this out, it worked for me in Blender 2.8:

import bpy
import mathutils 
from mathutils import Vector 

def CenterOrigin():
    bpy.ops.transform.translate(value=(0, 0, 1), orient_type='GLOBAL')
    
    #put cursor at origin 
    bpy.context.scene.cursor.location = Vector((0.0, 0.0, 0.0))
    bpy.context.scene.cursor.rotation_euler = Vector((0.0, 0.0, 0.0))
    
    bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
    
CenterOrigin()

You will have to import mathutils and the Vector as well. This works on the active (context) scene.

There is an ops command:

bpy.ops.view3d.snap_cursor_to_center()

But this will not work in text editor or in the console - this command can be used when you are building add-ons.

Hope that works for you!

Level

1 Like

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()
1 Like

Oh wow thank you so much :D… Will surely update my code accordingly :)…

Just a noob question… Why is there a Negative sign to the first zero in the first value ? Is it based on any actual purpose ?

That was my mistake, the - sign can be removed, is not needed and has no purpose. I’ve edited my original post.