Monday, September 22, 2014

Switch multiple textures in Maya using layered texture



This is a really cool technique I found in Maya to switch between multiple textures. Whenever I have googled about switching textures, all I could find is using psd layers or an image sequence. What if I don’t have Photoshop or any editing software but three or four texture file to switch between into a model or rig?
Here is how you go about:
Select your model and apply a lambert shader or any other material you would like.

Open your hypershade and add a layered texture from create menu.  Add three different images to individual file textures and then connect the out color to the layeredTexture input color. Your shading network would look like this:




Create an attribute in your global control which would be used to make texture selection. I have quickly added a texture attribute with integer value from min 0 and max 2. 0 is for texture 1, 1 is for texture 2 and 2 is for texture 3. You can have any other values or use enum type which is even more readable for the animator.
Here comes the real deal!
You use SDK to switch between textures and animate the Alpha values of the individual texture file added in the layered texture.
Basically, when you want to select the first texture, you zero out the alpha values of texture 2 and 3 in the layered texture and keep the alpha value 1 for the 1st texture.
You do the same for all other switches.
I could not SDK the alpha value using the SDK dialogue box, maya gave me an error “No object matches name: layeredTexture1.alpha”. So I wrote a small code in MEL to do this job-
   
for($i=0; $i<3; $i++)
{
    setAttr "pCube1.Texture" $i;
   
    if($i==0)
    {
        setAttr "layeredTexture1.inputs[0].alpha" 1;
        setAttr "layeredTexture1.inputs[1].alpha" 0;
        setAttr "layeredTexture1.inputs[2].alpha" 0;
    }
   
    else if($i==1)
    {
        setAttr "layeredTexture1.inputs[0].alpha" 0;
        setAttr "layeredTexture1.inputs[1].alpha" 1;
        setAttr "layeredTexture1.inputs[2].alpha" 0;
    }
    else if($i==2)
    {
        setAttr "layeredTexture1.inputs[0].alpha" 0;
        setAttr "layeredTexture1.inputs[1].alpha" 0;
        setAttr "layeredTexture1.inputs[2].alpha" 1;
    }
    setDrivenKeyframe -currentDriver "pCube1.Texture" "layeredTexture1.inputs[0].alpha";
    setDrivenKeyframe -currentDriver "pCube1.Texture" "layeredTexture1.inputs[1].alpha";
    setDrivenKeyframe -currentDriver "pCube1.Texture" "layeredTexture1.inputs[2].alpha";
   
}


 Cheers!

4 comments:

  1. Replies
    1. Sorry!
      I guess when I pasted the script something got messed up. I have edited it now.
      Should work!

      Delete
  2. Everything worked well, Thank u.
    But when scrubbing the time slider the texture resets to the last attribute option in the global control.
    Can u suggest me a solution.

    ReplyDelete
    Replies
    1. I know this is quite late but have you been able to fix it? If not you can email me at ruha.imin[at]gmail.com, I will try to help you at my best.
      Cheers!

      Delete