bake custom attributes to camera during export

This commit is contained in:
Ondrej Samohel 2020-07-24 19:52:20 +02:00
parent 40b470b1cf
commit bd7876fe20
No known key found for this signature in database
GPG key ID: 8A29C663C672C2B7
2 changed files with 59 additions and 0 deletions

View file

@ -19,6 +19,7 @@ class ExtractCameraAlembic(pype.api.Extractor):
label = "Camera (Alembic)"
hosts = ["maya"]
families = ["camera"]
bake_attributes = []
def process(self, instance):
@ -66,6 +67,14 @@ class ExtractCameraAlembic(pype.api.Extractor):
job_str += ' -file "{0}"'.format(path)
# bake specified attributes in preset
assert isinstance(self.bake_attributes, (list, tuple)), (
"Attributes to bake must be specified as a list"
)
for attr in self.bake_attributes:
self.log.info("Adding {} attribute".format(attr))
job_str += " -attr {0}".format(attr)
with lib.evaluation("off"):
with avalon.maya.suspended_refresh():
cmds.AbcExport(j=job_str, verbose=False)

View file

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
"""Extract camera to Maya file."""
import os
from maya import cmds
@ -65,6 +67,45 @@ def unlock(plug):
cmds.disconnectAttr(source, destination)
def bake_attribute(camera,
attributes,
step=1.0, simulation=True, frame_range=None):
"""Bake specified attributes on camera.
Args:
camera (str): Camera name.
attributes (list): List of attributes to bake.
step (float): Animation step used for baking.
simulation (bool): Perform simulation instead of just evaluating
each attribute separately over the range of time.
frame_rage (list, tuple): start and end frame to define range.
.. See also:
http://download.autodesk.com/us/maya/2011help/Commandspython/bakeResults.html
"""
if frame_range is None:
frame_range = [cmds.playbackOptions(query=True, minTime=True),
cmds.playbackOptions(query=True, maxTime=True)]
# If frame range is single frame bake one frame more,
# otherwise maya.cmds.bakeResults gets confused
if frame_range[1] == frame_range[0]:
frame_range[1] += 1
assert isinstance(attributes, (list, tuple)), (
"Attributes to bake must be specified as a list"
)
with lib.keytangent_default(in_tangent_type='auto',
out_tangent_type='auto'):
cmds.bakeResults(camera, attribute=attributes,
simulation=simulation,
time=(frame_range[0], frame_range[1]),
sampleBy=step)
class ExtractCameraMayaAscii(pype.api.Extractor):
"""Extract a Camera as Maya Ascii.
@ -84,6 +125,7 @@ class ExtractCameraMayaAscii(pype.api.Extractor):
label = "Camera (Maya Ascii)"
hosts = ["maya"]
families = ["camera"]
bake_attributes = []
def process(self, instance):
@ -148,6 +190,14 @@ class ExtractCameraMayaAscii(pype.api.Extractor):
unlock(plug)
cmds.setAttr(plug, value)
if self.bake_attributes:
self.log.info(
"Baking attributes: {}".format(
self.bake_attributes))
bake_attribute(
cam, self.bake_attributes,
frame_range=range_with_handles, step=step)
self.log.info("Performing extraction..")
cmds.select(baked_shapes, noExpand=True)
cmds.file(path,