Answer toward "Snap object to (0,0,0)"
You can use both operator or built-in method:
Operator
bpy.ops.object.location_clear(clear_delta=False)
This is equivalent to the button in Object → Clear → Clear Location (Alt + G)
And you can set clear_delta
parameter if you want to clear delta-transform as well.
built-in method:
obj.location = Vector((0,0,0))
(where obj is Blender object)
obj.delta_location = Vector((0,0,0))
(Clear delta location)
This give you direct(kind of) assess to location value, which should be saved as a Blender's Vector
Operator will somehow rely on context
when you call it, so the scene, selected objects, other possible associate content should be set properly. And I personally don't like to use operator in script because they are wrap function set that should be used in User Interface, making it hard to guess what will the operator do when you call it.
Original Answer toward "Set object origin to (0,0,0)"
It is not as easy as you think in this task due to the lack of the function implement.
The object origin is actually a descriptor for inner data. You can change the descriptor directly. But data didn't change inside so the position you see will be different, that's not we want.
Approach
- Use Blender bpy operator to do your task. The 3D cursor and set origin operator. (easy)
import bpy
from mathutils import Vector, Matrix
def bpy_set_origin(location=Vector((0.0, 0.0, 0.0))):
# store the location of current 3d cursor
# must copy the location since that's a reference.
saved_cursor_location = bpy.context.scene.cursor.location.copy()
# set 3d cursor to desired location
if isinstance(location,Vector):
bpy.context.scene.cursor.location = location
else:
print(f"Invalid location given:{location}")
return
# set object to new location
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
# restore former location
bpy.context.scene.cursor.location = saved_cursor_location
if __name__ == "__main__":
bpy_set_origin((0, 0, 0))
- Modify origin directly and make data consistent with the change
import bpy
from mathutils import Vector, Matrix
def directly_change_origin(
obj,
location = Vector((0.0, 0.0, 0.0))
):
# Get the original translation
translation_vector = obj.matrix_world.translation @ obj.matrix_world
# Make a tm for transform
transform_matrix = Matrix.Translation(translation_vector)
# Make object
obj.data.transform(transform_matrix)
obj.matrix_world.translation = location
if __name__ == "__main__":
if bpy.context.active_object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
selected_list = bpy.context.selected_objects
for obj in selected_list:
directly_change_origin(obj, (0, 0, 0))