2
$\begingroup$

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.

bpy.ops.import_scene.fbx( filepath = "sample.fbx" )
myObj  = bpy.data.objects['testObj']
print (myObj.data.vertices[0].co)
myObj.location = mathutils.Vector((1,3,5))
print (myObj.data.vertices[0].co)

This prints the same values both times. How do I update it? I would appreciate an answer for Blender 2.8

$\endgroup$
3
  • 1
    $\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$
    – batFINGER
    Commented Jan 17, 2020 at 5:01
  • 1
    $\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$
    – batFINGER
    Commented Jan 17, 2020 at 5:03
  • $\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$
    – kauDaOtha
    Commented Jan 17, 2020 at 18:49

2 Answers 2

3
$\begingroup$

For the scene global coordinate system, multiply by the object's world matrix:

myObj  = bpy.data.objects['testObj']
print (myObj.data.vertices[0].co)
myObj.location = mathutils.Vector((1,3,5))
print (myObj.matrix_world @ myObj.data.vertices[0].co) # <---- 
$\endgroup$
0
$\begingroup$

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)

$\endgroup$
1
  • $\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$ Commented Nov 4, 2020 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .