Jump to content
  • Advertisement
  • entries
    5
  • comments
    6
  • views
    7733

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

Sign in to follow this  
ongamex92

997 views

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 ^_^

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!