Setup
I have an object Plane with 6 vertices. To each vertex I assign another object as child.
Then I apply two keyframes on location to object Plane:
- In frame 0 it goes to Z = 5
- In frame 81 it goes to Z = 0
I want to know correct global x,y,z coordinates of each Sphere during frames 0 to 81.
Problem
I do not understand which matrix or matrix combination to use to achieve coordinates in this case.
For example for frame 81 I know that Z = 0 for Plane. It is also Z = 0 for all spheres.
I would like to know object Sphere global coordinates. If I code in Blender console:
>>> sp0.matrix_world.to_translation()
Vector((-1.0, -1.0, 0.0))
>>> sp0.location
Vector((-1.0, -1.0, 5.0))
If I write python script and run it I have:
obj = bpy.data.objects['Sphere']
x,y,z = obj.matrix_world.to_translation()
print("x,y,z ", [x,y,z])
print("setpoint_location ", obj.location)
>>>x,y,z [-1.0, -1.0, 5.0]
>>>setpoint_location <Vector (-1.0000, -1.0000, 5.0000)>
Additional info
Code I use to create Spheres and attach it to Plane as child objects on each vertex.
import bpy
def create_objects():
for i in range(6):
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.25, enter_editmode=False,location=(i*2., 0, 0))
def allocate_objs(plane, objects):
bpy.ops.object.select_all(action='DESELECT')
for i, vert in enumerate(plane.data.vertices):
world_matrix = plane.matrix_world
vertice_coords = world_matrix @ vert.co
objects[i].location = vertice_coords
objects[i].select_set(True)
plane.select_set(True)
bpy.context.view_layer.objects.active = plane
bpy.ops.object.parent_set(type='VERTEX')
#create_objects()
objects = [obj for obj in bpy.data.objects if "Plane" not in obj.name]
plane = bpy.data.objects["Plane"]
#allocate_objs(plane, objects)
obj = bpy.data.objects['Sphere']
for frame in range(0,100):
bpy.context.scene.frame_current = frame
if frame in [0, 81]:
x,y,z = obj.matrix_world.to_translation()
print(bpy.context.scene.frame_current)
print("world matrix \n", obj.matrix_world)
print("matrix_basis \n", obj.matrix_basis)
print("matrix_local \n", obj.matrix_local)
print("matrix_parent_inverse \n", obj.matrix_parent_inverse)
print("frame ", frame)
print("x,y,z ", [x,y,z])
print("setpoint_location ", obj.location)
print("======================")