PDA

View Full Version : viewToWorld() trouble.


BiGMaCHiNe
11-30-2011, 07:56 AM
Hi all,
Im just trying to write a basic script to return values on the near and far plane that correspond to viewport coordinates. At the moment I always get a return of None, regardless of what values im passing into the viewToWorld(xpos,ypos... ) component.

Ive culled down the script to a barebones version to try and figure it out. Still trying to wrap my head around the API a bit.


import maya.OpenMaya as om
import maya.OpenMayaUI as omUI

maya3DViewHandle = omUI.M3dView()
activeView = maya3DViewHandle.active3dView()
argX = om.MScriptUtil()
argX.createFromInt(200)
argXPtr = argX.asShortPtr()
xpos = om.MScriptUtil(argXPtr).asShort()
np = om.MPoint()
fp = om.MPoint()
print activeView.viewToWorld(xpos,xpos,np, fp)

NaughtyNathan
11-30-2011, 10:46 AM
I'm not that comfortable with the API either, but I think the problem is that the M3dView.viewToWorld method doesn't return any data (well, it does, but it's simply the status code (which python doesn't use?)) in other words, there is nothing to print.
looking at the docs for viewToWorld shows that the first two params (xpos,xpos) are input [in] and the second two (np, fp) are output [out], so if you want to print the "results" you need to print the contents of your two [out] params, np and fp:
print np.x,np.y,np.z
print fp.x,fp.y,fp.z
:nathaN

Azrail
11-30-2011, 04:16 PM
Nathan is right, also you don't need to use pointers in this case, and also you don't need the first M3dView instance. The code can be like this:

import maya.OpenMaya as om
import maya.OpenMayaUI as omUI
activeView = omUI.M3dView.active3dView()
np = om.MPoint()
fp = om.MPoint()
activeView.viewToWorld(100,100,np, fp)
print 'Near', [np[i] for i in range(3)]
print 'Far', [fp[i] for i in range(3)]


Pointers are required when the API call requires float/double/int/short type variable as an output parameter - if you like to convert point in space to viewport coordinates, and then the whole MScriptUtil mess is required:

import maya.OpenMaya as om
import maya.OpenMayaUI as omUI
activeView = omUI.M3dView.active3dView()
wp = om.MPoint(0,0,0)
msuX = om.MScriptUtil()
x = msuX.asShortPtr()
msuY = om.MScriptUtil()
y = msuY.asShortPtr()
activeView.worldToView(wp, x, y)
x = msuX.getShort(x)
y = msuX.getShort(y)
np = om.MPoint()
fp = om.MPoint()
activeView.viewToWorld(x,y,np, fp)
print 'Near', [np[i] for i in range(3)]
print 'Far', [fp[i] for i in range(3)]

BiGMaCHiNe
11-30-2011, 09:13 PM
ah, thanks for that guys.
I think the penny just dropped on making the API docs alot clearer!

CGTalk Moderation
11-30-2011, 09:13 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.