2D Animation In Maya: Import images as planes

A while ago I wanted to create a simple 2D “cut-out” animation, just a few images imported and assigned on a planes. Unfortunately, Maya did not had this functionally built in. Doing this by hand requires you to:

  • Create a File Texture
  • Create some material and assign the texture as diffuse colour and alpha
  • Create a plane with correct width/height ratio
  • Modify the UV channel
  • Assign the previously created material to the plane

And on top of that you’ve got to do it for every single image that you want to use.
In order to avoid doing this tedious task by hand, I wrote a script that does just that in a few clicks.
Basically the scripts prompts you to pick the images that you want to import, and everything else is done automatically.

You can just execute the script “Script Editor” or assign it to a button on the shelf.

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

// This scripts creates a planes and assignees to them materials
// that use 2D textures for their colours.

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();

I’ve tested in Maya 2017, but it should work just fine in all older versions.
I hope you find it useful! All kind of critique and feature requests are very welcome ^_^

comments powered by Disqus