def testManyUVConversionsFromPlug(self): coreMesh = IECore.Reader.create( "test/IECore/data/cobFiles/pSphereShape1.cob").read() self.assertTrue("uv" in coreMesh) for i in range(0, 7): coreMesh["testUVSet%d" % i] = IECoreScene.PrimitiveVariable( coreMesh["uv"].interpolation, coreMesh["uv"].data.copy()) fn = IECoreMaya.FnOpHolder.create("test", "meshMerge") mayaMesh = maya.cmds.ls(maya.cmds.polyPlane(), dag=True, type="mesh")[0] maya.cmds.connectAttr(fn.name() + ".result", mayaMesh + ".inMesh", force=True) op = fn.getOp() with fn.parameterModificationContext(): op["input"].setValue(coreMesh) maya.cmds.file(rename="/tmp/test.ma") maya.cmds.file(save=True) maya.cmds.file(new=True, f=True) maya.cmds.file("/tmp/test.ma", open=True) fnMesh = OpenMaya.MFnMesh(IECoreMaya.dagPathFromString(mayaMesh)) self.assertEqual(fnMesh.numPolygons(), 760) # When calling fnMesh.numFaceVertices() (and other MFnMesh API calls), given a mesh with 6 # or more UV sets, which has never been evaluated before, the first call throws kFailure. # From within the ToMayaMeshConverter itself, the output plug appears fine, and the API calls # evaluate as expected. Despite this, the resulting mesh cannot be evaluated on the first try. # Making the mesh visible, or making any attempt to evaluate it, will trigger some unknown # internal updating, and subsequent attempts to evaluate it will succeed. Meshes with 5 or less # UV sets do not suffer from this problem. This was fixed in Maya 2016, but I'll leave # this explanation so users of ToMayaMeshConverter have breadcrumbs to follow. self.assertEqual(fnMesh.numFaceVertices(), 2280) self.assertEqual(maya.cmds.polyEvaluate(mayaMesh, vertex=True), 382) self.assertEqual(maya.cmds.polyEvaluate(mayaMesh, face=True), 760) u = OpenMaya.MFloatArray() v = OpenMaya.MFloatArray() fnMesh.getUVs(u, v) self.assertEqual(u.length(), 2280) self.assertEqual(v.length(), 2280) self.assertEqual(u[0], coreMesh["uv"].data[0][0]) self.assertEqual(v[0], coreMesh["uv"].data[0][1]) for i in range(0, 7): fnMesh.getUVs(u, v, "testUVSet%d" % i) self.assertEqual(u.length(), 2280) self.assertEqual(v.length(), 2280) self.assertEqual(u[12], coreMesh["testUVSet%d" % i].data[12][0]) self.assertEqual(v[12], coreMesh["testUVSet%d" % i].data[12][1])
def testManyUVConversionsFromPlug( self ) : coreMesh = IECore.Reader.create( "test/IECore/data/cobFiles/pSphereShape1.cob" ).read() self.assertTrue( "s" in coreMesh ) self.assertTrue( "t" in coreMesh ) for i in range( 0, 7 ) : coreMesh[ "testUVSet%d_s" % i ] = IECore.PrimitiveVariable( coreMesh["s"].interpolation, coreMesh["s"].data.copy() ) coreMesh[ "testUVSet%d_t" % i ] = IECore.PrimitiveVariable( coreMesh["t"].interpolation, coreMesh["t"].data.copy() ) fn = IECoreMaya.FnOpHolder.create( "test", "meshMerge" ) mayaMesh = maya.cmds.ls( maya.cmds.polyPlane(), dag=True, type="mesh" )[0] maya.cmds.connectAttr( fn.name()+".result", mayaMesh+".inMesh", force=True ) op = fn.getOp() with fn.parameterModificationContext() : op["input"].setValue( coreMesh ) maya.cmds.file( rename="/tmp/test.ma" ) maya.cmds.file( save=True ) maya.cmds.file( new=True, f=True ) maya.cmds.file( "/tmp/test.ma", open=True ) fnMesh = OpenMaya.MFnMesh( IECoreMaya.dagPathFromString( mayaMesh ) ) self.assertEqual( fnMesh.numPolygons(), 760 ) # When calling fnMesh.numFaceVertices() (and other MFnMesh API calls), given a mesh with 6 # or more UV sets, which has never been evaluated before, the first call throws kFailure. # From within the ToMayaMeshConverter itself, the output plug appears fine, and the API calls # evaluate as expected. Despite this, the resulting mesh cannot be evaluated on the first try. # Making the mesh visible, or making any attempt to evaluate it, will trigger some unknown # internal updating, and subsequent attempts to evaluate it will succeed. Meshes with 5 or less # UV sets do not suffer from this problem. Leaving this test failing in case it is fixed in a # future Maya, and to mark the issue so users of ToMayaMeshConverter have breadcrumbs to follow. self.assertEqual( fnMesh.numFaceVertices(), 2280 ) # Known failure. See note above for an explanation. self.assertEqual( maya.cmds.polyEvaluate( mayaMesh, vertex=True ), 382 ) self.assertEqual( maya.cmds.polyEvaluate( mayaMesh, face=True ), 760 ) u = OpenMaya.MFloatArray() v = OpenMaya.MFloatArray() fnMesh.getUVs( u, v ) self.assertEqual( u.length(), 2280 ) self.assertEqual( v.length(), 2280 ) self.assertEqual( u[0], coreMesh[ "s" ].data[0] ) self.assertEqual( v[0], 1.0 - coreMesh[ "t" ].data[0] ) for i in range( 0, 7 ) : fnMesh.getUVs( u, v, "testUVSet%d" % i ) self.assertEqual( u.length(), 2280 ) self.assertEqual( v.length(), 2280 ) self.assertEqual( u[12], coreMesh[ "testUVSet%d_s" % i ].data[12] ) self.assertEqual( v[12], 1.0 - coreMesh[ "testUVSet%d_t" % i ].data[12] )
def testCreases( self ): cube = maya.cmds.polyCube()[0] fnMesh = OpenMaya.MFnMesh( IECoreMaya.dagPathFromString(cube) ) cornerIds = OpenMaya.MUintArray() cornerIds.append( 5 ) cornerSharpnesses = OpenMaya.MDoubleArray() cornerSharpnesses.append( 10 ) fnMesh.setCreaseVertices( cornerIds, cornerSharpnesses ) edgeIds = OpenMaya.MUintArray() edgeIds.append( 0 ) edgeIds.append( 1 ) edgeSharpnesses = OpenMaya.MDoubleArray() edgeSharpnesses.append( 1 ) edgeSharpnesses.append( 5 ) fnMesh.setCreaseEdges( edgeIds, edgeSharpnesses ) # store which vertices belong to the affected edges util = OpenMaya.MScriptUtil() vertices = [] for edgeId in edgeIds : edgeVertices = util.asInt2Ptr() fnMesh.getEdgeVertices( edgeId, edgeVertices ) vertices.append( util.getInt2ArrayItem( edgeVertices, 0, 0 ) ) vertices.append( util.getInt2ArrayItem( edgeVertices, 0, 1 ) ) # convert and test cube = maya.cmds.listRelatives( cube, shapes=True )[0] converter = IECoreMaya.FromMayaMeshConverter.create( cube, IECoreScene.MeshPrimitive.staticTypeId() ) cortexCube = converter.convert() self.assertEqual( cortexCube.cornerIds(), IECore.IntVectorData( [ 5 ] ) ) self.assertEqual( cortexCube.cornerSharpnesses(), IECore.FloatVectorData( [ 10.0 ] ) ) self.assertEqual( cortexCube.creaseLengths(), IECore.IntVectorData( [ 2, 2 ] ) ) self.assertEqual( cortexCube.creaseIds(), IECore.IntVectorData( vertices ) ) self.assertEqual( cortexCube.creaseSharpnesses(), IECore.FloatVectorData( [ 1, 5 ] ) )
def __createChild(self, childName, sceneFile, sceneRoot, drawGeo=False, drawChildBounds=False, drawRootBound=True, drawTagsFilter="", namespace=""): if namespace: namespace += ":" dag = maya.OpenMaya.MDagPath() self.getPath(dag) dag.pop() parentPath = dag.fullPathName() childPath = parentPath + "|" + namespace + childName try: childDag = IECoreMaya.dagPathFromString(childPath) childExists = True except RuntimeError: childExists = False if childExists: shape = maya.cmds.listRelatives(childPath, f=True, type="ieSceneShape") if shape: fnChild = IECoreMaya.FnSceneShape(shape[0]) else: fnChild = IECoreMaya.FnSceneShape.createShape(childPath) else: fnChild = IECoreMaya.FnSceneShape.create( childName, transformParent=parentPath) fnChild.findPlug("file").setString(sceneFile) sceneRootName = "/" + childName if sceneRoot == "/" else sceneRoot + "/" + childName fnChild.findPlug("root").setString(sceneRootName) fnChildTransform = maya.OpenMaya.MFnDagNode(fnChild.parent(0)) index = self.__queryIndexForPath("/" + childName) outTransform = self.findPlug("outTransform").elementByLogicalIndex( index) dgMod = maya.OpenMaya.MDGModifier() childTranslate = fnChildTransform.findPlug("translate") if childTranslate.isConnected(): connections = maya.OpenMaya.MPlugArray() childTranslate.connectedTo(connections, True, False) dgMod.disconnect(connections[0], childTranslate) dgMod.connect(outTransform.child(self.attribute("outTranslate")), childTranslate) childRotate = fnChildTransform.findPlug("rotate") if childRotate.isConnected(): connections = maya.OpenMaya.MPlugArray() childRotate.connectedTo(connections, True, False) dgMod.disconnect(connections[0], childRotate) dgMod.connect(outTransform.child(self.attribute("outRotate")), childRotate) childScale = fnChildTransform.findPlug("scale") if childScale.isConnected(): connections = maya.OpenMaya.MPlugArray() childScale.connectedTo(connections, True, False) dgMod.disconnect(connections[0], childScale) dgMod.connect(outTransform.child(self.attribute("outScale")), childScale) childTime = fnChild.findPlug("time") if childTime.isConnected(): connections = maya.OpenMaya.MPlugArray() childTime.connectedTo(connections, True, False) dgMod.disconnect(connections[0], childTime) dgMod.connect(self.findPlug("outTime"), childTime) dgMod.doIt() fnChild.findPlug("drawGeometry").setBool(drawGeo) fnChild.findPlug("drawChildBounds").setBool(drawChildBounds) fnChild.findPlug("drawRootBound").setBool(drawRootBound) if drawTagsFilter: parentTags = drawTagsFilter.split() childTags = fnChild.sceneInterface().readTags( IECoreScene.SceneInterface.EveryTag) commonTags = filter(lambda x: str(x) in childTags, parentTags) if not commonTags: # Hide that child since it doesn't match any filter fnChildTransform.findPlug("visibility").setBool(False) else: fnChild.findPlug("drawTagsFilter").setString( " ".join(commonTags)) return fnChild
def __createChild( self, childName, sceneFile, sceneRoot, drawGeo = False, drawChildBounds = False, drawRootBound = True, drawTagsFilter = "", namespace = "" ) : if namespace: namespace += ":" dag = maya.OpenMaya.MDagPath() self.getPath( dag ) dag.pop() parentPath = dag.fullPathName() childPath = parentPath+"|"+namespace+childName try : childDag = IECoreMaya.dagPathFromString( childPath ) childExists = True except RuntimeError : childExists = False if childExists : shape = maya.cmds.listRelatives( childPath, f=True, type="ieSceneShape" ) if shape: fnChild = IECoreMaya.FnSceneShape( shape[0] ) else: fnChild = IECoreMaya.FnSceneShape.createShape( childPath ) else: fnChild = IECoreMaya.FnSceneShape.create( childName, transformParent = parentPath ) fnChild.findPlug( "file" ).setString( sceneFile ) sceneRootName = "/"+childName if sceneRoot == "/" else sceneRoot+"/"+childName fnChild.findPlug( "root" ).setString( sceneRootName ) fnChildTransform = maya.OpenMaya.MFnDagNode( fnChild.parent( 0 ) ) index = self.__queryIndexForPath( "/"+childName ) outTransform = self.findPlug( "outTransform" ).elementByLogicalIndex( index ) dgMod = maya.OpenMaya.MDGModifier() childTranslate = fnChildTransform.findPlug( "translate" ) if childTranslate.isConnected() : connections = maya.OpenMaya.MPlugArray() childTranslate.connectedTo( connections, True, False ) dgMod.disconnect( connections[0], childTranslate ) dgMod.connect( outTransform.child( self.attribute( "outTranslate" ) ), childTranslate ) childRotate = fnChildTransform.findPlug( "rotate" ) if childRotate.isConnected() : connections = maya.OpenMaya.MPlugArray() childRotate.connectedTo( connections, True, False ) dgMod.disconnect( connections[0], childRotate ) dgMod.connect( outTransform.child( self.attribute( "outRotate" ) ), childRotate ) childScale = fnChildTransform.findPlug( "scale" ) if childScale.isConnected() : connections = maya.OpenMaya.MPlugArray() childScale.connectedTo( connections, True, False ) dgMod.disconnect( connections[0], childScale ) dgMod.connect( outTransform.child( self.attribute( "outScale" ) ), childScale ) childTime = fnChild.findPlug( "time" ) if childTime.isConnected() : connections = maya.OpenMaya.MPlugArray() childTime.connectedTo( connections, True, False ) dgMod.disconnect( connections[0], childTime ) dgMod.connect( self.findPlug( "outTime" ), childTime ) dgMod.doIt() fnChild.findPlug( "drawGeometry" ).setBool( drawGeo ) fnChild.findPlug( "drawChildBounds" ).setBool( drawChildBounds ) fnChild.findPlug( "drawRootBound" ).setBool( drawRootBound ) if drawTagsFilter: parentTags = drawTagsFilter.split() childTags = fnChild.sceneInterface().readTags(IECore.SceneInterface.EveryTag) commonTags = filter( lambda x: str(x) in childTags, parentTags ) if not commonTags: # Hide that child since it doesn't match any filter fnChildTransform.findPlug( "visibility" ).setBool( False ) else: fnChild.findPlug( "drawTagsFilter" ).setString( " ".join( commonTags ) ) return fnChild