Is it possible to get the screen position(not the viewport position) of a vertex?

221 views
Skip to first unread message

yaoys

unread,
Jun 13, 2018, 10:45:13 PM6/13/18
to Python Programming for Autodesk Maya
Hi, all

Motivation:
I'm trying to do the automation test for my maya plugin by manipulating mouse to select a vertex.

System information:
Maya2016

Methods which failed:
1. 
import maya.api.OpenMaya as om2
import maya.api.OpenMayaUI as omui2
point2 = cmds.pointPosition('pPlane1.vtx[0]', world=True)
v2 = omui2.M3dView()
worldPt2 = om2.Mpoint(point2[0], point2[1], point2[2])
#print worldPt2
print v2.worldToView(worldPt2)  # It complains an error here: Error: RuntimeError: file <maya console> line 7: (kFailure): Object does not exist #

2. 
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
point2 = cmds.pointPosition('pPlane1.vtx[0]', world=True)
#print point
v= omui.M3dView()
worldPt = om.Mpoint(point[0], point[1], point[2])
#print worldPt

xutil = om.MScriptUtil()
yutil = om.MScriptUtil()
xutil.createFromInt(0)
yutil.createFromInt(0)
xptr = xutil.asShortPtr()
yptr = yutil.asShortPtr()
print v.worldToView(worldPt, xptr, yptr)   # It complains an error here:  Error: RuntimeError: file S:\Maya_2016_DI\build\Release\runTime\Python\Lib\site-packages\maya\OpenMayaUI.py line 244:: (kFailure): Object does not exist #
x = xutil.getShort(xptr)
y = yutil.getShort(yptr)

BTW, S:\Maya_2016_DI\build\Release\runTime\Python\Lib\site-packages\maya\OpenMayaUI.py does not exist on my machine.


3.
I tried to compute viewport position manually by using this code: https://forum.highend3d.com/t/3d-point-to-2d-screen-position/48599/2
import maya.OpenMaya as om   def pointWorldToCam(cameraName, point, res): sel = om.MSelectionList() dag = om.MDagPath()   sel.add(cameraName) sel.getDagPath(0,dag)   cam = om.MFnCamera(dag) floatMat = cam.projectionMatrix() projMat = om.MMatrix(floatMat.matrix) floatMat = cam.postProjectionMatrix() postProjMat = om.MMatrix(floatMat.matrix) transMat = dag.inclusiveMatrix()   #long form ensures compatibility with MPoint and any iterable point = om.MPoint(point[0],point[1],point[2])   fullMat = transMat.inverse() * projMat * postProjMat nuPoint = point * fullMat return [(nuPoint[0]/nuPoint[3]/2+0.5)*res[0], (1-(nuPoint[1]/nuPoint[3]/2+0.5))*res[1]]

#test
point2 = cmds.pointPosition('pPlane1.vtx[0]', world=True)
height = cmds.control('modePanel4', q=True, height=True)
width= cmds.control('modePanel4', q=True, width=True)
viewport_position = pointWorldToCam('persp', (point2[0], point2[1], point2[2]), (width, height))


And I capture the real mouse position (screen_position) by using this code
#include <iostream> #include<windows.h> int main()
{ while(1){ POINT p; GetCursorPos(&p); std::cout << "screen_position: x:" << p.x << ", y:" << p.y << ". " << std::endl; } return 0; }

It shows that viewport_position does not equal with screen_position


My questions:
1. What's wrong in Method1 and Method2?
2. Even Method1 and Method2 could output the result, I'm afraid that their results are viewport positions which cannot be used to manipulate the mouse to click the specified vertex.
So, how to get the screen position from viewport position? is it possible in Maya?

Cheers
Yao





yaoys

unread,
Jun 13, 2018, 10:59:45 PM6/13/18
to Python Programming for Autodesk Maya
I find out the problem in Method1. I should use 
v= omui.M3dView().active3dView()
rather than 
v= omui.M3dView()

I confirmed that it is a viewport position, not the screen position.
Reply all
Reply to author
Forward
0 new messages