How to get the Vertex Global coordinate

A mesh Object has been translated/rotated/scaled.

We can check the vertex cordinate in 3dview-n pan.

But. When I try to calculate the Global coordinate form local coordinate myself. I have a wrong result.

Excuse.


import bpy


def main():
    print('

')
    C = bpy.context
    aObj = C.active_object
    if aObj == None:
        print('No Mesh seleted')
        return
    vts = aObj.data.vertices
    x = [i.index for i in vts if i.select==True][0]
    if x == None:
        print('1 Vertex selected at least')
        return
    wm = aObj.matrix_world.copy()
    aP = vts[x].co * wm
    print(' pointID: %s
 local: %s
 global: %s
 maxtrix: %s' % (x, vts[x].co, aP, wm))

    C.scene.cursor_location = aP     #check position~


main()

Thanks for your help~

Hi nirenyang.

For recent versions of blender you have your matrix vector multiplication back to front


 aP = wm * vts[x].co

also you wont see the cursor jumping from vert to vert with the cursor_location (check position) bit. It will be at the position of the last vert when the script finishes. If you want to animate that effect look into modal timer operators.

PS.

As a tip I suggest, even for simple test code, getting into the practice of passing context to your function… eg


def main(context):
    blah blah

main(bpy.context)

If you start using operators / panels most methods are defined with context as an argument.


class myoperator(bpy.types.Operator):
    #execute method of the op
    def execute(self, context):
        main(context)

another tip is to add your method to the drivers namespace


bpy.app.driver_namespace["global_verts"] = main

Then you can call it from the console


>>> gv = bpy.app.driver_namespace['global_verts']
>>> gv(C)






 pointID: 0
 local: <Vector (1.0000, 1.0000, -1.0000)>
 global: <Vector (2.4585, 1.5676, -4.1081)>
 maxtrix: <Matrix 4x4 ( 2.8794, -0.0112, 0.4097, 0.0000)
            (-0.1344,  2.7208, 1.0189, 0.0000)
            (-0.3872, -1.0277, 2.6932, 0.0000)
            ( 0.0000,  0.0000, 0.0000, 1.0000)>


>>> 

I think it should be

aP = wm * vts[x].co

since matrix multiplication is not commutative.

Dear batFINGER and Afeitadora! Thanks your Greatest Help ~~Happy Blender~

coord_global = Object.matrix_world * Object.data.vertices[#].co

but you should use Object.data.transform(matrix) on meshes to turn them completely into global space (best with to_mesh(), so a copy with optionally modifiers applied)