2D animation in Maya: a script that import images as planes

Published September 08, 2017
2D
Advertisement

This is basically a duplicate of what I've wrote on my recently created blog (https://ongamex.github.io/maya/mel/2017-09-08-Maya2DAnim/).

In order to do 2D animation in Maya, you have to import the images, assign them to material, then assign those materials to planes, and those planes must be properly sized with correct UVs, in order not to stretch the images. Doing this by hand is extremely annoying, so I wrote this script
 


// <?php This is actually mel script, I'm just using the syntax highlight for php ^_^

// This scripts creates a planes and assignes to them materails
// that use 2D textures for their colors. Useful quickly importing 2D 
// things for animation.

proc string obtainFilename(string $path)
{
  $filepart = match( "[^/\\]*$", $path );
  
  string $buffer[];
  tokenize $filepart "." $buffer;

  return $buffer[0];
}

proc textureTo2DPlane(string $textureFileName)
{
	$filename = obtainFilename($textureFileName);
	
	// Create the texture and a 2D placement for it.
	$textureFile = `shadingNode -asTexture -isColorManaged file -name ("file_"+$filename)`;
	$texturePlacement2D = `shadingNode -asUtility place2dTexture`;
	connectAttr -f ($texturePlacement2D+".coverage") ($textureFile+".coverage");

	setAttr -type "string" ($textureFile+".fileTextureName") $textureFileName;
	
	// Create the material and attach the file as an input color.
	$material = `shadingNode -asShader lambert -name ("mtl_" + $filename)`;
	connectAttr -f ($textureFile+".outColor") ($material+".color");
	connectAttr -f ($textureFile+".outTransparency") ($material+".transparency");
	
	// Obtain the size of the texture.
	float $size[2] = `getAttr ($textureFile+".outSize")`;
	
	// " -axis 0 0 1" will make the "plane" in XY plane, facing Z.
	// "sx" and "sy" control the amount of segments in each plane.
	$mesh = `polyPlane -w ($size[0]) -h ($size[1]) -sx 1 -sy 1 -axis 0 0 1 -name ($filename)`;
	
	// Now apply the newly created material.
	// TODO: Check if we could do this without selecting.
	// TODO: Should I create a default shading group?
	select $mesh;
	hyperShade -assign ($material);
}

proc promptForFiles()
{
	string $filenames[] = `fileDialog2 -fileMode 4 -caption "Import Image"`;
	
	HypershadeWindow;
	for($i = 0; $i < size($filenames); $i++)
	{
		textureTo2DPlane($filenames[$i]);
	}
}

promptForFiles();

You can execute it directly in the "script editor", or create a shelf button that executes it.

 


I hope you find it useful ^_^

2 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement