Hello!
I tried to replicate Maya camera 4x4 projection by extracting camera attributes, but I am not quite sure how maya calculate its projection matrix.
Comparing with the projection matrix obtained via open maya function(MFnCamera projectionMatrix),
The first cell (cam(0,0) ) is different. Not sure what is the way maya calculate its projection matrix.
Also I haven't consider the camera film offset(cameraShape1.horizontalFilmOffset and cameraShape1.verticalFilmOffset)
and film roll value (cameraShape1.filmRollValue)
Here is my comparision code.
Is anyone have idea? Thank you!
#################
import maya.OpenMaya as om
import maya.cmds as cmds
import math
import maya.mel as mm
def getProjectionMatrix(camera):
cameraShape = cmds.listRelatives(camera, children=True)[0]
list1 = om.MSelectionList()
list1.add(cameraShape)
depNode = om.MObject()
list1.getDependNode(0, depNode)
camFn = om.MFnCamera(depNode)
pMtx = om.MFloatMatrix()
pMtx = camFn.projectionMatrix()
cameraMatrix = []
for i in range(4*4):
cameraMatrix.append(0.0)
for i in range(4):
for j in range(4):
cameraMatrix[i*4 + j] = pMtx(i,j)
print("This is the camera projection matrix obtained via Open Maya")
print(cameraMatrix)
##########
myCameraMatrix = []
for i in range(4*4):
myCameraMatrix.append(0.0)
command = "camera -q -hfv " + cameraShape + ";"
fov = mm.eval(command)
command = "camera -q -vfv " + cameraShape + ";"
vfv = mm.eval(command)
command = "getAttr " + cameraShape + ".verticalFilmAperture;"
verticalFilmAperture = mm.eval(command)
command = "getAttr " + cameraShape + ".horizontalFilmAperture;"
horizontalFilmAperture = mm.eval(command)
aspectRatio = float(horizontalFilmAperture)/float(verticalFilmAperture)
command = "getAttr " + cameraShape + ".nearClipPlane;"
zNear = mm.eval(command)
command = "getAttr " + cameraShape + ".farClipPlane;"
zFar = mm.eval(command)
command = "getAttr " + cameraShape + ".preScale;"
preScale = mm.eval(command)
command = "getAttr " + cameraShape + ".postScale;"
postScale = mm.eval(command)
command = "getAttr " + cameraShape + ".filmTranslateH;"
filmTranslateH = mm.eval(command)
command = "getAttr " + cameraShape + ".filmTranslateV;"
filmTranslateV = mm.eval(command)
xMax = zNear * math.tan(math.radians(0.5*fov))
#xMin = -1*xMax
yMax = zNear * math.tan(math.radians(0.5*vfv))
#yMin = -1*yMax
#yMax = xMax / aspectRatio
#yMin = xMin / aspectRatio
myCameraMatrix[0] = preScale * postScale * ((zNear) / (xMax))
myCameraMatrix[1*4 + 1] = preScale * postScale * ((zNear) / (yMax))
myCameraMatrix[2*4 + 2] = (zFar + zNear) / (zFar - zNear)
myCameraMatrix[2*4] = filmTranslateH
myCameraMatrix[2*4 + 1] = filmTranslateV
myCameraMatrix[2*4 + 3] = -1.0
myCameraMatrix[3*4 + 2] = (2.0 * zFar * zNear) / (zFar - zNear)
print("This is my projection matrix")
print(myCameraMatrix)
The result i got is something like:
This is the camera projection matrix obtained via Open Maya
[3.7144701480865479, -0.25955617427825928, 0.0, 0.0, 0.19466713070869446, 4.9526267051696777, 0.0, 0.0, 1.6421337127685547, 1.9714449644088745, 1.0033389329910278, -1.0, 0.0, 0.0, 20.033390045166016, 0.0]
This is my projection matrix
[3.1740308789637033, 0.0, 0.0, 0.0, 0.0, 4.9594232483807872, 0.0, 0.0, 0.0, 0.0, 1.003338898163606, -1.0, 0.0, 0.0, 20.033388981636062, 0.0]