JSON Maya Camera Exporter

preview_player
Показать описание
JSON is a very fast and lightweight inter exchange format that can be leveraged to export most data that consists of key value pairs.

This video covers the process of writing a simple camera exporter for Maya. We will cover the process of writing a Python module that can handle the export of the necessary camera data
Рекомендации по теме
Комментарии
Автор

import os, json
import maya.cmds as cmds
'''
In Maya's script editor:

import maya.cmds as cmds
import tdcjson.mayaCamera as mc

reload(mc)
mc.exportCamera()

# or if you want to explicitly name an existing camera:


'''


def exportCamera(camera=None, fileName=None):
'''Export Maya camera to JSON'''

# First let's make sure a camera is provided
if not camera:
# Get the current selection
selection = cmds.ls(selection=True)

# If nothing selected, stop
if len(selection) == 0:
print "You must select a camera to export"
return -1

# If more than one item is selected, stop
if len(selection) > 1:
print "You must select a single camera"
return -1

# If there is only one thing selected, let's store it
camera = selection[0]

# Make sure the selected object is indeed a camera
if cmds.nodeType(camera) == "transform":
cameraShape = cmds.listRelatives(camera, children=True)[0]
# If children is a camera then we continue
if cmds.nodeType(cameraShape) != "camera":
print "The selected object is not a camera"
return -1

# Check if name provided
if not fileName:
# Then bring up a file dialog
basicFilter = ".json"
retval = cmds.fileDialog2(dialogStyle=2, fm=0)
# If a file was selected
if retval:
file, ext = os.path.splitext(retval[0])
# If the extension is not *.json, let's add it
if ext != ".json":
fileName = file + ".json"
else:
fileName = retval[0]

#print fileName

# Gather information about the camera
frameStart = int(cmds.playbackOptions(query=True, min=True))
frameEnd = int(cmds.playbackOptions(query=True, max=True))
 
# Set the format for the JSON export:
outData = {
"cameraName": camera,
"frameStart": frameStart,
"frameEnd": frameEnd,

"vfov": cmds.camera(camera, query=True,
verticalFieldOfView=True),

"hfov": cmds.camera(camera, query=True,
horizontalFieldOfView=True),

"frames": {"matrix_world":{}}
}

# Loop over all the frames in the animation
for i in range(frameStart, frameEnd + 1):
# Set the current frame
cmds.currentTime(i)
# Get the camera matrix in world space
= cmds.xform(camera,
query=True,
matrix=True,
worldSpace=True)

cmds.currentTime(frameStart)
# Save data to json
fout = open(fileName, 'w')
json.dump(outData, fout, indent=2)
fout.close()

print "Exported successfully to %s" % fileName


if __name__ == "__main__":
exportCamera()

rootytuners