The position of the object is defined by it's origin point. If you import a model from another 3d package by using an exchange format like .obj
or .fbx
, the origin is typically at (0,0,0)
:
In order to align an object directly to the ground plane:
- Move the object to the origin (to make sure the object is perfectly centered)
- Iterate through the geometry to find the minimum z location value of all verticies
- Place the 3d Cursor to the minimum z location value
- Set the Origin to 3d Cursor
- Move the object to
(0,0,0)
Suzanne aligned to the center of the ground plane
import bpy
# get the current object
current_obj = bpy.context.active_object
# get the scene
scene = bpy.context.scene
# set geometry to origin
bpy.ops.object.origin_set(type="GEOMETRY_ORIGIN")
zverts = []
# get all z coordinates of the vertices
for face in current_obj.data.polygons:
verts_in_face = face.vertices[:]
for vert in verts_in_face:
local_point = current_obj.data.vertices[vert].co
world_point = current_obj.matrix_world * local_point
zverts.append(world_point[2])
# set the minimum z coordinate as z for cursor location
scene.cursor_location = (0, 0, min(zverts))
# set the origin to the cursor
bpy.ops.object.origin_set(type="ORIGIN_CURSOR")
# set the object to (0,0,0)
current_obj.location = (0,0,0)
# reset the cursor
scene.cursor_location = (0,0,0)
GEOMETRY_ORIGIN
doesn't put model at the center. $\endgroup$bpy.ops.object.origin_set(type="GEOMETRY_ORIGIN")
bpy.ops.object.origin_set(type="ORIGIN_CENTER_OF_MASS")
obj.location = (0, 0, 0)
make it better but there is a part of feet still under the ground. $\endgroup$