textures connect to shaders respective attribute(if, elif not working)

textures connect to shaders respective attribute(if, elif not working)

Anonymous
Not applicable
598 Views
1 Reply
Message 1 of 2

textures connect to shaders respective attribute(if, elif not working)

Anonymous
Not applicable

Hey Guys,

 

I have created a script which will automatically connect textures to the respective attribute of shaders. but sometimes we are not using some textures then it is having an issue. Maya is giving an error:

# Error: IndexError: file <maya console> line 20: list index out of range #

Now, I am trying to use "if, elif" , which is not working.

Please have a look at the script below, Let me know what will be the solution for this. Thanks.

 

 

import maya.cmds as cmds


DIF = cmds.ls('*DIF*', sl=True)
RGH = cmds.ls('*RGH*', sl=True)
NRM = cmds.ls('*NRM*', sl=True)
MTL = cmds.ls('*MTL*', sl=True)
EMI = cmds.ls('*EMI*', sl=True)

MAT =cmds.shadingNode("aiStandardSurface",asShader=True, n='aiStandardSurface1')

if cmds.isTrue(RGH):
cmds.connectAttr(RGH[0]+'.outColorR',MAT+'.specularRoughness')
elif cmds.isTrue(DIF):
cmds.connectAttr(DIF[0]+'.outColor',MAT+'.baseColor')
elif cmds.isTrue(MTL):
cmds.connectAttr(MTL[0]+'.outColorR',MAT+'.metalness')
else:
print(' ')

0 Likes
Accepted solutions (1)
599 Views
1 Reply
Reply (1)
Message 2 of 2

stuzzz
Collaborator
Collaborator
Accepted solution

hello,

 

you have many ways to check the output or raise an exception but to make simple simple I would:

 

 

DIF = cmds.ls('*DIF*', sl=True)
RGH = cmds.ls('*RGH*', sl=True)
NRM = cmds.ls('*NRM*', sl=True)
MTL = cmds.ls('*MTL*', sl=True)
EMI = cmds.ls('*EMI*', sl=True)

MAT =cmds.shadingNode("aiStandardSurface",asShader=True, n='aiStandardSurface1')

#we need to make sure that RGH contains something
if RGH: 
    cmds.connectAttr(RGH[0]+'.outColorR',MAT+'.specularRoughness')

 

simple as that. The 

0 Likes