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:
- Open Blender, File > Load factory settings
- Hit Numpad 1 to go to front ortho view
- Hit R, Z, 45 to rotate the default cube 45 degrees around z axis
- Hit Tab to toggle edit mode and select the upper leftmost vert in this view
- Hit Tab, Tab to toggle edit mode off and on again
- Select scripting layout
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)
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
- From python console:
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'}