Skip to content

Commit e10370d

Browse files
committed
Animated Mesh import Tutorial (AL PR #967)
1 parent c01961a commit e10370d

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

tutorials/animatedMesh/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Animated Geometry Importer
3+
4+
In this folder there is a sample script that shows how to implement an "AlembicMaya" like import workflow in AL_USDMaya - i.e we:
5+
+ import a Proxy shape that contains some models that contain animated Geometry (typically...a character or prop)
6+
+ We want to convert that USD geometry to animated geometry in maya which is driven "live" from the Proxy Shape and animates as we scrub the timeline
7+
8+
This functionality is supported by AL_USDMaya, but is not exposed in any easy to use way - this script is an attempt to make that easier
9+
10+
```
11+
# Import the animatedMeshImport class in your preferred way
12+
proxyShape = cmds.AL_usdmaya_ProxyShapeImport(file='/your/usd/file.usdc')
13+
shape = usdmaya.ProxyShape.getByName(proxyShape[0])
14+
prims = Usd.PrimRange.Stage(shape.getUsdStage())
15+
primList = [prim for prim in prims][:1]
16+
parents = cmds.listRelatives(proxyShape[0], ap=True)
17+
animatedTranslator = TranslateUSDMeshesToMaya(parents[0], primList)
18+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
import logging
3+
logger = logging.getLogger(__name__)
4+
from pxr import Usd, UsdGeom
5+
from maya import cmds
6+
from AL import usdmaya
7+
8+
9+
10+
class TranslateUSDMeshesToMaya(object):
11+
"""
12+
Translate all child meshes from a given set of parent prims from the usd scene into maya
13+
"""
14+
15+
def __init__(self, proxyShapeName, parentPrims, onlyTranslateVisible=True, isAnimated=True, doPostTranslation=True):
16+
primsToTranslate = []
17+
for prim in parentPrims:
18+
primsToTranslate.extend(self.getPrimsToTranslate(prim, onlyTranslateVisible))
19+
20+
if not primsToTranslate:
21+
logger.error('Failed to find any prims to translate to maya')
22+
return
23+
24+
for prim in primsToTranslate:
25+
primPath = prim.GetPrimPath().pathString
26+
cmds.AL_usdmaya_TranslatePrim(ip=primPath, fi=True, proxy=proxyShapeName)
27+
# Selecting the mesh and applying static or animated mesh import is a temporary work around,
28+
# this will eventually be moved into the AL_usdmaya_TranslatePrim command as an argument
29+
if doPostTranslation:
30+
self.createMayaNodes(proxyShapeName, primPath, isAnimated)
31+
32+
@classmethod
33+
def getPrimsToTranslate(cls, parentPrim, onlyTranslateVisible):
34+
prims = []
35+
for prim in Usd.PrimRange(parentPrim):
36+
if prim.IsInstance():
37+
for instancedPrim in prim.GetFilteredChildren(Usd.TraverseInstanceProxies()):
38+
prims.extend(cls._getPrimsToTranslate(instancedPrim, onlyTranslateVisible))
39+
else:
40+
if prim.GetTypeName() != 'Mesh':
41+
continue
42+
if onlyTranslateVisible and UsdGeom.Imageable(prim).ComputeVisibility() == 'invisible':
43+
continue
44+
prims.extend(list(Usd.PrimRange(prim)))
45+
return prims
46+
47+
@classmethod
48+
def createMayaNodes(cls, proxyShapeName, primPath, isAnimated):
49+
dagPath = '{}{}'.format(proxyShapeName, primPath.replace('/', '|'))
50+
proxyShape = cmds.listConnections('{}.inStageData'.format(dagPath))[0]
51+
mesh = cmds.listRelatives(dagPath, c=True, type='mesh')[0]
52+
if cmds.listConnections('{}.inMesh'.format(mesh), s=True):
53+
return
54+
55+
if isAnimated:
56+
usdMaya_meshNode = cmds.createNode('AL_usdmaya_MeshAnimCreator')
57+
usdMaya_deformerNode = cmds.createNode('AL_usdmaya_MeshAnimDeformer')
58+
cmds.connectAttr('time1.outTime', '{}.inTime'.format(usdMaya_deformerNode))
59+
cmds.connectAttr('{}.outMesh'.format(usdMaya_meshNode), '{}.inMesh'.format(usdMaya_deformerNode))
60+
cmds.connectAttr('{}.outMesh'.format(usdMaya_deformerNode), '{}.inMesh'.format(mesh))
61+
for node in [usdMaya_meshNode, usdMaya_deformerNode]:
62+
cmds.setAttr('{}.primPath'.format(node), primPath, type='string')
63+
cmds.connectAttr('{}.outStageData'.format(proxyShape), '{}.inStageData'.format(node))
64+
else:
65+
usdMaya_meshNode = cmds.createNode('AL_usdmaya_MeshAnimCreator')
66+
cmds.setAttr('{}.primPath'.format(usdMaya_meshNode), primPath, type='string')
67+
cmds.connectAttr('{}.outStageData'.format(proxyShape), '{}.inStageData'.format(usdMaya_meshNode))
68+
cmds.connectAttr('{}.outMesh'.format(usdMaya_meshNode), '{}.inMesh'.format(mesh))
69+

0 commit comments

Comments
 (0)