PDA

View Full Version : Project point from world to camera coordinates


themush
11-02-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.

Thanks,
O



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

# set time
cmds.currentTime(frameNo)

# check if object is occluded in second frame
camPos = cmds.camera(cameraName, 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.add(cameraName)
dpathCameraShape=OpenMaya.MDagPath()
sl.getDagPath(0, dpathCameraShape)
dpathCameraShape.extendToShape()
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

takeo13
06-09-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 : http://forums.cgsociety.org/showthread.php?f=89&t=771313)
Because I am trying to do the same thing and I would like advice, please :)

Many thanks for you answer.

isoparmB
06-10-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.

http://www.185vfx.com/resources/screenSpace.mel

Cheers.

takeo13
06-10-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 (http://forums.cgsociety.org/showthr...p?f=89&t=771313 (http://forums.cgsociety.org/showthread.php?f=89&t=771313)), am I right ?

Cheers.

CGTalk Moderation
06-10-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.