one can be neater (and magnitudes faster) in python (since maya knows the operations directly with api calls*) same problems apply.
import maya.OpenMaya as om
def pointWorldToCam(cameraName, point, res):
sel = om.MSelectionList()
dag = om.MDagPath()
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,point,point)
fullMat = transMat.inverse() * projMat * postProjMat
nuPoint = point * fullMat
print pointWorldToCam('persp', (0,0,0), (640,480))
Mayas cordinates are measured form center positive being up. For example adobbe shows points in editors form top left hence the /2+0.5 and inverse for other axis. However many programs including adobe (eg illustrator, pdf, eps etc.) do internally use lower left corners as starting point. Modify to suit your need.
BTW while searched for my old codes i stumbled across:
that does much the same except for a bit of unnecceserily extra code for floatmatrix to mmatrix transform. i guess its the same base source as your code above. But yeah common ogl code style. Whileas i have a Lisplike dialect of coding.
PS: if you need many points then cache the fullMatrix
- It looks a bit long because c is like that you need to declare thinkgs as types up front and the om is a c++ wrapper. So it makes it unneccery longwinded. Only the last 3 lines actually do anything other than fetching data. If you use somepthing like pymel it gets shorter to write