Example #1
0
 def setAttribute(self, attr, value):
     if isinstance(attr, om2.MPlug):
         with plugs.setLockedContext(attr):
             plugs.setPlugValue(attr, value)
         return
     if self.hasAttribute(attr):
         plug = self._mfn.findPlug(attr, False)
         with plugs.setLockedContext(plug):
             plugs.setPlugValue(plug, value)
Example #2
0
def exportContext(rootNode):
    changed = []
    for i in nodes.iterChildren(rootNode, recursive=True):
        dp = om2.MFnDependencyNode(i)
        plug = dp.findPlug("visibility", False)
        with plugs.setLockedContext(plug):
            if plug.asFloat() != 1.0:
                plugs.setPlugValue(plug, 1.0)
                changed.append(dp)
    yield
    for i in iter(changed):
        plug = i.findPlug("visibility", False)
        with plugs.setLockedContext(plug):
            plugs.setPlugValue(plug, 0.0)
Example #3
0
    def connectToByPlug(self, sourcePlug, node, nodeAttributeName=None):
        nodeAttributeName = nodeAttributeName or "metaNode"
        dep = om2.MFnDependencyNode(node)
        if not dep.hasAttribute(nodeAttributeName):
            destinationPlug = dep.findPlug(
                nodes.addAttribute(node, nodeAttributeName, nodeAttributeName,
                                   attrtypes.kMFnMessageAttribute).object(),
                False)
        else:
            destinationPlug = dep.findPlug(nodeAttributeName, False)
            plugs.disconnectPlug(destinationPlug)

        with plugs.setLockedContext(sourcePlug):
            destIsLock = False
            sourceIsLock = False
            if destinationPlug.isLocked:
                destinationPlug.isLocked = False
                destIsLock = True
            if sourcePlug.isLocked:
                sourcePlug.isLocked = False
                sourceIsLock = True
            plugs.connectPlugs(sourcePlug, destinationPlug)
            if sourceIsLock:
                sourcePlug.isLocked = True
            if destIsLock:
                destinationPlug.isLocked = True
        return destinationPlug
Example #4
0
 def addParent(self, parent):
     """Sets the parent meta node for this node, removes the previous parent if its attached
     
     :param parent: The meta node to add as the parent of this meta node 
     :type parent: MetaBase
     """
     parentPlug = self._mfn.findPlug(MPARENT_ATTR_NAME, False)
     nextElement = plugs.nextAvailableDestElementPlug(parentPlug)
     with plugs.setLockedContext(parentPlug):
         plugs.connectPlugs(parent.findPlug(MCHILDREN_ATTR_NAME, False), nextElement)
Example #5
0
 def renameAttribute(self, name, newName):
     try:
         plug = self._mfn.findPlug(name, False)
     except RuntimeError:
         raise AttributeError("No attribute named {} on metaNode->{}".format(name, self.fullPathName()))
     with plugs.setLockedContext(plug):
         mod = om2.MDGModifier()
         mod.renameAttribute(self.mobject(), plug.attribute(), newName, newName)
         mod.doIt()
     return True
Example #6
0
 def removeAllParents(self):
     parentPlug = self._mfn.findPlug(MPARENT_ATTR_NAME, False)
     mod = om2.MDGModifier()
     with plugs.setLockedContext(parentPlug):
         for index in iter(parentPlug.getExistingArrayAttributeIndices()):
             childrenElement = parentPlug.elementByLogicalIndex(index)
             if childrenElement.isConnected:
                 mod.disconnect(childrenElement.source(), childrenElement)
                 mod.removeMultiInstance(childrenElement, False)
     mod.doIt()
     return True
Example #7
0
 def removeParent(self):
     parent = self.metaParent()
     if parent is None:
         return False
     mod = om2.MDGModifier()
     source = parent.findPlug(MCHILDREN_ATTR_NAME, False)
     destination = self.findPlug(MPARENT_ATTR_NAME, False)
     with plugs.setLockedContext(source):
         destination.isLocked = False
         mod.disconnect(source, destination)
         mod.doIt()
     return True
Example #8
0
 def _deserializeConnections(self, connections):
     # plugList = om2.MSelectionList()
     for conn in connections:
         sourceNode = conn["source"]
         destinationNode = conn["destination"]
         if sourceNode is None or destinationNode is None:
             continue
         # connect the source and destination plug
         try:
             sourcePlug = plugs.asMPlug(conn["sourcePlug"])
             destinationPlug = plugs.asMPlug(conn["destinationPlug"])
             if destinationPlug.isDestination:
                 continue
             with contextlib.nested(
                     plugs.setLockedContext(sourcePlug),
                     plugs.setLockedContext(destinationPlug)):
                 plugs.connectPlugs(sourcePlug,
                                    destinationPlug,
                                    force=False)
         except RuntimeError:
             continue
Example #9
0
 def addParent(self, parent):
     """Sets the parent meta node for this node, removes the previous parent if its attached
     
     :param parent: The meta node to add as the parent of this meta node 
     :type parent: MetaBase
     :todo: change the method name to setParent since we should only allow one parent
     """
     metaParent = self.metaParent()
     if metaParent is not None or metaParent == parent:
         self.removeParent()
     parentPlug = self._mfn.findPlug(MPARENT_ATTR_NAME, False)
     with plugs.setLockedContext(parentPlug):
         plugs.connectPlugs(parent.findPlug(MCHILDREN_ATTR_NAME, False),
                            parentPlug)
Example #10
0
def updateCommandString(node, value, commandType=None):
    """Function to update the node attributes related to the trigger data

    :param node: The node with the trigger attributes to update
    :type node: om2.MObject
    :param value:
    :type value:
    :param commandType: the command type, see :func:`createTriggerAttributes` for more information
    :type commandType: int
    :return: True if successful
    :rtype: bool
    """
    fn = om2.MFnDependencyNode(node)
    if not fn.hasAttribute(TRIGGER_ATTR_NAME):
        return False
    comp = fn.findPlug(TRIGGER_ATTR_NAME, False)
    commandStr = comp.child(1)
    with plugs.setLockedContext(commandStr):
        commandStr.setString(value)
    if commandType is not None:
        commandTypeP = comp.child(0)
        with plugs.setLockedContext(commandTypeP):
            commandTypeP.setString(commandType)
    return True
Example #11
0
 def removeParent(self, parent=None):
     """
     :param parent: The meta class to remove, if set to None then all parents will be removed
     :type parent: :class:`MetaBase` or None
     :rtype: bool
     """
     parentPlug = self._mfn.findPlug(MPARENT_ATTR_NAME, False)
     mod = om2.MDGModifier()
     with plugs.setLockedContext(parentPlug):
         for index in iter(parentPlug.getExistingArrayAttributeIndices()):
             childrenElement = parentPlug.elementByLogicalIndex(index)
             if childrenElement.isConnected:
                 mb = MetaBase(childrenElement.source().node())
                 if parent is None or mb == parent:
                     mod.disconnect(childrenElement.source(), childrenElement)
                     mod.removeMultiInstance(childrenElement, False)
     mod.doIt()
     return True
Example #12
0
    def removeChild(self, node):
        """Removes the meta child from this node which should currently be the meta parent

        :param node:
        :type node: MObject or MetaBase instance
        :return: True if removed
        :rtype: bool
        """
        if isinstance(node, MetaBase):
            node.removeParent()
            return True
        childPlug = self._mfn.findPlug(MCHILDREN_ATTR_NAME, False)
        mod = om2.MDGModifier()
        destination = om2.MFnDependencyNode(node).findPlug(
            MPARENT_ATTR_NAME, False)
        with plugs.setLockedContext(childPlug):
            destination.isLocked = False
        mod.disconnect(childPlug, destination)
        mod.doIt()
        return True
Example #13
0
    def connectTo(self, attributeName, node, nodeAttributeName=None):
        """Connects one plug to another by attribute name

        :param attributeName: the meta attribute name to connect from, if it doesn't exist it will be created
        :type attributeName: str
        :param node: the destination node
        :type node: MObject
        :param nodeAttributeName: the destination node attribute name, if one doesn't exist one will be created
        :type nodeAttributeName: str
        :return: the destination plug
        :rtype: om2.MPlug
        """
        nodeAttributeName = nodeAttributeName or "metaNode"
        dep = om2.MFnDependencyNode(node)
        self.disconnectFromNode(node)
        if not dep.hasAttribute(nodeAttributeName):
            destinationPlug = dep.findPlug(
                nodes.addAttribute(node, nodeAttributeName, nodeAttributeName,
                                   attrtypes.kMFnMessageAttribute).object(),
                False)
        else:
            destinationPlug = dep.findPlug(nodeAttributeName, False)
            plugs.disconnectPlug(destinationPlug)

        if self._mfn.hasAttribute(attributeName):
            # we should have been disconnected from the destination control above
            sourcePlug = self._mfn.findPlug(attributeName, False)
        else:
            newAttr = self.addAttribute(attributeName, None,
                                        attrtypes.kMFnMessageAttribute)
            if newAttr is not None:
                sourcePlug = newAttr
            else:
                sourcePlug = self._mfn.findPlug(attributeName, False)
        with plugs.setLockedContext(sourcePlug):
            if destinationPlug.isLocked:
                destinationPlug.isLocked = False
            plugs.connectPlugs(sourcePlug, destinationPlug)
            destinationPlug.isLocked = True
        return destinationPlug
Example #14
0
 def test_isLockedContext(self):
     p = plugs.asMPlug(self.node + ".translate")
     p.isLocked = True
     with plugs.setLockedContext(p):
         self.assertFalse(p.isLocked)
     self.assertTrue(p.isLocked)