|
| 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