Пример #1
0
    def execute(self):
        targetMll = MllInterface(mesh=self.targetMesh)

        self.ensureTargetMeshLayers()

        previousLayerIds = [layerId for layerId, _ in targetMll.listLayers()]

        sourceMesh = self.sourceMesh
        if self.sourceModel is not None:
            self.sourceModel.saveTo(MllInterface.TARGET_REFERENCE_MESH)
            sourceMesh = MllInterface.TARGET_REFERENCE_MESH

        vertexTransferMode = self.vertexTransferModes[
            self.parent.controls.transferMode.getSelectedText()]

        sourceMll = MllInterface(mesh=sourceMesh)
        sourceMll.transferWeights(self.targetMesh,
                                  influencesMapping=self.mapper.mapping,
                                  vertexTransferMode=vertexTransferMode)

        if self.parent.controls.keepExistingLayers.getValue() != 1:
            for layerId in previousLayerIds:
                targetMll.deleteLayer(layerId)

        LayerDataModel.getInstance().updateLayerAvailability()
        LayerEvents.layerListModified.emit()
Пример #2
0
class DuplicateLayersTest(AdditionalAsserts, unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)
        
        self.mll = MllInterface()
        
        self.setup = DuplicateLayers()
        self.setup.setMllInterface(self.mll)
        
        testUtils.openMayaFile("influence transfer.mb")
        
        self.mll.setCurrentMesh('sourceMesh')
        
        self.mll.initLayers()
        
    def testLayerName(self):
        self.assertEquals("layer1 copy",self.setup.createLayerName("layer1"))
        self.assertEquals("layer1 copy(2)",self.setup.createLayerName("layer1 copy"))
        self.assertEquals("layer1 copy(1000001)",self.setup.createLayerName("layer1 copy(1000000)"))
        
        
    @insideMayaOnly
    def testDuplicateOneLayer(self):
        id = self.mll.createLayer("layer1 copy")
        self.setup.addLayer(id)
        self.setup.execute()
        
        newId = self.setup.duplicateIds[0]
        self.assertEqual(self.mll.getLayerName(newId),'layer1 copy(2)')
        self.assertEqual(self.mll.isLayerEnabled(newId),True)
        self.assertAlmostEqual(self.mll.getLayerOpacity(newId),1.0)
        self.assertFloatArraysEqual(self.mll.getLayerMask(newId), [])
        
        self.assertFloatArraysEqual(self.mll.getInfluenceWeights(id, 1), self.mll.getInfluenceWeights(newId, 1))
    
    @insideMayaOnly
    def testLayerOrder(self):
        id1 = self.mll.createLayer("layer1")
        id2 = self.mll.createLayer("layer2")
        
        def layerNames():
            return [a[1] for a in self.mll.listLayers()]
    
        self.assertArraysEqual(layerNames(), ["layer2","layer1"])
        
        self.setup.addLayer(id1)
        self.setup.addLayer(id2)
        
        self.setup.execute()
        
        # order respects "newer layers on top"
        self.assertArraysEqual(layerNames(), ["layer2 copy","layer1 copy","layer2","layer1"])
        
        # individual indexes in copy array respect indexes in the original array
        self.assertEquals(self.mll.getLayerName(self.setup.duplicateIds[0]),"layer1 copy")
        self.assertEquals(self.mll.getLayerName(self.setup.duplicateIds[1]),"layer2 copy")
Пример #3
0
def copySkinLayers(srcMeshName, destMeshName, layers, influenceAssociation, surfaceAssociation, sampleSpace, normalize, uv=None):
    '''
    layers [list] - ids of layers to be copied
    if layers is [], all layers will be copied
    '''
    srcMll = MllInterface()
    destMll = MllInterface()
    
    srcMll.setCurrentMesh(srcMeshName)
    destMll.setCurrentMesh(destMeshName)
    
    # check that selected objects are valid
    if False in (srcMll.getLayersAvailable(), destMll.getLayersAvailable()):
        mc.error("Skinning layers must be initialized on both source and destination meshes")
    
    if layers == []:
        layers = [layerId for layerId, _ in srcMll.listLayers()]
        layers.reverse()
        
    for eachLayer in layers:
        copySkinLayerById(srcMll, destMll, eachLayer, influenceAssociation, surfaceAssociation, sampleSpace, normalize, uv)
Пример #4
0
    def execute(self):
        targetMll = MllInterface(mesh=self.targetMesh)

        self.ensureTargetMeshLayers()
        
        previousLayerIds = [layerId for layerId, _  in targetMll.listLayers()]

        sourceMesh = self.sourceMesh        
        if self.sourceModel is not None:
            self.sourceModel.saveTo(MllInterface.TARGET_REFERENCE_MESH)
            sourceMesh = MllInterface.TARGET_REFERENCE_MESH
        
        vertexTransferMode = self.vertexTransferModes[self.parent.controls.transferMode.getSelectedText()]

        sourceMll = MllInterface(mesh=sourceMesh)
        sourceMll.transferWeights(self.targetMesh,influencesMapping=self.mapper.mapping,vertexTransferMode=vertexTransferMode)

        if self.parent.controls.keepExistingLayers.getValue()!=1:
            for layerId in previousLayerIds:
                targetMll.deleteLayer(layerId)

        LayerDataModel.getInstance().updateLayerAvailability()
        LayerEvents.layerListModified.emit()
Пример #5
0
class LayerData(object):
    '''
    Intermediate data object between ngSkinTools core and importers/exporters,
    representing all layers info in one skin cluster.
    
    .. py:attribute:: layers
        
        a list of :py:class:`Layer` objects.
        
    .. py:attribute:: influences
        
        a list of :py:class:`InfluenceInfo` objects. Provides information about influences
        that were found on exported skin data, and used for influence matching when importing.

    '''
    def __init__(self):
        self.layers = []
        self.mll = MllInterface()

        self.meshInfo = MeshInfo()

        self.influences = []

        # a map [sourceInfluenceName] -> [destinationInfluenceName]
        self.mirrorInfluenceAssociationOverrides = None

        self.skinClusterFn = None

    def addMirrorInfluenceAssociationOverride(self,
                                              sourceInfluence,
                                              destinationInfluence=None,
                                              selfReference=False,
                                              bidirectional=True):
        '''
        Adds mirror influence association override, similar to UI of "Add influences association".
        Self reference creates a source<->source association, bidirectional means that destination->source 
        link is added as well
        '''

        if self.mirrorInfluenceAssociationOverrides is None:
            self.mirrorInfluenceAssociationOverrides = {}

        if selfReference:
            self.mirrorInfluenceAssociationOverrides[
                sourceInfluence] = sourceInfluence
            return

        if destinationInfluence is None:
            raise MessageException("destination influence must be specified")

        self.mirrorInfluenceAssociationOverrides[
            sourceInfluence] = destinationInfluence

        if bidirectional:
            self.mirrorInfluenceAssociationOverrides[
                destinationInfluence] = sourceInfluence

    def addLayer(self, layer):
        '''
        register new layer into this data object
        
        :param Layer layer: layer object to add.
        '''
        assert isinstance(layer, Layer)
        self.layers.append(layer)

    @staticmethod
    def getFullNodePath(nodeName):
        result = cmds.ls(nodeName, l=True)
        if result is None or len(result) == 0:
            raise MessageException("node %s was not found" % nodeName)

        return result[0]

    def loadInfluenceInfo(self):
        self.influences = self.mll.listInfluenceInfo()

    def loadFrom(self, mesh):
        '''
        loads data from actual skin cluster and prepares it for exporting.
        supply skin cluster or skinned mesh as an argument
        '''

        self.mll.setCurrentMesh(mesh)

        meshExporter = MeshDataExporter()
        self.meshInfo = MeshInfo()
        if mesh != MllInterface.TARGET_REFERENCE_MESH:
            mesh, skinCluster = self.mll.getTargetInfo()
            meshExporter.setTransformMatrixFromNode(mesh)
            meshExporter.useSkinClusterInputMesh(skinCluster)
            self.meshInfo.verts, self.meshInfo.triangles = meshExporter.export(
            )
        else:
            self.meshInfo.verts = self.mll.getReferenceMeshVerts()
            self.meshInfo.triangles = self.mll.getReferenceMeshTriangles()

        self.loadInfluenceInfo()

        # map LayerId->layerIndex
        layerIndexById = {}
        for index, (layerID, layerName,
                    parentId) in enumerate(self.mll.listLayers()):
            layerIndexById[layerID] = index
            self.mirrorInfluenceAssociationOverrides = self.mll.getManualMirrorInfluences(
            )
            if len(self.mirrorInfluenceAssociationOverrides) == 0:
                self.mirrorInfluenceAssociationOverrides = None

            layer = Layer()
            layer.name = layerName
            self.addLayer(layer)

            layer.opacity = self.mll.getLayerOpacity(layerID)
            layer.enabled = self.mll.isLayerEnabled(layerID)

            layer.mask = self.mll.getLayerMask(layerID)
            layer.dqWeights = self.mll.getDualQuaternionWeights(layerID)

            # transform parent ID to local index in the model
            if parentId is not None:
                layer.parent = layerIndexById[parentId]

            for inflName, logicalIndex in self.mll.listLayerInfluences(
                    layerID, activeInfluences=True):
                if inflName == '':
                    inflName = None
                influence = Influence()
                if inflName is not None:
                    influence.influenceName = self.getFullNodePath(inflName)
                influence.logicalIndex = logicalIndex
                layer.addInfluence(influence)

                influence.weights = self.mll.getInfluenceWeights(
                    layerID, logicalIndex)

    def __validate(self):

        numVerts = self.mll.getVertCount()

        def validateVertCount(count, message):
            if count != numVerts:
                raise Exception(message)

        for layer in self.layers:
            if layer.mask is not None and len(layer.mask) != 0:
                validateVertCount(
                    len(layer.mask),
                    "Invalid vertex count for mask in layer '%s': expected size is %d"
                    % (layer.name, numVerts))

            for influence in layer.influences:
                validateVertCount(
                    len(influence.weights),
                    "Invalid weights count for influence '%s' in layer '%s': expected size is %d"
                    % (influence.influenceName, layer.name, numVerts))

                if self.skinClusterFn:
                    influence.logicalIndex = self.skinClusterFn.getLogicalInfluenceIndex(
                        influence.influenceName)

    @Utils.undoable
    def saveTo(self, mesh):
        '''
        saveTo(self,mesh)
        
        saves data to actual skin cluster
        '''

        # set target to whatever was provided
        self.mll.setCurrentMesh(mesh)

        if mesh == MllInterface.TARGET_REFERENCE_MESH:
            self.mll.setWeightsReferenceMesh(self.meshInfo.verts,
                                             self.meshInfo.triangles)

        if not self.mll.getLayersAvailable():
            self.mll.initLayers()

        if not self.mll.getLayersAvailable():
            raise Exception("could not initialize layers")

        # is skin cluster available?
        if mesh != MllInterface.TARGET_REFERENCE_MESH:
            mesh, self.skinCluster = self.mll.getTargetInfo()
            self.skinClusterFn = SkinClusterFn()
            self.skinClusterFn.setSkinCluster(self.skinCluster)

        self.__validate()

        # set target to actual mesh
        self.mll.setCurrentMesh(mesh)

        with self.mll.batchUpdateContext():
            if self.mirrorInfluenceAssociationOverrides:
                self.mll.setManualMirrorInfluences(
                    self.mirrorInfluenceAssociationOverrides)

            # IDS of created layers, in reverse order
            layerIds = []
            for layer in reversed(self.layers):
                layerId = self.mll.createLayer(name=layer.name,
                                               forceEmpty=True)
                layerIds.append(layerId)

                self.mll.setCurrentLayer(layerId)
                if layerId is None:
                    raise Exception(
                        "import failed: could not create layer '%s'" %
                        (layer.name))

                self.mll.setLayerOpacity(layerId, layer.opacity)
                self.mll.setLayerEnabled(layerId, layer.enabled)
                self.mll.setLayerMask(layerId, layer.mask)
                self.mll.setDualQuaternionWeights(layerId, layer.dqWeights)

                self.mll.setLayerWeightsBufferSize(layerId,
                                                   len(layer.influences))
                for influence in layer.influences:
                    # because layer was just created and will belong to same undo chunk, disabling undo
                    # for setting weights bit
                    self.mll.setInfluenceWeights(layerId,
                                                 influence.logicalIndex,
                                                 influence.weights,
                                                 undoEnabled=False)

            # layer.parent reference index is in normal order, therefore need to reverse it again
            layerIds = list(reversed(layerIds))

            for index, layer in enumerate(self.layers):
                if layer.parent is not None:
                    self.mll.setLayerParent(layerIds[index],
                                            layerIds[layer.parent])
                    # reparenting will move it to the end of the list; move it to the bottom instead
                    self.mll.setLayerIndex(layerIds[index], 0)

    def __repr__(self):
        return "[LayerDataModel(%r)]" % self.layers

    def getAllInfluences(self):
        '''
        a convenience method to retrieve a list of names of all influences used in this layer data object
        '''

        result = set()

        for layer in self.layers:
            for influence in layer.influences:
                result.add(influence.influenceName)

        return tuple(result)
Пример #6
0
class LayerDataModel:
    log = getLogger("layerDataModel")

    # holds instance of singleton object
    __instance = None

    @staticmethod
    def getInstance():
        '''
        returns singleton instance of LayerDataModel
        
        :rtype: LayerDataModel
        '''
        return LayerDataModel.__instance

    @classmethod
    def bindAll(cls):
        cls.__instance = LayerDataModel()

        MayaEvents.undoRedoExecuted.addHandler(
            cls.__instance.updateLayerAvailability)
        MayaEvents.nodeSelectionChanged.addHandler(
            cls.__instance.updateLayerAvailability)

        cls.__instance.updateLayerAvailability()

    def __init__(self):
        self.layerDataAvailable = None
        self.mll = MllInterface()
        self.clipboard = WeightsClipboard(self.mll)

    def setLayerListsUI(self, ui):
        self.layerListsUI = ui

    def getLayerListsUI(self):
        '''
        :rtype: LayerListsUI
        '''
        from ngSkinTools.ui import mainwindow
        mainWindow = mainwindow.MainWindow.getInstance()
        if mainWindow is None:
            return None
        return mainWindow.getLayersUI()

    def getSelectedLayer(self):
        listsUi = self.getLayerListsUI()
        if listsUi is None:
            return None
        return listsUi.getLayersList().getSelectedID()

    def getSelectedLayers(self):
        listsUi = self.getLayerListsUI()
        if listsUi is None:
            return []
        return listsUi.getSelectedLayers()

    def getSelectedInfluenceIds(self):
        listsUi = self.getLayerListsUI()
        if listsUi is None:
            return []
        return listsUi.getSelectedInfluenceIds()

    def updateLayerAvailability(self):
        '''
        checks if availability of skin layers changed with the 
        current scene selection 
        '''
        self.log.info("updating layer availability")

        oldValue = self.layerDataAvailable
        self.layerDataAvailable = self.mll.getLayersAvailable()
        if self.layerDataAvailable != oldValue:
            LayerEvents.layerAvailabilityChanged.emit()

    @Utils.undoable
    def addLayer(self, name):
        def guessParent():
            currentLayer = self.mll.getCurrentLayer()
            if currentLayer is None:
                return None
            # guess layer's new parent
            parentsByLayerId = dict([
                (layerId, parentId)
                for layerId, _, parentId in self.mll.listLayers()
                if currentLayer in (layerId, parentId)
            ])

            # current layer is a parent?
            if currentLayer in parentsByLayerId.values():
                return currentLayer

            # current layer has parent
            if currentLayer in parentsByLayerId.keys():
                return parentsByLayerId[currentLayer]

        layerId = self.mll.createLayer(name)
        self.mll.setLayerParent(layerId, guessParent())

        if layerId is None:
            return None

        LayerEvents.layerListModified.emit()

        self.setCurrentLayer(layerId)
        return layerId

    def removeLayer(self, layerId):
        self.mll.deleteLayer(layerId)
        LayerEvents.layerListModified.emit()
        LayerEvents.currentLayerChanged.emit()

    def setCurrentLayer(self, layerId):
        self.mll.setCurrentLayer(layerId)
        LayerEvents.currentLayerChanged.emit()

    def getCurrentLayer(self):
        return self.mll.getCurrentLayer()

    def attachLayerData(self):
        self.mll.initLayers()
        with self.mll.batchUpdateContext():
            self.addLayer('Base Weights')

        self.updateLayerAvailability()
        selectionState.selectionInfo.dropCache()

    def cleanCustomNodes(self):
        '''
        removes all custom nodes from current scene
        '''

        # just in case we were in the middle of painting
        cmds.setToolTo('selectSuperContext')

        LayerUtils.deleteCustomNodes()

        # notify the rest of the world
        self.updateLayerAvailability()
        selectionState.selectionInfo.dropCache()

    def getLayerName(self, layerId):
        return mel.eval('ngSkinLayer -id {0} -q -name'.format(int(layerId)))

    def setLayerName(self, layerId, name):
        self.mll.setLayerName(layerId, name)
        LayerEvents.nameChanged.emit()

    def getLayerOpacity(self, layerId):
        return mel.eval('ngSkinLayer -id {0} -q -opacity'.format(layerId))

    def getLayerEnabled(self, layerId):
        return mel.eval('ngSkinLayer -id {0} -q -enabled'.format(layerId))

    def setLayerEnabled(self, layerId, enabled):
        cmds.ngSkinLayer(e=True, id=layerId, enabled=1 if enabled else 0)

    def toggleLayerEnabled(self, layerId):
        self.setLayerEnabled(layerId, not self.getLayerEnabled(layerId))

    def getLayersCandidateFromSelection(self):
        '''
        for given selection, returns mesh and skin cluster node names where skinLayer data
        is (or can be) attached. 
        '''
        return self.mll.getTargetInfo()

    def getLayersAvailable(self):
        self.updateLayerAvailability()
        return self.layerDataAvailable

    def isDqMode(self):
        '''
        returns True if current skin cluster is operating in dual quaternion mode
        '''
        target = self.mll.getTargetInfo()
        if not target:
            return False
        skinCluster = target[1]
        return cmds.skinCluster(skinCluster, q=True, skinMethod=True) == 2
class DuplicateLayersTest(AdditionalAsserts, unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)

        self.mll = MllInterface()

        self.setup = DuplicateLayers()
        self.setup.setMllInterface(self.mll)

        testUtils.openMayaFile("influence transfer.mb")

        self.mll.setCurrentMesh('sourceMesh')

        self.mll.initLayers()

    def testLayerName(self):
        self.assertEquals("layer1 copy", self.setup.createLayerName("layer1"))
        self.assertEquals("layer1 copy(2)",
                          self.setup.createLayerName("layer1 copy"))
        self.assertEquals("layer1 copy(1000001)",
                          self.setup.createLayerName("layer1 copy(1000000)"))

    @insideMayaOnly
    def testDuplicateOneLayer(self):
        id = self.mll.createLayer("layer1 copy")
        self.setup.addLayer(id)
        self.setup.execute()

        newId = self.setup.duplicateIds[0]
        self.assertEqual(self.mll.getLayerName(newId), 'layer1 copy(2)')
        self.assertEqual(self.mll.isLayerEnabled(newId), True)
        self.assertAlmostEqual(self.mll.getLayerOpacity(newId), 1.0)
        self.assertFloatArraysEqual(self.mll.getLayerMask(newId), [])

        self.assertFloatArraysEqual(self.mll.getInfluenceWeights(id, 1),
                                    self.mll.getInfluenceWeights(newId, 1))

    @insideMayaOnly
    def testLayerOrder(self):
        id1 = self.mll.createLayer("layer1")
        id2 = self.mll.createLayer("layer2")

        def layerNames():
            return [a[1] for a in self.mll.listLayers()]

        self.assertArraysEqual(layerNames(), ["layer2", "layer1"])

        self.setup.addLayer(id1)
        self.setup.addLayer(id2)

        self.setup.execute()

        # order respects "newer layers on top"
        self.assertArraysEqual(
            layerNames(), ["layer2 copy", "layer1 copy", "layer2", "layer1"])

        # individual indexes in copy array respect indexes in the original array
        self.assertEquals(self.mll.getLayerName(self.setup.duplicateIds[0]),
                          "layer1 copy")
        self.assertEquals(self.mll.getLayerName(self.setup.duplicateIds[1]),
                          "layer2 copy")
Пример #8
0
class LayerData(object):
    '''
    Intermediate data object between ngSkinTools core and importers/exporters,
    representing all layers info in one skin cluster.
    
    .. py:attribute:: layers
        
        a list of :py:class:`Layer` objects.
        
    .. py:attribute:: influences
        
        a list of :py:class:`InfluenceInfo` objects. Provides information about influences
        that were found on exported skin data, and used for influence matching when importing.

    '''
    
    def __init__(self):
        self.layers = []
        self.mll = MllInterface()

        self.meshInfo = MeshInfo()
        
        self.influences = []
        
        # a map [sourceInfluenceName] -> [destinationInfluenceName]
        self.mirrorInfluenceAssociationOverrides = None
        
        self.skinClusterFn = None
        
    def addMirrorInfluenceAssociationOverride(self,sourceInfluence,destinationInfluence=None,selfReference=False,bidirectional=True):
        '''
        Adds mirror influence association override, similar to UI of "Add influences association".
        Self reference creates a source<->source association, bidirectional means that destination->source 
        link is added as well
        '''
        
        if self.mirrorInfluenceAssociationOverrides is None:
            self.mirrorInfluenceAssociationOverrides = {}
        
        if selfReference:
            self.mirrorInfluenceAssociationOverrides[sourceInfluence] = sourceInfluence
            return
        
        if destinationInfluence is None:
            raise MessageException("destination influence must be specified")
        
        self.mirrorInfluenceAssociationOverrides[sourceInfluence] = destinationInfluence
        
        if bidirectional:
            self.mirrorInfluenceAssociationOverrides[destinationInfluence] = sourceInfluence 
        

    def addLayer(self, layer):
        '''
        register new layer into this data object
        
        :param Layer layer: layer object to add.
        '''
        assert isinstance(layer, Layer)
        self.layers.append(layer)
       
    @staticmethod 
    def getFullNodePath(nodeName):
        result = cmds.ls(nodeName,l=True)
        if result is None or len(result)==0:
            raise MessageException("node %s was not found" % nodeName)
        
        return result[0]
    
    def loadInfluenceInfo(self):    
        self.influences = self.mll.listInfluenceInfo()
    
    def loadFrom(self, mesh):
        '''
        loads data from actual skin cluster and prepares it for exporting.
        supply skin cluster or skinned mesh as an argument
        '''
        
        self.mll.setCurrentMesh(mesh)
        
        meshExporter = MeshDataExporter()
        self.meshInfo = MeshInfo()
        if mesh!=MllInterface.TARGET_REFERENCE_MESH:
            mesh,skinCluster = self.mll.getTargetInfo()
            meshExporter.setTransformMatrixFromNode(mesh)
            meshExporter.useSkinClusterInputMesh(skinCluster)
            self.meshInfo.verts,self.meshInfo.triangles = meshExporter.export()
        else:
            self.meshInfo.verts = self.mll.getReferenceMeshVerts()
            self.meshInfo.triangles = self.mll.getReferenceMeshTriangles()

        self.loadInfluenceInfo()
        
        for layerID, layerName in self.mll.listLayers():
            self.mirrorInfluenceAssociationOverrides = self.mll.getManualMirrorInfluences()
            if len(self.mirrorInfluenceAssociationOverrides)==0:
                self.mirrorInfluenceAssociationOverrides = None
            
            layer = Layer()
            layer.name = layerName
            self.addLayer(layer)
            
            
            layer.opacity = self.mll.getLayerOpacity(layerID)
            layer.enabled = self.mll.isLayerEnabled(layerID)
            
            layer.mask = self.mll.getLayerMask(layerID)
            layer.dqWeights = self.mll.getDualQuaternionWeights(layerID)
            
            for inflName, logicalIndex in self.mll.listLayerInfluences(layerID,activeInfluences=True):
                if inflName=='':
                    inflName = None
                influence = Influence()
                if inflName is not None:
                    influence.influenceName = self.getFullNodePath(inflName)
                influence.logicalIndex = logicalIndex
                layer.addInfluence(influence)
                
                influence.weights = self.mll.getInfluenceWeights(layerID, logicalIndex)
                
    def __validate(self):
        
        numVerts = self.mll.getVertCount()
        
        def validateVertCount(count,message):
                if count!=numVerts:
                    raise Exception(message) 
        
        for layer in self.layers:
            maskLen = len(layer.mask)
            if maskLen != 0:
                validateVertCount(maskLen, "Invalid vertex count for mask in layer '%s': expected size is %d" % (layer.name, numVerts))
            
            for influence in layer.influences:
                validateVertCount(len(influence.weights), "Invalid weights count for influence '%s' in layer '%s': expected size is %d" % (influence.influenceName, layer.name, numVerts))
                
                if self.skinClusterFn:
                    influence.logicalIndex = self.skinClusterFn.getLogicalInfluenceIndex(influence.influenceName)
                
        
    @Utils.undoable        
    def saveTo(self, mesh):
        '''
        saveTo(self,mesh)
        
        saves data to actual skin cluster
        '''
        
        # set target to whatever was provided
        self.mll.setCurrentMesh(mesh)

        if mesh==MllInterface.TARGET_REFERENCE_MESH:
            self.mll.setWeightsReferenceMesh(self.meshInfo.verts, self.meshInfo.triangles)
            
        
        if not self.mll.getLayersAvailable():
            self.mll.initLayers()
            
        if not self.mll.getLayersAvailable():
            raise Exception("could not initialize layers")
        

        # is skin cluster available?
        if mesh!=MllInterface.TARGET_REFERENCE_MESH:
            mesh, self.skinCluster = self.mll.getTargetInfo()
            self.skinClusterFn = SkinClusterFn()
            self.skinClusterFn.setSkinCluster(self.skinCluster)
        
        self.__validate()
        
        # set target to actual mesh
        self.mll.setCurrentMesh(mesh)
            
        with self.mll.batchUpdateContext():
            if self.mirrorInfluenceAssociationOverrides:
                self.mll.setManualMirrorInfluences(self.mirrorInfluenceAssociationOverrides)
            
            for layer in reversed(self.layers):
                layerId = self.mll.createLayer(name=layer.name, forceEmpty=True)
                self.mll.setCurrentLayer(layerId)
                if layerId is None:
                    raise Exception("import failed: could not create layer '%s'" % (layer.name))
                
                self.mll.setLayerOpacity(layerId, layer.opacity)
                self.mll.setLayerEnabled(layerId, layer.enabled)
                self.mll.setLayerMask(layerId, layer.mask)
                self.mll.setDualQuaternionWeights(layerId, layer.dqWeights)
                
                for influence in layer.influences:
                    self.mll.setInfluenceWeights(layerId, influence.logicalIndex, influence.weights)
        
                
    def __repr__(self):
        return "[LayerDataModel(%r)]" % self.layers
    
    def getAllInfluences(self):
        '''
        a convenience method to retrieve a list of names of all influences used in this layer data object
        '''
        
        result = set()
        
        for layer in self.layers:
            for influence in layer.influences:
                result.add(influence.influenceName)
                
        return tuple(result)
Пример #9
0
class VariousImportScenarios(AdditionalAsserts, unittest.TestCase):
    
    def setUp(self):
        self.targetMesh = 'testMesh' 
        self.numVerts = 169 # number of verts in our mesh
        self.mll = MllInterface()
        
        unittest.TestCase.setUp(self)
        openMayaFile('simplemirror.ma')
        cmds.hide("|y_axis")
        cmds.showHidden("|x_axis")
    
        self.mll.setCurrentMesh(self.targetMesh)
        self.mll.initLayers()
        self.mll.createLayer("original weights")
        
        self.model = ie.LayerData()
        self.layer = ie.Layer()
        self.layer.opacity = 1.0
        self.layer.enabled = True
        self.layer.mask = [0.0]*self.numVerts
        self.layer.mask[1] = 0.9 
        self.layer.mask[5] = 0.5566 
        self.layer.name = "imported layer"
        self.model.addLayer(self.layer)

        self.infl = ie.Influence()
        self.layer.addInfluence(self.infl)
        self.infl.influenceName = "x_axis|root|R_joint1"
        self.infl.logicalIndex = 666 # use nonsense value
        self.infl.weights = [0.1]*self.numVerts
        self.infl.weights[3] = 0.688
        self.infl.weights[4] = 0.345
        
        
    @decorators.insideMayaOnly
    def testSave(self):
        self.model.saveTo(self.targetMesh)
        
        layers = list(self.mll.listLayers())
        id,name = layers[0]
        self.assertEquals("imported layer", name)
        self.assertFloatArraysEqual(self.mll.getLayerMask(id), self.layer.mask)
        self.assertFloatArraysEqual(self.mll.getInfluenceWeights(id, self.infl.logicalIndex), self.infl.weights)
        
    @decorators.insideMayaOnly
    def testEmptyMask(self):
        self.layer.mask = []
        self.model.saveTo(self.targetMesh)
        

    @decorators.insideMayaOnly
    def testInvalidVertCount(self):
        self.infl.weights = self.infl.weights[:-1]
        with self.assertRaises(Exception,"Invalid weights count for influence 'x_axis|root|R_joint1' in layer 'imported layer': expected size is 169"):
            self.model.saveTo(self.targetMesh)
            
    @decorators.insideMayaOnly
    def testInvalidMaskCount(self):
        self.layer.mask = self.layer.mask[:-1]
        with self.assertRaises(Exception,"Invalid vertex count for mask in layer 'imported layer': expected size is 169"):
            self.model.saveTo(self.targetMesh)

    @decorators.insideMayaOnly
    def testInvalidMesh(self):
        with self.assertRaises(Exception,"could not initialize layers"):
            self.model.saveTo("persp")

    @decorators.insideMayaOnly
    def testUninitializedLayers(self):
        '''
        if layers on mesh were uninitialized, just initialize layers as usual, and do not fail
        '''
        LayerUtils.deleteCustomNodes()
        
        self.model.saveTo(self.targetMesh)

    @decorators.insideMayaOnly
    def testMissingInfluence(self):
        self.infl.influenceName = "something_is_missing_here"
        
        with self.assertRaises(MessageException,"Could not find influence 'something_is_missing_here' in skinCluster1"):
            self.model.saveTo(self.targetMesh)
class LayerData(object):
    '''
    Intermediate data object between ngSkinTools core and importers/exporters,
    representing all layers info in one skin cluster. 
    '''
    def __init__(self):
        #: layers list
        self.layers = []
        self.mll = MllInterface()

        # a map [sourceInfluenceName] -> [destinationInfluenceName]
        self.mirrorInfluenceAssociationOverrides = {}

    def addMirrorInfluenceAssociationOverride(self,
                                              sourceInfluence,
                                              destinationInfluence=None,
                                              selfReference=False,
                                              bidirectional=True):
        '''
        Adds mirror influence association override, similar to UI of "Add influences association".
        Self reference creates a source<->source association, bidirectional means that destination->source 
        link is added as well
        '''

        if selfReference:
            self.mirrorInfluenceAssociationOverrides[
                sourceInfluence] = sourceInfluence
            return

        if destinationInfluence is None:
            raise MessageException("destination influence must be specified")

        self.mirrorInfluenceAssociationOverrides[
            sourceInfluence] = destinationInfluence

        if bidirectional:
            self.mirrorInfluenceAssociationOverrides[
                destinationInfluence] = sourceInfluence

    def addLayer(self, layer):
        '''
        register new layer into this data object
        '''
        assert isinstance(layer, Layer)
        self.layers.append(layer)

    @staticmethod
    def getFullNodePath(nodeName):
        result = cmds.ls(nodeName, l=True)
        if result is None or len(result) == 0:
            raise MessageException("node %s was not found" % nodeName)

        return result[0]

    def loadFrom(self, mesh):
        '''
        loads data from actual skin cluster and prepares it for exporting.
        supply skin cluster or skinned mesh as an argument
        '''

        self.mll.setCurrentMesh(mesh)

        for layerID, layerName in self.mll.listLayers():
            self.mirrorInfluenceAssociationOverrides = self.mll.listManualMirrorInfluenceAssociations(
            )

            layer = Layer()
            layer.name = layerName
            self.addLayer(layer)

            layer.opacity = self.mll.getLayerOpacity(layerID)
            layer.enabled = self.mll.isLayerEnabled(layerID)

            layer.mask = self.mll.getLayerMask(layerID)

            for inflName, logicalIndex in self.mll.listLayerInfluences(
                    layerID):
                influence = Influence()
                influence.influenceName = self.getFullNodePath(inflName)
                influence.logicalIndex = logicalIndex
                layer.addInfluence(influence)

                influence.weights = self.mll.getInfluenceWeights(
                    layerID, logicalIndex)

    def __validate(self):
        numVerts = self.mll.getVertCount()
        for layer in reversed(self.layers):
            maskLen = len(layer.mask)
            if maskLen != 0 and maskLen != numVerts:
                raise Exception(
                    "Invalid vertex count for mask in layer '%s': expected size is %d"
                    % (layer.name, numVerts))

            for influence in layer.influences:
                weightsLen = len(influence.weights)
                if weightsLen != numVerts:
                    raise Exception(
                        "Invalid weights count for influence '%s' in layer '%s': expected size is %d"
                        % (influence.influenceName, layer.name, numVerts))

                influence.logicalIndex = self.skinClusterFn.getLogicalInfluenceIndex(
                    influence.influenceName)

    @Utils.undoable
    def saveTo(self, mesh):
        '''
        saves data to actual skin cluster
        '''

        # set target to whatever was provided
        self.mll.setCurrentMesh(mesh)

        if not self.mll.getLayersAvailable():
            self.mll.initLayers()

        if not self.mll.getLayersAvailable():
            raise Exception("could not initialize layers")

        mesh, self.skinCluster = self.mll.getTargetInfo()
        self.skinClusterFn = SkinClusterFn()
        self.skinClusterFn.setSkinCluster(self.skinCluster)

        self.__validate()

        # set target to actual mesh
        self.mll.setCurrentMesh(mesh)

        with self.mll.batchUpdateContext():
            for source, destination in self.mirrorInfluenceAssociationOverrides:
                self.mll.addManualMirrorInfluenceAssociation(
                    source, destination)

            for layer in reversed(self.layers):
                layerId = self.mll.createLayer(name=layer.name,
                                               forceEmpty=True)
                self.mll.setCurrentLayer(layerId)
                if layerId is None:
                    raise Exception(
                        "import failed: could not create layer '%s'" %
                        (layer.name))

                self.mll.setLayerOpacity(layerId, layer.opacity)
                self.mll.setLayerEnabled(layerId, layer.enabled)
                self.mll.setLayerMask(layerId, layer.mask)

                for influence in layer.influences:
                    self.mll.setInfluenceWeights(layerId,
                                                 influence.logicalIndex,
                                                 influence.weights)

    def __repr__(self):
        return "[LayerDataModel(%r)]" % self.layers

    def getAllInfluences(self):
        '''
        a convenience method to retrieve a list of names of all influences used in this layer data object
        '''

        result = set()

        for layer in self.layers:
            for influence in layer.influences:
                result.add(influence.influenceName)

        return tuple(result)
Пример #11
0
class LayerData(object):
    '''
    Intermediate data object between ngSkinTools core and importers/exporters,
    representing all layers info in one skin cluster. 
    '''
    
    def __init__(self):
        #: layers list
        self.layers = []
        self.mll = MllInterface()
        
        # a map [sourceInfluenceName] -> [destinationInfluenceName]
        self.mirrorInfluenceAssociationOverrides = {}
        
    def addMirrorInfluenceAssociationOverride(self,sourceInfluence,destinationInfluence=None,selfReference=False,bidirectional=True):
        '''
        Adds mirror influence association override, similar to UI of "Add influences association".
        Self reference creates a source<->source association, bidirectional means that destination->source 
        link is added as well
        '''
        
        if selfReference:
            self.mirrorInfluenceAssociationOverrides[sourceInfluence] = sourceInfluence
            return
        
        if destinationInfluence is None:
            raise MessageException("destination influence must be specified")
        
        self.mirrorInfluenceAssociationOverrides[sourceInfluence] = destinationInfluence
        
        if bidirectional:
            self.mirrorInfluenceAssociationOverrides[destinationInfluence] = sourceInfluence 
        

    def addLayer(self, layer):
        '''
        register new layer into this data object
        '''
        assert isinstance(layer, Layer)
        self.layers.append(layer)
       
    @staticmethod 
    def getFullNodePath(nodeName):
        result = cmds.ls(nodeName,l=True)
        if result is None or len(result)==0:
            raise MessageException("node %s was not found" % nodeName)
        
        return result[0]
        
    def loadFrom(self, mesh):
        '''
        loads data from actual skin cluster and prepares it for exporting.
        supply skin cluster or skinned mesh as an argument
        '''
        
        self.mll.setCurrentMesh(mesh)
        
        for layerID, layerName in self.mll.listLayers():
            self.mirrorInfluenceAssociationOverrides = self.mll.listManualMirrorInfluenceAssociations() 
            
            layer = Layer()
            layer.name = layerName
            self.addLayer(layer)
            
            
            layer.opacity = self.mll.getLayerOpacity(layerID)
            layer.enabled = self.mll.isLayerEnabled(layerID)
            
            layer.mask = self.mll.getLayerMask(layerID)
            
            for inflName, logicalIndex in self.mll.listLayerInfluences(layerID):
                influence = Influence()
                influence.influenceName = self.getFullNodePath(inflName)
                influence.logicalIndex = logicalIndex
                layer.addInfluence(influence)
                
                influence.weights = self.mll.getInfluenceWeights(layerID, logicalIndex)
                
    def __validate(self):
        numVerts = self.mll.getVertCount()
        for layer in reversed(self.layers):
            maskLen = len(layer.mask)
            if maskLen != 0 and maskLen != numVerts:
                raise Exception("Invalid vertex count for mask in layer '%s': expected size is %d" % (layer.name, numVerts))
            
            for influence in layer.influences:
                weightsLen = len(influence.weights) 
                if weightsLen != numVerts:
                    raise Exception("Invalid weights count for influence '%s' in layer '%s': expected size is %d" % (influence.influenceName, layer.name, numVerts))
                
                influence.logicalIndex = self.skinClusterFn.getLogicalInfluenceIndex(influence.influenceName)
                
        
    @Utils.undoable        
    def saveTo(self, mesh):
        '''
        saves data to actual skin cluster
        '''
        
        # set target to whatever was provided
        self.mll.setCurrentMesh(mesh)
        
        
        if not self.mll.getLayersAvailable():
            self.mll.initLayers()
            
        if not self.mll.getLayersAvailable():
            raise Exception("could not initialize layers")
        

        mesh, self.skinCluster = self.mll.getTargetInfo()
        self.skinClusterFn = SkinClusterFn()
        self.skinClusterFn.setSkinCluster(self.skinCluster)
        
        self.__validate()
        
        # set target to actual mesh
        self.mll.setCurrentMesh(mesh)
            
        with self.mll.batchUpdateContext():
            for source,destination in self.mirrorInfluenceAssociationOverrides.iteritems():
                self.mll.addManualMirrorInfluenceAssociation(source, destination)
            
            for layer in reversed(self.layers):
                layerId = self.mll.createLayer(name=layer.name, forceEmpty=True)
                self.mll.setCurrentLayer(layerId)
                if layerId is None:
                    raise Exception("import failed: could not create layer '%s'" % (layer.name))
                
                self.mll.setLayerOpacity(layerId, layer.opacity)
                self.mll.setLayerEnabled(layerId, layer.enabled)
                self.mll.setLayerMask(layerId, layer.mask)
                
                for influence in layer.influences:
                    self.mll.setInfluenceWeights(layerId, influence.logicalIndex, influence.weights)
        
                
    def __repr__(self):
        return "[LayerDataModel(%r)]" % self.layers
    
    def getAllInfluences(self):
        '''
        a convenience method to retrieve a list of names of all influences used in this layer data object
        '''
        
        result = set()
        
        for layer in self.layers:
            for influence in layer.influences:
                result.add(influence.influenceName)
                
        return tuple(result)
Пример #12
0
class VariousImportScenarios(AdditionalAsserts, unittest.TestCase):
    def setUp(self):
        self.targetMesh = 'testMesh'
        self.numVerts = 169  # number of verts in our mesh
        self.mll = MllInterface()

        unittest.TestCase.setUp(self)
        openMayaFile('simplemirror.ma')
        cmds.hide("|y_axis")
        cmds.showHidden("|x_axis")

        self.mll.setCurrentMesh(self.targetMesh)
        self.mll.initLayers()
        self.mll.createLayer("original weights")

        self.model = ie.LayerData()
        self.layer = ie.Layer()
        self.layer.opacity = 1.0
        self.layer.enabled = True
        self.layer.mask = [0.0] * self.numVerts
        self.layer.mask[1] = 0.9
        self.layer.mask[5] = 0.5566
        self.layer.name = "imported layer"
        self.model.addLayer(self.layer)

        self.infl = ie.Influence()
        self.layer.addInfluence(self.infl)
        self.infl.influenceName = "x_axis|root|R_joint1"
        self.infl.logicalIndex = 666  # use nonsense value
        self.infl.weights = [0.1] * self.numVerts
        self.infl.weights[3] = 0.688
        self.infl.weights[4] = 0.345

    @decorators.insideMayaOnly
    def testSave(self):
        self.model.saveTo(self.targetMesh)

        layers = list(self.mll.listLayers())
        id, name = layers[0]
        self.assertEquals("imported layer", name)
        self.assertFloatArraysEqual(self.mll.getLayerMask(id), self.layer.mask)
        self.assertFloatArraysEqual(
            self.mll.getInfluenceWeights(id, self.infl.logicalIndex),
            self.infl.weights)

    @decorators.insideMayaOnly
    def testEmptyMask(self):
        self.layer.mask = []
        self.model.saveTo(self.targetMesh)

    @decorators.insideMayaOnly
    def testInvalidVertCount(self):
        self.infl.weights = self.infl.weights[:-1]
        with self.assertRaises(
                Exception,
                "Invalid weights count for influence 'x_axis|root|R_joint1' in layer 'imported layer': expected size is 169"
        ):
            self.model.saveTo(self.targetMesh)

    @decorators.insideMayaOnly
    def testInvalidMaskCount(self):
        self.layer.mask = self.layer.mask[:-1]
        with self.assertRaises(
                Exception,
                "Invalid vertex count for mask in layer 'imported layer': expected size is 169"
        ):
            self.model.saveTo(self.targetMesh)

    @decorators.insideMayaOnly
    def testInvalidMesh(self):
        with self.assertRaises(Exception, "could not initialize layers"):
            self.model.saveTo("persp")

    @decorators.insideMayaOnly
    def testUninitializedLayers(self):
        '''
        if layers on mesh were uninitialized, just initialize layers as usual, and do not fail
        '''
        LayerUtils.deleteCustomNodes()

        self.model.saveTo(self.targetMesh)

    @decorators.insideMayaOnly
    def testMissingInfluence(self):
        self.infl.influenceName = "something_is_missing_here"

        with self.assertRaises(
                MessageException,
                "Could not find influence 'something_is_missing_here' in skinCluster1"
        ):
            self.model.saveTo(self.targetMesh)