def filterSceneByAttributeValues(attributeNames, filter): """From the all scene zoo meta nodes find all attributeNames on the node if the value of the attribute is a string the filter acts as a regex otherwise it'll will do a value == filter op. :param attributeNames: a list of attribute name to find on each node :type attributeNames: seq(str) :param filter: filters the found attributes by value :type filter: any maya datatype :return: A seq of plugs :rtype: seq(MPlug) """ for meta in iterSceneMetaNodes(): dep = om2.MFnDependencyNode(meta) for attr in attributeNames: try: plug = dep.findPlug(attr, False) except RuntimeError: continue value = plugs.getPlugValue(plug) if isinstance(value, basestring): grp = re.search(filter, value) if grp: yield plug elif value == filter: yield plug
def getParentInverseMatrix(mobject): """Returns the parent inverse matrix from the Mobject :param mobject: MObject :return: MMatrix """ return plugs.getPlugValue(parentInverseMatrixPlug(mobject))
def getWorldMatrix(mobject): """Returns the worldMatrix value as an MMatrix. :param mobject: MObject, the MObject that points the dagNode :return: MMatrix """ return plugs.getPlugValue(worldMatrixPlug(mobject))
def getMatrix(mobject): """ Returns the MMatrix of the given mobject :param mobject: MObject :return:MMatrix """ return plugs.getPlugValue(om2.MFnDependencyNode(mobject).findPlug("matrix", False))
def test_setRotationOrder(self): con = control.Control(name="testControl") con.create(shape="arrow") con.setRotationOrder(om2.MTransformationMatrix.kXZY) self.assertEqual( plugs.getPlugValue( om2.MFnDependencyNode(con.mobject()).findPlug( "rotateOrder", False)), om2.MTransformationMatrix.kXZY)
def setNodeColour(node, colour): """Set the given node mobject override color can be a mobject representing a transform or shape :param node: the node which you want to change the override colour of :type node: mobject :param colour: The RGB colour to set :type colour: MColor or tuple """ dependNode = om2.MFnDagNode(om2.MFnDagNode(node).getPath()) plug = dependNode.findPlug("overrideColorRGB", False) enabledPlug = dependNode.findPlug("overrideEnabled", False) overrideRGBColors = dependNode.findPlug("overrideRGBColors", False) if not plugs.getPlugValue(enabledPlug): plugs.setPlugValue(enabledPlug, True) if not plugs.getPlugValue(overrideRGBColors): plugs.setPlugValue(dependNode.findPlug("overrideRGBColors", False), True) plugs.setPlugValue(plug, colour)
def getParentMatrix(mobject): """Returns the parent matrix of the given MObject :param mobject: MObject :return: MMatrix """ wm = om2.MFnDependencyNode(mobject).findPlug("parentMatrix", False) wm.evaluateNumElements() matplug = wm.elementByPhysicalIndex(0) return plugs.getPlugValue(matplug)
def getNodeColourData(node): """ :param node: The maya node mobject that you want to get the override colour from :type node: MObject :return: {"overrideEnabled": bool, "overrideColorRGB": plugs.getAttr(plug), "overrideRGBColors": plugs.getAttr(overrideRGBColors)} :rtype: dict """ dependNode = om2.MFnDagNode(om2.MFnDagNode(node).getPath()) plug = dependNode.findPlug("overrideColorRGB", False) enabledPlug = dependNode.findPlug("overrideEnabled", False) overrideRGBColors = dependNode.findPlug("overrideRGBColors", False) return {"overrideEnabled": plugs.getPlugValue(enabledPlug), "overrideColorRGB": plugs.getPlugValue(plug), "overrideRGBColors": plugs.getPlugValue(overrideRGBColors)}
def mirrorJoint(node, parent, translate, rotate): nFn = om2.MFnDependencyNode(node) rotateOrder = nFn.findPlug("rotateOrder", False).asInt() transMatRotateOrder = generic.intToMTransformRotationOrder(rotateOrder) translation, rotMatrix = mirrorTransform(node, parent, translate, rotate) # MVector, MMatrix jointOrder = om2.MEulerRotation(plugs.getPlugValue(nFn.findPlug("jointOrient", False))) # deal with joint orient jo = om2.MTransformationMatrix().setRotation(jointOrder).asMatrixInverse() # applyRotation and translation rot = mayamath.toEulerFactory(rotMatrix * jo, transMatRotateOrder) setRotation(node, rot) setTranslation(node, translation)
def serialize(self): data = {} for plug in self.iterAttributes(): attrData = {"name": plug.name(), "type": plug.attribute().apiTypeStr, "value": plugs.getPlugValue(plug)} connections = [] if plug.isSource: for connection in plug.connectedTo(False, True): connections.append((nodes.nameFromMObject(connection.node()), connection.name())) attrData["connections"] = connections data.update(attrData) return data
def findChildrenByFilter(self, filter, plugName=None, depthLimit=256): children = [] for child in self.iterMetaChildren(depthLimit): if not plugName: grp = re.search(filter, nodes.nameFromMObject(child)) else: try: plug = child._mfn.findPlug(plugName, False) grp = re.search(filter, plugs.getPlugValue(plug)) except RuntimeError: continue if grp: children.append(child) return children
def create(self, driver, driven, skipScale=None, skipRotate=None, skipTranslate=None, maintainOffset=False, space=om2.MFn.kWorld): composename = "_".join([self.name, "wMtxCompose"]) multMatrix = None if maintainOffset: offset = nodes.getOffsetMatrix(driver, driven) offsetname = "_".join([self.name, "wMtxOffset"]) parentInverse = nodes.parentInverseMatrixPlug( driven) if self.dynamic else plugs.getPlugValue( nodes.parentInverseMatrixPlug(driven)) multMatrix = creation.createMultMatrix( offsetname, inputs=(offset, nodes.worldMatrixPlug(driver), parentInverse), output=None) outputPlug = om2.MFnDependencyNode(multMatrix).findPlug( "matrixSum", False) else: outputPlug = nodes.worldMatrixPlug(driver) decompose = creation.createDecompose(composename, destination=driven, translateValues=skipTranslate, scaleValues=skipScale, rotationValues=skipRotate) decomposeFn = om2.MFnDependencyNode(decompose) plugs.connectPlugs(outputPlug, decomposeFn.findPlug("inputMatrix", False)) self.node = om2.MObjectHandle(decompose) mapping = dict(skipScale=skipScale, skipRotate=skipRotate, skipTranslate=skipTranslate, maintainOffset=maintainOffset) kwargsMap = json.dumps(mapping) addConstraintMap(driver, (driven, ), (decompose, multMatrix), kwargsMap=kwargsMap) return decompose, multMatrix
def addTarget(self, driver): """Adds the given driver transform to the constraint :param driver: The driver mobject transform :type driver: MObject @note having to use maya commands here due to api not able to resize the plugs array outside the datablock """ driven = self.drivenObject() driverName = nodes.nameFromMObject(driver) # so we have the fullPath driverShortName = om2.MNamespace.stripNamespaceFromName(driverName).split("|")[-1] nextWeightIndex = self.numTargets() # starts at zero so the return is the next element drivenFn = om2.MFnDependencyNode(driven) offsetMatrix = om2.MTransformationMatrix(nodes.getOffsetMatrix(driver, driven)) translation = offsetMatrix.translation(om2.MSpace.kTransform) rotation = generic.eulerToDegrees( offsetMatrix.rotation().reorder(plugs.getPlugValue(drivenFn.findPlug("rotateOrder", False)))) # create the weight attribute weightName = "W".join([driverShortName, str(nextWeightIndex)]) weightAttr = nodes.addAttribute(self.node.object(), weightName, weightName, attrType=attrtypes.kMFnNumericDouble) weightAttr.setMin(0.0) weightAttr.setMax(1.0) weightAttr.default = 1.0 weightAttr.keyable = True driverFn = om2.MFnDependencyNode(driver) targetPlug = self.mfn.findPlug("target", False).elementByLogicalIndex(nextWeightIndex) cmds.connectAttr(driverFn.findPlug("parentMatrix", False).elementByPhysicalIndex(0).name(), targetPlug.child(0).name()) # targetParentMatrix cmds.connectAttr(driverFn.findPlug("scale", False).name(), targetPlug.child(13).name()) # targetScale cmds.connectAttr(driverFn.findPlug("rotateOrder", False).name(), targetPlug.child(8).name()) # targetRotateOrder cmds.connectAttr(driverFn.findPlug("rotate", False).name(), targetPlug.child(7).name()) # targetRotate cmds.connectAttr(driverFn.findPlug("rotatePivotTranslate", False).name(), targetPlug.child(5).name()) # targetRotateTranslate cmds.connectAttr(driverFn.findPlug("rotatePivot", False).name(), targetPlug.child(4).name()) # targetRotatePivot cmds.connectAttr(driverFn.findPlug("translate", False).name(), targetPlug.child(3).name()) # targetTranslate cmds.connectAttr(om2.MPlug(self.mfn.object(), weightAttr.object()).name(), targetPlug.child(1).name()) # targetWeight # setting offset value plugs.setPlugValue(targetPlug.child(6), translation) # targetOffsetTranslate plugs.setPlugValue(targetPlug.child(10), rotation) # targetOffsetRotate