MEL How-To #32

Back · Previous · Next Maya

How do I create a Lambert shading network given the path to the image file on disk?

Here's an example procedure that performs the necessary steps.

// To support new place2dTexture architecture in Maya v4.5
proc float mayaVersion()
{
  float $version = (float)`about -v`;
  return $version;
}

// A utility procedure to ease the connecting of two plugs with the same name
proc connect( string $attr, string $place, string $file )
{
  connectAttr -f ( $place + "." + $attr ) ( $file + "." + $attr );
}

global proc makeShader( string $file )
{
  // Create a Lambert Material, with Shading Group
  string $material = `shadingNode -asShader lambert -name myLambert`;
  string $SG = `sets -renderable true -noSurfaceShader true -empty -name myLambertSG`;
  connectAttr -f ( $material + ".outColor" ) ( $SG + ".surfaceShader" );

  // Create a File Texture and link to Material color
  string $fileNode = `shadingNode -asTexture file -name myFile`;
  connectAttr -f ( $fileNode + ".outColor" ) ( $material + ".color" );

  // Assign file to File Texture Name
  setAttr -type "string" ( $fileNode + ".fileTextureName" ) $file;

  // If you want a place2dTexture node, include the following
  string $placeName = ( "place_" + $fileNode );
  $placeName = `shadingNode -asUtility place2dTexture -name $placeName`;

  // Connect necessary plugs from place2dTexture node
  // Note: These utilize the connect() proc defined above
  connect "coverage" $placeName $fileNode;
  connect "translateFrame" $placeName $fileNode;
  connect "rotateFrame" $placeName $fileNode;
  connect "stagger" $placeName $fileNode;
  connect "wrapU" $placeName $fileNode;
  connect "wrapV" $placeName $fileNode;
  connect "repeatUV" $placeName $fileNode;
  connect "offset" $placeName $fileNode;
  connect "rotateUV" $placeName $fileNode;

  // Maya v4.5 splits the '.mirror' attribute into
  // 'mirrorU' and '.mirrorV'
  if ( mayaVersion() < 4.5 )
  {
    connect "mirror" $placeName $fileNode;
  }
  else
  {
    connect "mirrorU" $placeName $fileNode;
    connect "mirrorV" $placeName $fileNode;
  }

  // connect the placement UV plugs to the file node
  connectAttr -f ( $placeName + ".outUV" ) ( $fileNode + ".uv" );
  connectAttr -f ( $placeName + ".outUvFilterSize" ) ( $fileNode + ".uvFilterSize" );
}

To use, call 'makeShader' with the name of the image file; e.g.:

makeShader "D:/projects/images/scalySkin.iff";

22 December 2002