Assign Shaders Based on Imported File's Shaders?


#1

Hi guys,

I have to work back-and-forth between Alias and Maya. I have an Alias shader library of Blinns. When I import the Alias file into Maya, I’d like to have a script reads the names of the imported shaders and assigns corresponding Maya ones (I have an equivalent Maya library too).

E.G. I make a bicycle in Alias. I assign a red paint for the metal and leather for the seat. When I import the bike into Maya, the red paint and leather are replaced by maya shaders that were already in the maya scene.

Any ideas? What should I research?

Thanks in advance!

:shrug:


#2

I think you can go in such way:

  1. Create list of all imported geometry and for each member of list:
  2. Check get name of assigned shader
  3. Assign new corresponding shader

To make this algorithm work you need specific names for each shader (source and new, for example: SRC_METAL_01 >> FIN_METAL_01 ). If its possible, then there should not be any issue with it (i can supply all necessary python commands). If proper naming is a problem, then you have another option — automatically convert shaders.


#3

Beautiful solution, thank you!

:keenly:


#4

Kiryha,

One more question: If a shader is missing, it breaks my script. My script works correctly if a model has every shader in my library assigned. But most things I build only require 3 or 4 shaders out of the 30 available materials. Once the script can’t find a shader it stops.

Do I need to include some language that tells maya to ignore ‘missing shaders’? Like a conditional?


#5

Can you post your script here?


#6

I have shaders called Metal_Red, Metal_Green and Metal_Blue.
The Maya scene already has identical shader names. Once the file is imported into Maya, the incoming shaders are immediately renamed Metal_Red1, Metal_Blue1, etc.

My Maya script says:

select -r Metal_Red1 ;
hyperShade -objects Metal_Red1;
hyperShade -assign Metal_Red;
sets -e forceElement Metal_RedSG

select -r Metal_Green1 ;
hyperShade -objects Metal_Green1;
hyperShade -assign Metal_Green;
sets -e forceElement Metal_GreenSG

select -r Metal_Blue1 ;
hyperShade -objects Metal_Blue1;
hyperShade -assign Metal_Blue;
sets -e forceElement Metal_BlueSG

This scripts works, but only if all three materials are used. If I only use 1 or 2 materials, the script breaks.


#7

You may try objExists command to check if material exists or not