Mel Script - Skin Clusters [API]

 

 

 

 

Smooth skinning is implimented via skin clusters. The skin cluster takes inputs from a set of joints and then deforms the vertices of the input mesh with respect to a weighted sum of the influence joints.

There are two main commands to deal with skin clusters in maya, skinCluster and skinPercent. The first command allows you to skin geometry, the second allows you to edit the skin weights.

 

 

 

 

 

Finding All Skin Clusters in a Scene

To find all of the skin clusters nodes, we can simply use ls.

 

 


		// get a list of all skin clusters in the scene
		$clusters = `ls -type "skinCluster"`;

		// loop through each skin cluster
		for( $cluster in $clusters )
		{
			// print the name of the skin node
			print( $cluster + "\n" );
		}
		

 

 

 

 

Accessing the Skinning Data

To access the geometry that is skinned, and the influence joints, you need to use the skinCluster command with -query flag. The skinPercent command allows you to query the specific percentage weightings of the influence joints.

 

 

 


  proc getSkinClusterData( string $skin )
  {
		// get influenced geometry
		string $geometry[] = `skinCluster -q -g $skin`;
	
		// get a list of transforms that have an influence on the geometry
		string $influences[] = `skinCluster -q -wi $skin`;
	
		print( "$skin +
				"\n{\n\tGeom " + $geometry[0] +
				"\n\tInfluences " + size($influences) + " ";
	
		// output the transforms that influence the geometry
		for($i=0;$i<size($influences);$i++)
		{
			print($influences[$i] + " ");
		}
	
		// if outputting a mesh
		if(`nodeType $geometry[0]` == "mesh")
		{
			int $controlPointCount = `polyEvaluate -v $geometry[0]`;
	
			//	output the vertex weights for a mesh
			for($i=0;$i<$controlPointCount;$i++)
			{
				// select the vertex to query the weights for
				select -r ($geometry[0]+".v["+$i+"]");
	
				// print all influence weights for this vertex
				print("\n\t");
				for($j=0;$j<size($influences);$j++)
				{
					$weight = `skinPercent -t $influences[$j] -q $skin`;
					print($weight+" ");
				}
			}
		}
		else
		// if outputting a surface
		if(`nodeType $geometry[0]` == "nurbsSurface")
		{
			// use a surface info Node to access the NURBS data.
			$infoNode = `createNode surfaceInfo`;
	
			// commect the info node to the surface
			$outAttr = $geometry[0]+ ".ws[0]" ;
			$inAttr  = $infoNode + ".is" ;
			connectAttr $outAttr $inAttr ;
	
			// get U information
			$degreeU = `getAttr ($geometry[0]+ ".degreeU")`;
			$spansU  = `getAttr ($geometry[0]+ ".spansU")`;
			$numCVsU = $spansU + $degreeU;
	
			// get V information
			$degreeV = `getAttr ($geometry[0]+ ".degreeV")`;
			$spansV  = `getAttr ($geometry[0]+ ".spansV")`;
			$numCVsV = $spansV + $degreeV;
	
			delete $infoNode;
	
			// output the weighting data for a nurbs surface
			for($i=0;$i<$numCVsU;$i++)
			{
				for($k=0;$k<$numCVsV;$k++)
				{
					// select the control point to query the weight on
					select -r ($geometry[0]+".cv["+$i+"]["+$k+"]");
						
					// print all influence weights for the CV
					print("\n\t");
					for($j=0;$j<size($influences);$j++)
					{
						$weight = `skinPercent -t $influences[$j] -q $skin`;
						print( $weight + " ");
					}
				}
			}
		}
	
		//	close the data off a bit
		print("\n}\n");
  }	

 

 

 

 

Skinning Geometry via mel

To access the geometry that is skinned, and the influence joints, you need to use the skinCluster command with -query flag. The skinPercent command allows you to query the specific percentage weightings of the influence joints.

 

 

 


		
		// create some geometry to skin 
		$geom=`polyCylinder -r 1 -h 4 -sx 20 -sy 10 -sz 1 -ax 0 1 0 -tx 1 -ch 1`;
		
		// create first joint
		$j1 = `joint -p 0 1.6 0`;
		
		// create second joint
		$j2 = `joint -p 0 0 0`;
		
		// parent to first
		joint -e -zso -oj xyz -sao yup $j1;
		
		// create third joint
		$j3 = `joint -p 0 -1.6 0`;
		
		// parent to second
		joint -e -zso -oj xyz -sao yup $j2;

		// smooth bind them...
		skinCluster -toSelectedBones -dropoffRate 4.5 $j1 $j2 $j3 $geom;

 

 

 

 

Editing Skin Weights Via mel

To edit the skin weightings for a surface, use the skinPercent command with the -tv flag. The -tv flag takes two arguments, the name of the joint and it's weight.

 

 

 


		// edit skin weights for pSphere1.vtx[0]
		skinPercent -tv joint 0.3 -tv joint2 0.7 "pSphere.vtx[0]";