Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
I import a .fbx object and move the location. Then I try to print the local coordinates of the object. Regardless of where I move it, it prints the same local coordinates leading to me believe that the mesh vertices are not being updated. The snippet is attached below.
$\begingroup$For 2.7x and before, but related blender.stackexchange.com/questions/94034/… Vertex coordinates are in local space. Can move a mesh object anywhere in the scene by setting the objects transform (translation, rotation, scale) without altering the vertex coordinates. This is a fundamental of 3d maths eg one default cube has local coordinates existing of the 8 combinations of +/- 1 (-1, -1, -1) , (-1, -1, 1) ... (1, 1, 1) that remain unchanged no matter how it is transformed. As shown in the answer below object transform is used to$\endgroup$
$\begingroup$place local objects into our global space. Put another way object transforms don't change the local data. If you wish to transorm the data not the object use myObj.data.transform(Matrix.Translation((1, 3, 5))) to translate all local coordinates by (1, 3, 5)$\endgroup$
$\begingroup$I now understand local coordinates. So if want to change my location of the object and get the global coordinates of the vertices, I should use Matrix.Translation to move it and then pre-multiply with matrix_world ? I cannot get the global coordinates of the vertices by setting the myObj.location?$\endgroup$
This code is to be run in blender 2.9 It explain how to get or convert local coordinates of a vertex to global coordinates.
#############################################################
# imports :
import bpy
from mathutils import Vector
#############################################################
# Add a Cube and Get the initial local coordinates of vertex id 0,
# Get the initial Cube matrix_world,
# Calculate the initial vertex id 0 Global Coordinates :
bpy.ops.mesh.primitive_cube_add()
Cube = bpy.context.object
Mw = Cube.matrix_world
Vert0_Lco = Cube.data.vertices[0].co
Vert0_Gco = Mw @ Vert0_Lco
print (f"Vert 0 initial Local coordinates :\n{Vert0_Lco}") # should be (-1, -1, -1)
print (f"Cube initial Matrix world :\n{Mw}") # should be the identity Matrix
print (f"Vert 0 initial Global coordinates :\n{Vert0_Gco}") # should be (-1, -1, -1) also
#############################################################
# Make a translation vector :
# Translate the cube,
# Get the final Cube matrix_world,
# Calculate the final vertex id 0 Global Coordinates :
Trans_Vector = Vector((1,3,5))
'''Now If you want the Cube matrix_world to be updated after the translation transform dont use<<< Cube.location = Trans_Vector >>>, instead use :'''
Mw.translation += Trans_Vector
print (f"Vert 0 final Local coordinates :\n{Vert0_Lco}")#Should be the same ( (-1, -1, -1) dont change after transform)
print (f"Cube final Matrix world :\n{Mw}") # should change to :
'''
Matrix(((1, 0, 0, 1 )
(0, 1, 0, 3 )
(0, 0, 1, 5 )
(0, 0, 0, 1 )))
'''
print (f"Vert 0 final Global coordinates :\n{Vert0_Gco}") # should change to (0, 2, 4)
$\begingroup$Hi thanks for the answer. Code only answers are frowned upon here. What does this code do, what is its purpose and how does it work? Could you add a short explanation of what you did here?$\endgroup$
myObj.data.transform(Matrix.Translation((1, 3, 5)))
to translate all local coordinates by (1, 3, 5) $\endgroup$