View Full Version : Project point from world to camera coordinates

11 November 2010, 09:03 PM
Hi everyone,

I was looking for some advice on how to write a better version of the python function below. It seems a bit messy and it also doesnt seem to work very well when you change the pivot point of the camera.

Basically, I want to take a point from the world and project it back into the camera. There is also a call to a funtion detectCollisionBack which returns a None value if there is no occlusion on the way from the world to the camera.


def worldToImage(cameraName, imageXRes, imageYRes, worldX, worldY, worldZ, frameNo):

# set time

# check if object is occluded in second frame
camPos =, q=True, p=True)
hitPt = detectCollisionBack(worldX, worldY, worldZ, camPos[0], camPos[1], camPos[2])

if (hitPt == None):
p = OpenMaya.MPoint(worldX, worldY, worldZ)
sl = OpenMaya.MSelectionList()
sl.getDagPath(0, dpathCameraShape)
fnc = OpenMaya.MFnCamera(dpathCameraShape.node())

projMat = floatMMatrixToMMatrix_(fnc.projectionMatrix())
camMatInv = dpathCameraShape.inclusiveMatrix().inverse();

projected = p * camMatInv * projMat

imageX = (projected.x/projected.w / 2.0 + 0.5) * imageXRes
imageY = imageYRes - ((projected.y/projected.w / 2.0 + 0.5) * imageYRes)
return imageX, imageY

return None

06 June 2011, 01:52 PM
@ themush : it is not really linked to what you asked above but I saw that you tried to link coordinates of a rendered image to world coordinates in Maya, did you success ? (I am talking about this thread :
Because I am trying to do the same thing and I would like advice, please :)

Many thanks for you answer.

06 June 2011, 04:37 AM
There's a wonderful script from Rob Bedrow that would return normalized 2d values of a specific point in 3d space. It's called screenSpace.mel.


06 June 2011, 10:44 AM
Many thanks for this script, I will look at it ! :)

I wondered whether it would be possible to do the opposite operation (because I would be far more interested in this direction) : selecting a point in a 2D rendered image and using a script (or something else...) which returns the coordinates of the point in the 3D space ?
Because I understood that it was this operation that themush wanted to do in the thread ( (, am I right ?


CGTalk Moderation
06 June 2011, 10:44 AM
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.