Skip to content

Instantly share code, notes, and snippets.

@patwooky
Created May 17, 2019 03:10
Show Gist options
  • Save patwooky/fe384eaed98f1a92b68e95c5ff3438c8 to your computer and use it in GitHub Desktop.
Save patwooky/fe384eaed98f1a92b68e95c5ff3438c8 to your computer and use it in GitHub Desktop.
Bakes a Maya camera's Translate rotate and focal length out, set all keys to spline tangents, and set pre/post infinity of curves to linear
from pymel.core import *
from pprint import pprint as pp
'''
BakeMmCam
version 001_01
written by Patrick Woo (patrickwoo@yahoo.com)
This script
- bakes a Maya camera's Translate rotate and focal length out,
- set all keys to spline tangents,
- and set pre/post infinity of curves to linear
'''
def bakeMmCam(sel):
try:
camShape = sel.listRelatives(ad=True, type='camera')[0]
except:
print('\nCamera not found in selection. Aborted.')
return
cam = camShape.getParent()
# unlock attributes required attributes for camera transform and camera shape nodes
bakeAttrListCam = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz']
[cam.attr(x).unlock() for x in bakeAttrListCam]
bakeAttrListCamShape = ['focalLength']
[camShape.attr(x).unlock() for x in bakeAttrListCamShape]
# builds a string like '1001:1044' which represents 'start frame:end frame' for bakeResults
minMax = '{}:{}'.format( playbackOptions(q=True, min=True), playbackOptions(q=True, max=True) )
# print minMax
bakeResults(cam, simulation=True, time=minMax, sampleBy=1, oversamplingRate=1, disableImplicitControl=True,
preserveOutsideKeys=True, removeBakedAttributeFromLayer=False, bakeOnOverrideLayer=False,
minimizeRotation=True, at=bakeAttrListCam)
# set all animated curves on cam transform node and cam shape node to spline tangents
for myObj in [cam, camShape]:
interp = 'spline'
keyTangent(myObj, inTangentType=interp, outTangentType=interp)
# get all anim curves from cam transform
connections = cam.listConnections(type='animCurve')
connections += camShape.listConnections(type='animCurve') # include cam shape animated curves
# pp(connections)
for thisConnection in connections:
thisConnection.setPreInfinityType('linear')
thisConnection.setPostInfinityType('linear')
print('\nMatchmove cam baking complete.')
return
try:
bakeMmCam(ls(sl=True)[0])
except:
print('\nSelect a camera obj to bake, then run the script.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment