Create animated camera from multiple cameras in Maya (Python)
This script creates a single animated camera from selected cameras. In order to set the keyframes to correct frames, the cameras need to have imagePlane node that has an image file containing the frame number. The image file needs to be in format “image.####.ext”. You will also need to change the file_ext variable string to the file extension that the image sequence is using. This is so that the script can substring out the frame number correctly.
Logic around the script:
List selected nodes
create a new camera and store its shape and transform node names
for each camera in selection
read transform and camera attributes to variables
set these attributes to the newly created camera
read frame number from the image file in the image plane of the camera
create a key at this frame to all the attributes
import maya.cmds as cmds file_ext = ".jpg" selected = cmds.ls( sl=True ) new_cam = cmds.camera( n='anim_cam') cameraShape = new_cam[1] cameraXform = new_cam[0] print(cameraShape) print(cameraXform) for node in selected: pos = cmds.camera(node, q=True, p=True) rot = cmds.camera(node, q=True, rot=True) focal_len = cmds.camera(node, q=True, fl=True) vfilm_apert = cmds.camera(node, q=True, vfa=True) hfilm_apert = cmds.camera(node, q=True, hfa=True) relatives = cmds.listRelatives( node ) imagefile = cmds.getAttr ( relatives[0] + '.imageName' ) frame = imagefile[(-4-len(file_ext)):(len(file_ext)*-1)] print(frame) cmds.camera(cameraShape, e=True, p=pos ) cmds.camera(cameraShape, e=True, rot=rot ) cmds.camera(cameraShape, e=True, fl=focal_len ) cmds.camera(cameraShape, e=True, vfa=vfilm_apert ) cmds.camera(cameraShape, e=True, hfa=hfilm_apert ) cmds.setKeyframe( cameraXform, t=frame ) cmds.setKeyframe( cameraShape, t=frame )