Mel Script - File Textures [API]

 

 

 

 

Textures applied to surfaces are connected via their outColor attribute to one of the input colour parameters of a material. The placement of the texture on the surface is deteremined by a 2D textures placement node. It is the texture placement node that can be used to create effects such as scrolling or rotating UV's etc.

 

 

 

 

Finding All Textures in a Scene

Using the mel command ls we are able to list nodes of a specific type within the Maya scene. Each file texture node has an attribute called "fileTextureName" which holds the path to the image file.

 

 


		// get a list of all textures in the scene
		$textures = `ls -type "file"`;

		// loop through each texture
		for( $tex in $textures )
		{
			// print the name of the texture node
			print( $tex + "\n" );
			
			// print the file texture name
			print( `getAttr ($tex+".fileTextureName")` + "\n" );
		}
		

 

 

 

Creating a File Texture

When creating a file texture node, we also need to create the 2D texture placement node. The attributes on these two nodes then need to be connected.

 

 


			
	// this procedure creates a file texture and a 2d texture placement node.
	// it then connects all of the required attributes and sets the 
	// fileTextureName atribute of the file texture.
	proc string CreateFileTexture(string $filename) {
	
		// create the file texture node
		$file = `shadingNode -asTexture file`;
	
		// create a texture placement to allow UV scrolling etc
		$placement = `shadingNode -asUtility place2dTexture`;
	
		// connect the texture placement node to the file texture
		connectAttr -f ($placement+".coverage") ($file+".coverage");
		connectAttr -f ($placement+".translateFrame") ($file+".translateFrame");
		connectAttr -f ($placement+".rotateFrame") ($file+".rotateFrame");
		connectAttr -f ($placement+".mirrorU") ($file+".mirrorU");
		connectAttr -f ($placement+".mirrorV") ($file+".mirrorV");
		connectAttr -f ($placement+".stagger") ($file+".stagger");
		connectAttr -f ($placement+".wrapU") ($file+".wrapU");
		connectAttr -f ($placement+".wrapV") ($file+".wrapV");
		connectAttr -f ($placement+".repeatUV") ($file+".repeatUV");
		connectAttr -f ($placement+".offset") ($file+".offset");
		connectAttr -f ($placement+".rotateUV") ($file+".rotateUV");
		connectAttr -f ($placement+".noiseUV") ($file+".noiseUV");
		connectAttr -f ($placement+".vertexUvOne") ($file+".vertexUvOne");
		connectAttr -f ($placement+".vertexUvTwo") ($file+".vertexUvTwo");
		connectAttr -f ($placement+".vertexUvThree") ($file+".vertexUvThree");
		connectAttr -f ($placement+".vertexCameraOne") ($file+".vertexCameraOne");
		connectAttr ($placement+".outUV") ($file+".uv");
		connectAttr ($placement+".outUvFilterSize") ($file+".uvFilterSize");
	
		// set the file texture name of the file node
		setAttr -type "string" ($file+".ftn") $filename;
	
		return $file;
	}	
		

 

 

Accessing Texture Placement Information

Whenever we have a file texture we will also have an associated texture placement node that transforms the placement of the texture. If we want to access any of this data, we need to first get access to the texture placement node and then query it's attributes.

 

 


			
	// this procedure creates a file texture and a 2d texture placement node.
	// it then connects all of the required attributes and sets the 
	// fileTextureName atribute of the file texture.
	proc TexturePlacementInfo(string $file_texture) {
		string $placement="";
	
		// get a list of connections to this node
		$nodes = `listConnections -s 1 -d 0 $file_texture`;
		for( $node in $nodes ) {
			if( `nodeType $node` == "texturePlacement2d" ) {
				$placement = $node;
				break;
			}
		}
		
		if($placement =="")
			return;
	
		// create a texture placement to allow UV scrolling etc
		$placement = `shadingNode -asUtility place2dTexture`;
	
		// connect the texture placement node to the file texture
		print("coverage "+ `getAttr -f ($placement+".coverage")` +"\n");
		print("translateFrame "+ `getAttr -f ($placement+".translateFrame")` +"\n");
		print("rotateFrame "+ `getAttr -f ($placement+".rotateFrame")` +"\n");
		print("mirrorU "+ `getAttr -f ($placement+".mirrorU")` +"\n");
		print("mirrorV "+ `getAttr -f ($placement+".mirrorV")` +"\n");
		print("stagger "+ `getAttr -f ($placement+".stagger")` +"\n");
		print("wrapU "+ `getAttr -f ($placement+".wrapU")` +"\n");
		print("wrapV "+ `getAttr -f ($placement+".wrapV")` +"\n");
		print("repeatUV "+ `getAttr -f ($placement+".repeatUV")` +"\n");
		print("offset "+ `getAttr -f ($placement+".offset")` +"\n");
		print("rotateUV "+ `getAttr -f ($placement+".rotateUV")` +"\n");
		print("noiseUV "+ `getAttr -f ($placement+".noiseUV")` +"\n");
		
	}