get screen position


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

Use M3dView.

def screenPositionAPI(panel, pos):
	mp = OpenMaya.MPoint(pos[0], pos[1], pos[2])
	m3v = OpenMayaUI.M3dView.active3dView()
	OpenMayaUI.M3dView.getM3dViewFromModelPanel(panel, m3v)
 
	xp = OpenMaya.MScriptUtil().asShortPtr()
	yp = OpenMaya.MScriptUtil().asShortPtr()
	ret = m3v.worldToView(mp, xp, yp)
	x = OpenMaya.MScriptUtil().getShort(xp)
	y = OpenMaya.MScriptUtil().getShort(yp)
 
	xPtr = OpenMaya.MScriptUtil().asUintPtr()
	yPtr = OpenMaya.MScriptUtil().asUintPtr()
	widthPtr = OpenMaya.MScriptUtil().asUintPtr()
	heightPtr = OpenMaya.MScriptUtil().asUintPtr()
	m3v.viewport(xPtr, yPtr, widthPtr, heightPtr)
	viewWidth = OpenMaya.MScriptUtil().getUint( widthPtr )
	viewHeight = OpenMaya.MScriptUtil().getUint( heightPtr )
 
	return (float(x)/float(viewWidth), float(y)/float(viewHeight))
 

Or calculate by yourself(using inverse Matrix)

def screenPositionAPI2(cam, pos):
	mp = OpenMaya.MPoint(pos[0], pos[1], pos[2])
	camDagPath = mDagPathFromName(cam)
	mtx = camDagPath.inclusiveMatrixInverse()
	newPos = mp * mtx
	resX = - newPos.x / newPos.z  * cmds.camera(cam, q=True, fl=True)
	resY = - newPos.y / newPos.z  * cmds.camera(cam, q=True, fl=True)
	resX /=  cmds.camera(cam, q=True, hfa=True)/ 25.4
	resY /=  cmds.camera(cam, q=True, vfa=True)/ 25.4
	return (resX, resY)
 

This one is better.

import math
def screenPositionAPI3(cam, pos):
	mp = OpenMaya.MPoint(pos[0], pos[1], pos[2])
	camDagPath = mDagPathFromName(cam)
	mtx = camDagPath.inclusiveMatrixInverse()
	newPos = mp * mtx
	hfv = cmds.camera(cam, q=True, hfv=True)
	resX = ((newPos.x/(-newPos.z))/math.tan(math.radians(hfv/2)))/2.0;
	vfv = cmds.camera(cam, q=True, vfv=True)
	resY = ((newPos.y/(-newPos.z))/math.tan(math.radians(vfv/2)))/2.0;
	return (resX, resY)
 
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。