Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def worldSpaceToScreenSpace(camera, worldPoint):
- # get current resolution
- resWidth = mc.getAttr('defaultResolution.width')
- resHeight = mc.getAttr('defaultResolution.height')
- # get the dagPath to the camera shape node to get the world inverse matrix
- selList = om.MSelectionList()
- selList.add(camera)
- dagPath = om.MDagPath()
- selList.getDagPath(0,dagPath)
- dagPath.extendToShape()
- camInvMtx = dagPath.inclusiveMatrix().inverse()
- # use a camera function set to get projection matrix, convert the MFloatMatrix
- # into a MMatrix for multiplication compatibility
- fnCam = om.MFnCamera(dagPath)
- mFloatMtx = fnCam.projectionMatrix()
- projMtx = om.MMatrix(mFloatMtx.matrix)
- # multiply all together and do the normalisation
- mPoint = om.MPoint(worldPoint[0],worldPoint[1],worldPoint[2]) * camInvMtx * projMtx;
- x = (mPoint[0] / mPoint[3] / 2 + .5) * resWidth
- y = (mPoint[1] / mPoint[3] / 2 + .5) * resHeight
- return [x,y]
Add Comment
Please, Sign In to add comment