5
$\begingroup$

I am trying to get global coordinates of a point for an addon I'm creating.

If an object has no transformations, the coordinates I'm grabbing work, and also if there is either a scale or a translation. Yet, any rotation transformation produces incorrect results:

  1. Open Blender, File > Load factory settings
  2. Hit Numpad 1 to go to front ortho view
  3. Hit R, Z, 45 to rotate the default cube 45 degrees around z axis
  4. Hit Tab to toggle edit mode and select the upper leftmost vert in this view
  5. Hit Tab, Tab to toggle edit mode off and on again
  6. Select scripting layout
  7. Run this code in Python Console:

    ob = bpy.context.object
    
    for point in ob.data.vertices:
        if point.select:
            print(point.co * ob.matrix_world)
    
  8. Press N in 3D View panel to bring up point coords, switch to Global and compare coords from python console, these are different:

    • From python console: Vector (0.0000, 1.4142, 1.0000)
    • From N panel: X: -1.41421, Y: -0.0000004, Z: 1.00000

So, what am I doing wrong? Here's my code:

class GrabPointFromActive( bpy.types.Operator ):
    bl_idname = "mct.grabpointfromactive" # Check the blender precedent here for naming
    bl_label = "Grab From Active Point"
    bl_description = "Grabs coordinates from selected vertex in edit mode"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        addonData = bpy.context.scene.mct_data
        prims = bpy.context.scene.mct_data.primList
        ob = bpy.context.object

        # fix later, only allow execution when a single vert is selected, otherwise warn
        # fix later, check if this is a mesh first before toggling, we need this so most recent active selection is used
        bpy.ops.object.editmode_toggle()
        bpy.ops.object.editmode_toggle()
        for vert in ob.data.vertices:
            if vert.select:
                prims[addonData.activeListItemIndex].point = vert.co * ob.matrix_world

        return {'FINISHED'}
$\endgroup$

1 Answer 1

7
$\begingroup$

When applying an objects transformation matrix to a vertex, you need to multiply:

Matrix * Vector

However your example does:

Vector * Matrix

This behavior matches numpy and other math libraries.

$\endgroup$
0

You must log in to answer this question.

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