
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi
I have a question regarding obtainng the location of mouse click in world coordinates in Maya.
I am using Maya 2016 and Python API 2.0
So I know I can use the cmds.draggerContext() Python command to get the location of the mouse click in world space coordinates by setting space = "world" in cmds.draggerContext().
Here is how I am doing it:
import maya.cmds as cmds draggerContextName = "myDragger" def dragger_onPress(): pos = cmds.draggerContext(draggerContextName, query = True, anchorPoint = True) print("init:", pos[0], pos[1], pos[2]) def dragger_onDrag(): pos = cmds.draggerContext(draggerContextName, query = True, dragPoint = True) print("drag pos:", pos[0], pos[1], pos[2]) if (cmds.contextInfo(draggerContextName, exists = True)): cmds.deleteUI(draggerContextName, toolContext = True ) cmds.draggerContext(draggerContextName, pressCommand = dragger_onPress, dragCommand = dragger_onDrag, cursor = "crossHair", space="world") cmds.setToolTo(draggerContextName)
I am also aware that I can use the Maya Python 2.0 API to get the position of the mouse click in world space coordinates as follows:
import maya.cmds as cmds import maya.api.OpenMaya as om import maya.api.OpenMayaUI as omui2 draggerContextName = "myDragger" def viewToWorld(screenX, screenY): worldPos = om.MPoint() # out variable worldDir = om.MVector() # out variable activeView = omui2.M3dView().active3dView() activeView.viewToWorld(int(screenX), int(screenY), worldPos, worldDir) return worldPos, worldDir def dragger_onPress(): pos = cmds.draggerContext(draggerContextName, query = True, anchorPoint = True) pos = viewToWorld(pos[0], pos[1])[0] print("init:", pos[0], pos[1], pos[2]) def dragger_onDrag(): pos = cmds.draggerContext(draggerContextName, query = True, dragPoint = True) pos = viewToWorld(pos[0], pos[1])[0] # get the first value of the tuple print("drag pos:", pos[0], pos[1], pos[2]) if (cmds.contextInfo(draggerContextName, exists = True)): cmds.deleteUI(draggerContextName, toolContext = True ) cmds.draggerContext(draggerContextName, pressCommand = dragger_onPress, dragCommand = dragger_onDrag, cursor = "crossHair") cmds.setToolTo(draggerContextName)
Note that I have not set the space flag in cmds.draggerContext() command here so I am getting the mouse click coordinates in screen space which I am then converting to world space using the M3dView.active3dView.viewToWorld() function.
Though I would expect similar output on mouse click in both the approaches, the result however is not the same.
While I get consistent coordinates in the first approach, using the Maya API the coordinates differ when zooming or translating the viewport.
Even with same zoom and translation of the viewport, the ouput is different in both the scenarios.
Can somebody please tell me why is there such a difference between the two cases?
Using the first approach seems preferable but the Maya API also gives the wordDir MVector() which can then be used for hit testing with an object.
Solved! Go to Solution.