def test_childPaths(self): nodeParent = nodes.asMObject(cmds.group(self.node)) child1 = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(child1, nodeParent) dagPath = om.MFnDagNode(nodeParent).getPath() self.assertEquals(len(nodes.childPaths(dagPath)), 2) self.assertTrue(all(isinstance(i, om.MDagPath) for i in nodes.childPaths(dagPath)))
def test_nameFromMObject(self): self.assertEquals( nodes.nameFromMObject(nodes.asMObject(self.node), partialName=False), "|transform1") self.assertEquals( nodes.nameFromMObject(nodes.asMObject(self.node), partialName=True), "transform1")
def test_childPathAtIndex(self): nodeParent = nodes.asMObject(cmds.group(self.node)) child1 = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(child1, nodeParent) dagPath = om.MFnDagNode(nodeParent).getPath() self.assertEquals( nodes.childPathAtIndex(dagPath, 0).partialPathName(), self.node)
def test_getParent(self): obj = nodes.asMObject(self.node) self.assertFalse(nodes.hasParent(obj)) transform = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(transform, obj) parent = nodes.getParent(transform) self.assertEquals(parent, obj)
def test_childPathsByFn(self): nodeParent = nodes.asMObject(cmds.polyCube(ch=False)[0]) nodes.setParent(nodes.asMObject(self.node), nodeParent) child1 = nodes.asMObject(cmds.createNode("transform")) child2 = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(child1, nodeParent) nodes.setParent(child2, nodeParent) dagPath = om.MFnDagNode(nodeParent).getPath() results = nodes.childPathsByFn(dagPath, om2.MFn.kTransform) self.assertEquals(len(results), 3) results = nodes.childPathsByFn(dagPath, om2.MFn.kMesh) self.assertEquals(len(results), 1)
def test_getChildren(self): parent = nodes.asMObject(cmds.group(self.node)) children = nodes.getChildren(parent) self.assertEquals(len(children), 1) self.assertIsInstance(children[0], om.MObject) secondChild = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(secondChild, parent) children = nodes.getChildren(parent) self.assertEquals(len(children), 2) thirdChild = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(thirdChild, parent) children = nodes.getChildren(parent, recursive=True) self.assertEquals(len(children), 3)
def test_getTranslation(self): node = nodes.asMObject(self.node) om2.MFnTransform(node).setTranslation(om2.MVector(10, 10, 10), om2.MSpace.kObject) translation = nodes.getTranslation(node, om2.MSpace.kObject) self.assertIsInstance(translation, om2.MVector) self.assertEquals(translation, om2.MVector(10, 10, 10))
def test_getObjectMatrix(self): node = nodes.asMObject(self.node) matrix = nodes.getMatrix(node) self.assertIsInstance(matrix, om2.MMatrix) matPl = om2.MFnDagNode(node).findPlug("matrix", False) self.assertEquals(matrix, om2.MFnMatrixData(matPl.asMObject()).matrix())
def test_shapeSaveToLibrary(self): circle = cmds.circle(ch=False)[0] self.data, self.shapePath = shapelib.saveToLib(nodes.asMObject(circle), "circleTest") data = filesystem.loadJson(self.shapePath) for shapeName, shapeData in iter(data.items()): self.assertTrue("cvs" in shapeData) self.assertTrue("degree" in shapeData) self.assertTrue("form" in shapeData) self.assertTrue("knots" in shapeData) self.assertTrue("overrideColorRGB" in shapeData) self.assertTrue("overrideEnabled" in shapeData) self.assertTrue(shapeName in cmds.listRelatives(circle, s=True)[0]) self.assertEquals(len(shapeData["cvs"]), len(self.data[shapeName]["cvs"])) self.assertEquals(len(shapeData["knots"]), len(self.data[shapeName]["knots"])) self.assertEquals(shapeData["degree"], self.data[shapeName]["degree"]) self.assertEquals(shapeData["form"], self.data[shapeName]["form"]) self.assertEquals(tuple(shapeData["knots"]), self.data[shapeName]["knots"]) self.assertEquals(shapeData["overrideColorRGB"], self.data[shapeName]["overrideColorRGB"]) self.assertEquals(shapeData["overrideEnabled"], self.data[shapeName]["overrideEnabled"])
def test_init(self): con = control.Control(name="testControl") self.assertEquals(con.colour, (0, 0, 0)) self.assertEquals(con.name, "testControl") curve = cmds.circle(ch=False)[0] con = control.Control(node=nodes.asMObject(curve)) self.assertTrue(cmds.objExists(con.dagPath.fullPathName()))
def exportFbx(filePath, sceneNode, version="FBX201600", skeletonDefinition=False, constraints=False): filePath = filePath.replace("/", "\\") with exportContext(nodes.asMObject(sceneNode)): mel.eval("FBXResetExport ;") mel.eval("FBXExportSmoothingGroups -v true;") mel.eval("FBXExportHardEdges -v true;") mel.eval("FBXExportTangents -v true;") mel.eval("FBXExportSmoothMesh -v false;") mel.eval("FBXExportInstances -v true;") # Animation mel.eval("FBXExportBakeComplexAnimation -v true;") mel.eval("FBXExportApplyConstantKeyReducer -v true;") mel.eval("FBXExportUseSceneName -v false;") mel.eval("FBXExportQuaternion -v euler;") mel.eval("FBXExportShapes -v true;") mel.eval("FBXExportSkins -v true;") mel.eval("FBXExportConstraints -v {};".format("false" if not constraints else "true")) mel.eval("FBXExportSkeletonDefinitions -v {};".format("false" if not skeletonDefinition else "true")) mel.eval("FBXExportCameras -v true;") mel.eval("FBXExportLights -v true;") mel.eval("FBXExportEmbeddedTextures -v false;") mel.eval("FBXExportInputConnections -v false;") mel.eval("FBXExportUpAxis {};".format(general.upAxis())) mel.eval("FBXExportFileVersion -v {};".format(version)) cmds.select(sceneNode) mel.eval('FBXExport -f "{}" -s;'.format(filePath.replace("\\", "/"))) # this maya is retarded cmds.select(cl=True) return filePath
def test_removeFromActiveSelection(self): cmds.select([self.node, cmds.createNode("transform")]) selected = scene.getSelectedNodes() self.assertEquals(len(selected), 2) scene.removeFromActiveSelection(nodes.asMObject(self.node)) selected = scene.getSelectedNodes() self.assertEquals(len(selected), 1)
def test_getWorldInverseMatrix(self): node = nodes.asMObject(self.node) matrix = nodes.getWorldInverseMatrix(node) self.assertIsInstance(matrix, om2.MMatrix) parentPlug = om2.MFnDagNode(node).findPlug("worldInverseMatrix", False) parentPlug.evaluateNumElements() matPl = parentPlug.elementByPhysicalIndex(0) self.assertEquals(matrix, om2.MFnMatrixData(matPl.asMObject()).matrix())
def create(self, driver, driven, skipRotate=None, skipTranslate=None, maintainOffset=False): driverName = nodes.nameFromMObject(driver) drivenName = nodes.nameFromMObject(driven) const = cmds.parentConstraint(driverName, drivenName, skipRotate=skipRotate or [], skipTranslate=skipTranslate or [], weight=1.0, maintainOffset=maintainOffset) self.node = om2.MObjectHandle(nodes.asMObject(const[0])) return self.node.object()
def globalWidthHeight(): """Returns the Scene global render width and height :return: The width and height values returns from the "defaultResolution" node :rtype: tuple(int, int) """ fn = om2.MFnDependencyNode(nodes.asMObject("defaultResolution")) width, height = fn.findPlug("width", False).asInt(), fn.findPlug("height", False).asInt() return width, height
def validateAndBuild(parentMenu, nodeName): if cmds.objExists(nodeName): triggerNode = nodes.asMObject(nodeName) else: # ::note should we just check for selection here and validate? return 0 if not utils.hasTrigger(triggerNode): return 0 triggerNodes = [triggerNode] + [ i for i in scene.getSelectedNodes() if i != triggerNode ] visited = [] validLayout = None dynamic = False # gather the trigger information from the current node and the selection for st in triggerNodes: # get the compound trigger plug triggerPlugs = utils.triggerPlugsFromNode(st) # for each compound found, consolidate and gather info for tp in triggerPlugs: node = tp.node() if node in visited: continue visited.append(node) # pull the command type and command string from the compoundplug commandType = tp.child(0).asInt() commandStr = tp.child(1).asString() if commandType == utils.LAYOUT_TYPE: layout = menu.findLayout(commandStr) if not layout: continue if validLayout: validLayout.merge(layout) else: validLayout = layout elif commandType == utils.DYNAMIC_TYPE: dynamic = True break if not dynamic: if validLayout is None: return 0 validLayout.solve() mainMenu = menu.MarkingMenu(validLayout, "zooTriggerMenu", parentMenu, menu.Registry()) mainMenu.attach(**{"nodes": map(om2.MObjectHandle, triggerNodes)}) else: menu.MarkingMenu.buildDynamicMenu( commandStr, parent=parentMenu, arguments={"nodes": map(om2.MObjectHandle, triggerNodes)}) return 1
def createAnnotation(rootObj, endObj, text=None, name=None): name = name or "annotation" rootDag = om2.MFnDagNode(rootObj) boundingBox = rootDag.boundingBox center = om2.MVector(boundingBox.center) transform = nodes.createDagNode("_".join([name, "loc"]), "transform", parent=rootObj) nodes.setTranslation(transform, nodes.getTranslation(rootObj, om2.MSpace.kWorld), om2.MSpace.kWorld) annotationNode = nodes.asMObject( cmds.annotate(nodes.nameFromMObject(transform), tx=text)) annParent = nodes.getParent(annotationNode) nodes.rename(annParent, name) plugs.setPlugValue( om2.MFnDagNode(annotationNode).findPlug("position", False), center) nodes.setParent(annParent, endObj, False) return annotationNode, transform
def createAnnotation(rootObj, endObj, text=None, name=None): newparent = nodes.createDagNode("_".join([name, "ann_hrc"]), "transform") name = name or "annotation" locator = nodes.createDagNode("_".join([name, "loc"]), "locator", newparent) annotationNode = nodes.asMObject( cmds.annotate(nodes.nameFromMObject(locator), tx=text)) annParent = nodes.getParent(annotationNode) nodes.rename(annParent, name) nodes.setParent(annParent, newparent, False) extras = [newparent] constraint = constraints.MatrixConstraint( name="_".join([name, "startMtx"])) extras.extend( constraint.create(endObj, locator, (True, True, True), (True, True, True), (True, True, True))) extras.extend( constraint.create(rootObj, annParent, (True, True, True), (True, True, True), (True, True, True))) return annParent, locator, extras
def create(self, driver, driven, skipRotate=None, skipTranslate=None, maintainOffset=False): driverName = nodes.nameFromMObject(driver) drivenName = nodes.nameFromMObject(driven) const = cmds.parentConstraint(driverName, drivenName, skipRotate=skipRotate or [], skipTranslate=skipTranslate or [], weight=1.0, maintainOffset=maintainOffset) self.node = om2.MObjectHandle(nodes.asMObject(const[0])) mapping = dict(skipRotate=skipRotate, skipTranslate=skipTranslate, maintainOffset=maintainOffset) kwargsMap = json.dumps(mapping) addConstraintMap(driver, (driven, ), (self.node.object(), ), kwargsMap=kwargsMap) return self.node.object()
def test_iterParent(self): cmds.group(self.node) cmds.group(self.node) cmds.group(self.node) parents = [i for i in nodes.iterParents(nodes.asMObject(self.node))] self.assertEquals(len(parents), 3)
def test_cmpNodes(self): obj = nodes.asMObject(self.node) self.assertTrue(generic.compareMObjects(obj, obj)) self.assertFalse( generic.compareMObjects( obj, nodes.asMObject(cmds.createNode("multiplyDivide"))))
def test_getShapes(self): node = nodes.asMObject(cmds.polyCube(ch=False)[0]) self.assertEquals(len(nodes.shapes(om.MFnDagNode(node).getPath())), 1) self.assertIsInstance( nodes.shapes(om.MFnDagNode(node).getPath())[0], om.MDagPath)
def test_setTranslation(self): node = nodes.asMObject(self.node) nodes.setTranslation(node, om2.MVector(0, 10, 0)) self.assertEquals(nodes.getTranslation(node, space=om2.MSpace.kObject), om2.MVector(0, 10, 0))
def test_setParent(self): obj = nodes.asMObject(self.node) self.assertFalse(nodes.hasParent(obj)) transform = nodes.asMObject(cmds.createNode("transform")) nodes.setParent(transform, obj) self.assertTrue(nodes.hasParent(transform))
def test_asMObject(self): self.assertIsInstance(nodes.asMObject(self.node), om.MObject)
def test_iterAttributes(self): node = nodes.asMObject(self.node) for i in nodes.iterAttributes(node): self.assertIsInstance(i, om2.MPlug) self.assertFalse(i.isNull)
def test_isMObjectHandle(self): obj = om.MObjectHandle(nodes.asMObject(self.node)) self.assertTrue(generic.isValidMObjectHandle(obj)) cmds.delete(self.node) self.assertFalse(generic.isValidMObjectHandle(obj))
def test_deleteNode(self): node = om2.MObjectHandle(nodes.asMObject(cmds.createNode("transform"))) self.assertTrue(node.isValid() and node.isAlive()) nodes.delete(node.object()) self.assertFalse(node.isValid() and node.isAlive())
def globalWidthHeight(): fn = om2.MFnDependencyNode(nodes.asMObject("defaultResolution")) width, height = fn.findPlug("width", False).asInt(), fn.findPlug("height", False).asInt() return width, height, float(width) / float(height)
def test_isValidMDagPath(self): obj = om.MFnDagNode(nodes.asMObject(self.node)).getPath() self.assertTrue(nodes.isValidMDagPath(obj))