0
$\begingroup$

I'd like to locate a object at (0,0,0) in the global coordinate system, but the object has its own coordinate system, such that bpy.context.object.location = (0,0,0) does not work:

Coordinate systems

How can I determine the offset to move the object to the global point of origin (0,0,0)?

$\endgroup$
  • 2
    $\begingroup$ related: blender.stackexchange.com/questions/24015/… $\endgroup$ – p2or Jan 18 '16 at 19:38
  • $\begingroup$ without origin_set(): blender.stackexchange.com/questions/35825/… $\endgroup$ – p2or Jan 18 '16 at 19:49
  • $\begingroup$ @poor Thanks, this goes definitive in the right direction, I still don't have a working code, but I will continue to figure it out ... $\endgroup$ – frthjf Jan 18 '16 at 20:22
  • $\begingroup$ Glad I could help. What exactly is not working? $\endgroup$ – p2or Jan 18 '16 at 20:23
  • $\begingroup$ I did not recognize that the object dimensions are local coordinates too ... $\endgroup$ – frthjf Jan 18 '16 at 22:00
1
$\begingroup$

You can also do this with three operators and no calculations (will certainly be faster than iterating over all vertices if you have a high-resolution mesh). That is if you don't mind that it will center your object's origin point.

import bpy
bpy.ops.object.mode_set( mode = 'OBJECT' ) # Make sure we're in object mode
bpy.ops.object.origin_set( type = 'ORIGIN_GEOMETRY' ) # Move object origin to center of geometry
bpy.ops.object.location_clear() # Clear location - set location to (0,0,0)
$\endgroup$
0
$\begingroup$

Using the ideas of the proposed solution, this snippet does the offset calculation:

bpy.context.scene.update()

# determine minima of the object
min_x = min_y = min_z = 999999.0
for vertex in self.blender_object.data.vertices:
        # object vertices are in object space, translate to world space
        v_world = self.blender_object.matrix_world * Vector((vertex.co[0], vertex.co[1], vertex.co[2]))

        if v_world[0] < min_x:
            min_x = v_world[0]

        if v_world[1] < min_y:
            min_y = v_world[1]

        if v_world[2] < min_z:
            min_z = v_world[2]

# convert to world coordinates again
size = self.blender_object.matrix_world * self.blender_object.dimensions

offset = Vector((min_x +  size.x/2, min_y - size.y/2, min_z))

self.blender_object.location = self.blender_object.location - offset
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.