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
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
