def setAttributeByLabel(self, nodeName, attrLabel, value): """ Set value for attribute with given label on the node with given name. """ obj = commonUtils.nameToNode(nodeName) if self.model is None or obj is None: return node = self.model.findNode(obj) node.setAttributeByLabel(attrLabel, value)
def instance(): """Return the render setup singleton node, creating it if required.""" # If name-based lookup is a bottleneck, could have a render setup # cache, at the expense of render setup node lifescope management # complexity, since the cache should not prevent a render setup node # from being removed from the graph, nor should it be stale in such a # case. PPT, 5-May-2015. renderSetupObj = commonUtils.nameToNode(_RENDER_SETUP_NAME) if not renderSetupObj: # No renderSetup node, create one # Creation of render setup node singleton must not affect # undo stack, disable it for the creation only swf = cmds.undoInfo(query=True, stateWithoutFlush=True) try: cmds.undoInfo(stateWithoutFlush=False) renderSetupObj = _createInstance() finally: cmds.undoInfo(stateWithoutFlush=swf) fn = OpenMaya.MFnDependencyNode(renderSetupObj) # If renderSetup node isn't the proper type, blow up. if fn.typeId != RenderSetup.kTypeId: exceptionInfo = (_RENDER_SETUP_NAME, RenderSetup.kTypeName) raise TypeError(kRenderSetupNodeTypeMismatch % exceptionInfo) return fn.userNode()
def decode(self, encodedData): nodes = {} # Avoid creating several time the same node for (key, value) in encodedData.items(): (nodeName, attrName) = plug.Plug.getNames(key) node = None if nodeName in nodes: node = nodes[nodeName] else: try: node = commonUtils.nameToNode(nodeName) OpenMaya.MFnDependencyNode(node) except: node = None nodes[nodeName] = node if node is None: # No guarantee that the default node exist OpenMaya.MGlobal.displayWarning(kDefaultNodeMissing % nodeName) else: plg = plug.findPlug(nodeName, attrName) if plg is None: OpenMaya.MGlobal.displayWarning(kDefaultNodeAttrMissing % (nodeName, attrName)) else: plg.value = value
def longNamesToNamesDict(names): dict = {} for name in names: node = commonUtils.nameToNode(name) if node is None: raise RuntimeError(kInvalidNodeName % name) dict[commonUtils.nodeToLongName(node)] = name return dict
def hasInstance(): """ Return true if the render setup node exists """ # If name-based lookup is a bottleneck, could have a render setup # cache, at the expense of render setup node lifescope management # complexity, since the cache should not prevent a render setup node # from being removed from the graph, nor should it be stale in such a # case. PPT, 5-May-2015. return commonUtils.nameToNode(_RENDER_SETUP_NAME) is not None
def _targets(self, selectedNodeNames=None): '''Returns a generator of plug targets on which to create apply connection override nodes.''' # If we weren't given node names to find target for, get them from our # parent collection's selector. nodes = (commonUtils.nameToNode(name) for name in selectedNodeNames) \ if selectedNodeNames is not None else \ self.parent().getSelector().nodes() return (OpenMaya.MFnDependencyNode(o).findPlug('message', False) for o in nodes if o.hasFn(OpenMaya.MFn.kShadingEngine))
def _restoreOriginalPlug(self, original): # If the original shading engine was in a referenced file, the # connection to it may have been broken. To deal with this # case, we also stored the shading engine name as a string. originalStr = commonUtils.findPlug( self.thisMObject(), MaterialOverride.kShadingEngineNameLong) if originalStr is not None: originalNode = commonUtils.nameToNode(originalStr.asString()) if not originalNode.isNull(): fnEngine = OpenMaya.MFnDependencyNode(originalNode) enginePlug = fnEngine.findPlug('message', False) utils.connect(enginePlug, original)
def _preventDeletionFromSceneCleanupCB(nodeToBeDeleted, connectedNode, connection): # Prevent node deletion if the connected node is # any of the connection override nodes nodeObj = commonUtils.nameToNode(connectedNode) if nodeObj: fn = OpenMaya.MFnDependencyNode(nodeObj) return fn.typeId in [ typeIDs.applyConnectionOverride, typeIDs.connectionOverride, typeIDs.shaderOverride, typeIDs.materialOverride ] return False
def refresh(self, imposeRefresh): node = commonUtils.nameToNode(RS_DEFAULT_RENDER_GLOBALS) self.startFrame = commonUtils.findPlug(node, RS_START_FRAME).asMTime().value self.endFrame = commonUtils.findPlug(node, RS_END_FRAME).asMTime().value self.animation = commonUtils.findPlug(node, RS_ANIMATION).asBool() if self.callback is 0: self.callback = OpenMaya.MNodeMessage.addAttributeChangedCallback( node, self.defaultRenderGlobalsChanged, None) if imposeRefresh: self.emitDataChanged()
def encode(self): attrs = {} for name in self.getNodes(): node = commonUtils.nameToNode(name) nodeFn = None try: nodeFn = OpenMaya.MFnDependencyNode(node) except: # No guarantee that the default node exist OpenMaya.MGlobal.displayWarning(kDefaultNodeMissing % name) else: for attrIdx in xrange(nodeFn.attributeCount()): plg = plug.Plug(node, nodeFn.attribute(attrIdx)) if plg.type is not plug.Plug.kInvalid and plg.name not in self._plugsToIgnore: attrs[plg.name] = plg.value return attrs
def _afterOpenCB(self, clientData): """If file references were loaded during file open in a visible render layer, refresh that layer.""" # Render layer membership is stored as a connection from the member # node to the legacy render layer node. Similarly, applied # overrides are connected to the attribute they override. For # scenes without file references, these connections persist in the # saved file. # # For scenes with file references, these connections are NOT saved # with the file: as per the file referencing architecture, they are # supposed to be connection reference edits stored with the file. # # However, render setup blocks all reference edits during render # setup operations, as the information they contain is largely # redundant with the render setup procedures themselves (e.g. render # layer membership, applied overrides). Therefore, re-apply the # layer if loaded file references are present in the scene. layer = self.getVisibleRenderLayer() # Are we in a layer that isn't the default? if layer != self._defaultRenderLayer: # Are there any file reference nodes in our scene? refNodeNames = cmds.ls(type='reference') if refNodeNames: # Are there any loaded file reference nodes in our scene? refNodes = (commonUtils.nameToNode(r) for r in refNodeNames) loadedRef = next( (r for r in refNodes if OpenMaya.MFnReference(r).isLoaded()), None) if loadedRef: # backward comp after refactoring overrideManager, see overrideManager.py for details if layer._backwardCompID is not None: layer._transferAttributes() # Unapply / reapply overrides. layer.needsApplyUpdate = True self.switchToLayer(layer)
def nameToUserNode(name): node = commonUtils.nameToNode(name) return OpenMaya.MFnDependencyNode(node).userNode() if node else None
def getLongName(name): node = commonUtils.nameToNode(name) if node is not None: return commonUtils.nodeToLongName(node) return None
def renderSetupFind(objectNodeNames, renderLayerNames, includeLayers): nodes = ( commonUtils.nameToNode(name) for name in objectNodeNames ) longNames = [ commonUtils.nodeToLongName(node) for node in nodes if node is not None ] renderLayers = (utils.nameToUserNode(renderLayerName) for renderLayerName in renderLayerNames) models = itertools.chain.from_iterable(rl.findIn(longNames, includeSelf=includeLayers) for rl in renderLayers) return [model.name() for model in models]