Maya FOV to Unity FOV – Camera Matching

Maya_to_Unity_FOV_Conversion

So anyone who’s set up a scene in Maya and subsequently imported that scene to Unity has probably discovered two things. One, that Cameras don’t translate over. And two, that Unity uses a completely different field of view, FOV, compared to Maya. The solution? This python script for Maya, which adds a custom channel to the attributes of anything selected and named “camera1.”

This is version 0.1, so expect future improvements as I find more Maya to Unity workflow bottlenecks that are driving me NUTS. The code is includ and if you would like to donate to my lunch fund, please do so at the bottom. Thanks for being such a great community, and I look forward to solving more Maya issues in the future!


##################### Maya Python Code ############################
################### Maya FOV to Unity FOV - "camera1" ################
################### version 0.1 by John Draisey #######################
# Instructions - Select a camera called "camera1", apply the script either via the
# script editor or as a shelf button. The custom Unity FOV field should be visible
# in the attributes of your "camera1".

#imports maya python, imports the math functions, sets up variable Pi
import maya.cmds as cmds
import math
pie = float(3.14159265359)

#gets the name of the selected camera and stores it as variable "stringCamName"
stringCamName = cmds.ls( selection=True )

# creates a custom channel in the selected camera called "Unity_FOV".
cmds.addAttr( longName='Unity_FOV', attributeType='float', readable = True )

#establishes the aspect ratio of your camera using the Maya render settings, as well as the
#horizontal field of view of your camera1
renderHeight = float(cmds.getAttr ("defaultResolution.height"))
renderWidth = float(cmds.getAttr ("defaultResolution.width"))
horizontalFOV = float(cmds.camera('cameraShape1', q=True, horizontalFieldOfView=True))

#convert horizontalFOV to radians
horizontalFOVRAD = (horizontalFOV * pie )/ 180

#ran into a calculation issue down below when I didn't have horizontalFOVRAD divided
#I may need to learn more about math...
horizontalFOVRADhalf = float(horizontalFOVRAD/2)

#calculate verticalFOVRAD
verticalFOVRAD = 2 * math.atan ((math.tan(horizontalFOVRADhalf) * (renderHeight/renderWidth)))

#convert back to degrees, what a nightmare, right?
verticalFOV = (180 * verticalFOVRAD) / pie

#copies the value of variable "vfov" to the custom channel "Unity_FOV"
#rejoice, you're done!
cmds.setAttr ('camera1.Unity_FOV', verticalFOV)

#uncomment these lines if needed, it helped me double check the math calculations
#print renderHeight
#print renderWidth
#print horizontalFOV
#print horizontalFOVRAD
#print horizontalFOVRADhalf
#print verticalFOVRAD
#print verticalFOV





Leave a Reply