Пример #1
0
    def testDefectXmlImportIssue(self):
        openMayaFile("defect-xml-import-fails/mirror_association_issues.ma")

        importer = XmlImporter()
        data = importer.process(getTestFileContents("defect-xml-import-fails/mirror_weighting_manual_layers.xml"))
        
        
        mll = MllInterface()
        mll.setCurrentMesh("pSphere1")
        
        
        overrides = mll.listManualMirrorInfluenceAssociations()
        self.assertEqual(len(overrides), 0)
        
        data.saveTo("pSphere1")
        
        overrides = mll.listManualMirrorInfluenceAssociations()
        self.assertEqual(len(overrides), 1)
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)
class MllInterfaceTest(AdditionalAsserts, unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.mll = MllInterface()

    @insideMayaOnly
    def testAccessForNoSelection(self):
        testUtils.openMayaFile("normalization.ma")
        cmds.select(cl=True)
        self.mll.setCurrentMesh(None)

        self.assertEquals(None, self.mll.getTargetInfo())

    @insideMayaOnly
    def testInvalidInfluenceIndex(self):
        testUtils.openMayaFile("normalization.ma")
        self.mll.setCurrentMesh('testMesh')
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights")

        weights = [0.0] * self.mll.getVertCount()

        with self.assertRaises(
                errorMessage="skin cluster does not have logical influence 666"
        ):
            self.mll.setInfluenceWeights(layerId, 666, weights)

    @insideMayaOnly
    def testAccess(self):
        # test the same for specified mesh, or None for current selection
        for mesh in ('testMesh', None):
            testUtils.openMayaFile("normalization.ma")
            self.mll.setCurrentMesh(mesh)
            if mesh is None:
                cmds.select('testMesh')

            self.mll.initLayers()
            layerId = self.mll.createLayer("initial weights")
            self.assertNotEqual(None, layerId)

            self.assertEqual("initial weights", self.mll.getLayerName(layerId))

            self.assertEqual(16, self.mll.getVertCount(),
                             "Invalid vertex count detected")

            self.assertEqual(1, self.mll.getLayerOpacity(layerId))
            self.assertEqual(True, self.mll.isLayerEnabled(layerId))
            self.assertEqual([], self.mll.getLayerMask(layerId))
            self.assertFloatArraysEqual(
                self.mll.getInfluenceWeights(layerId, 1), [
                    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0,
                    1.0, 1.0, 1.0, 1.0
                ])

    @insideMayaOnly
    def testOverwriteWeightsCrashesMaya(self):
        testUtils.openMayaFile("genericSkinnedMesh.mb")
        self.mll.setCurrentMesh("mesh|pPlane1")
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights", forceEmpty=True)
        weights = [1.0] * self.mll.getVertCount()
        weights[0] = 0.7
        weights[1] = 0.5
        weights[2] = 0.3

        self.mll.setInfluenceWeights(layerId, 0, weights)
        self.assertFloatArraysEqual(
            self.mll.getInfluenceWeights(layerId, 0)[0:3], [0.7, 0.5, 0.3])

        self.mll.setInfluenceWeights(layerId, 1, weights)
        self.assertFloatArraysEqual(weights,
                                    self.mll.getInfluenceWeights(layerId, 1))

        # previous influence should have some of it's value substracted
        self.assertFloatArraysEqual(
            self.mll.getInfluenceWeights(layerId, 0)[0:3], [0.3, 0.5, 0.3])

    @insideMayaOnly
    def testAddManualMirrorInfluences(self):
        testUtils.openMayaFile("genericSkinnedMesh.mb")
        self.mll.setCurrentMesh("mesh|pPlane1")
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights")

        result = self.mll.listManualMirrorInfluenceAssociations()
        self.assertArraysEqual(result, [])

        self.mll.addManualMirrorInfluenceAssociation("L_joint1", "R_joint1")
        self.mll.addManualMirrorInfluenceAssociation("R_joint1", "L_joint1")

        result = self.mll.listManualMirrorInfluenceAssociations()
        self.assertDictionariesEqual(result, {
            "L_joint1": "R_joint1",
            "R_joint1": "L_joint1"
        })

    @insideMayaOnly
    def testSetMaxInfluencePerVertex(self):
        testUtils.openMayaFile("genericSkinnedMesh.mb")
        self.mll.setCurrentMesh("mesh|pPlane1")
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights")

        self.assertEqual(self.mll.getInfluenceLimitPerVertex(), 0,
                         "initial limit per vertex should be zero")

        self.mll.setInfluenceLimitPerVertex(10)
        self.assertEqual(self.mll.getInfluenceLimitPerVertex(), 10,
                         "was not able to set limit to the desired value")

        self.mll.setInfluenceLimitPerVertex(2)
        self.assertEqual(self.mll.getInfluenceLimitPerVertex(), 2,
                         "was not able to set limit to the desired value")
Пример #4
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)
Пример #5
0
class MllInterfaceTest(AdditionalAsserts, unittest.TestCase):
    
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.mll = MllInterface()
        

    @insideMayaOnly
    def testAccessForNoSelection(self):
        testUtils.openMayaFile("normalization.ma")
        cmds.select(cl=True)
        self.mll.setCurrentMesh(None)
        
        self.assertEquals(None,self.mll.getTargetInfo())

    @insideMayaOnly
    def testInvalidInfluenceIndex(self):
        testUtils.openMayaFile("normalization.ma")
        self.mll.setCurrentMesh('testMesh')
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights")
        
        weights = [0.0]*self.mll.getVertCount()
        
        with self.assertRaises(errorMessage="skin cluster does not have logical influence 666"):
            self.mll.setInfluenceWeights(layerId, 666, weights)
            
    
    @insideMayaOnly
    def testAccess(self):
        # test the same for specified mesh, or None for current selection
        for mesh in ('testMesh',None):
            testUtils.openMayaFile("normalization.ma")
            self.mll.setCurrentMesh(mesh)
            if mesh is None:
                cmds.select('testMesh')
                            
            self.mll.initLayers()
            layerId = self.mll.createLayer("initial weights")
            self.assertNotEqual(None, layerId)
            
            self.assertEqual("initial weights",self.mll.getLayerName(layerId))
            
            self.assertEqual(16, self.mll.getVertCount(), "Invalid vertex count detected")
            
            self.assertEqual(1, self.mll.getLayerOpacity(layerId))
            self.assertEqual(True, self.mll.isLayerEnabled(layerId))
            self.assertEqual([], self.mll.getLayerMask(layerId))
            self.assertFloatArraysEqual(self.mll.getInfluenceWeights(layerId,1),[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
            
    @insideMayaOnly
    def testOverwriteWeightsCrashesMaya(self):
        testUtils.openMayaFile("genericSkinnedMesh.mb")
        self.mll.setCurrentMesh("mesh|pPlane1")
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights",forceEmpty=True)
        weights = [1.0]*self.mll.getVertCount()
        weights[0] = 0.7
        weights[1] = 0.5
        weights[2] = 0.3
        
        self.mll.setInfluenceWeights(layerId, 0, weights)
        self.assertFloatArraysEqual(self.mll.getInfluenceWeights(layerId, 0)[0:3], [0.7,0.5,0.3])

        self.mll.setInfluenceWeights(layerId, 1, weights)
        self.assertFloatArraysEqual(weights,self.mll.getInfluenceWeights(layerId, 1))
        
        # previous influence should have some of it's value substracted
        self.assertFloatArraysEqual(self.mll.getInfluenceWeights(layerId, 0)[0:3], [0.3,0.5,0.3])
        
        
        
    @insideMayaOnly
    def testAddManualMirrorInfluences(self):
        testUtils.openMayaFile("genericSkinnedMesh.mb")
        self.mll.setCurrentMesh("mesh|pPlane1")
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights")
        
        result = self.mll.listManualMirrorInfluenceAssociations()
        self.assertArraysEqual(result, [])
        
        self.mll.addManualMirrorInfluenceAssociation("L_joint1", "R_joint1")
        self.mll.addManualMirrorInfluenceAssociation("R_joint1", "L_joint1")
        
        result = self.mll.listManualMirrorInfluenceAssociations()
        self.assertDictionariesEqual(result, {"L_joint1":"R_joint1","R_joint1":"L_joint1"})
        

    @insideMayaOnly
    def testSetMaxInfluencePerVertex(self):
        testUtils.openMayaFile("genericSkinnedMesh.mb")
        self.mll.setCurrentMesh("mesh|pPlane1")
        self.mll.initLayers()
        layerId = self.mll.createLayer("initial weights")
        
        self.assertEqual(self.mll.getInfluenceLimitPerVertex(),0,"initial limit per vertex should be zero")

        self.mll.setInfluenceLimitPerVertex(10)
        self.assertEqual(self.mll.getInfluenceLimitPerVertex(),10,"was not able to set limit to the desired value")

        self.mll.setInfluenceLimitPerVertex(2)
        self.assertEqual(self.mll.getInfluenceLimitPerVertex(),2,"was not able to set limit to the desired value")