I am trying to grab all the file connections in all the surface shaders in my scene.
However, I keep getting the following error even though it is printing out all the geo names:
# Error: list index out of range
# Traceback (most recent call last):
# File "<maya console>", line 6, in <module>
# IndexError: list index out of range #
I am using the names to reassign new shaders onto it, and sadly it is able to assign 1-2 onto the right mesh while others are not assigned at all.
Those that are not assigned, I believed they are not able to map onto the same geo that is already containing a shader onto it.
Example, A building with 4 sides has 4 different textures, each assigned to the 4 individual faces respectively. (I am grabbing these gesos from google earth)
This is the code that I am using to grab the names:
import pymel.core as pm
import maya.cmds as cmds
surShader = cmds.ls(type = 'surfaceShader')
for con in surShader:
surShaderOut = cmds.listConnections('%s.outColor' % con)
meshAssign = pm.listConnections(surShaderOut[1], type = "mesh")[0].longName()
print meshAssign
print meshAssign wont print anything for the faulty call as it will error before it reaches that point.
It’s either falling out of range on surShaderOut or where you’re listing the mesh connections.
I’d try printing out the value of surShaderOut and pm.listConnections(surShaderOut[1], type = “mesh”) before the meshAssign to see which of these it is.
[QUOTE=snoutling;24895]print meshAssign wont print anything for the faulty call as it will error before it reaches that point.
It’s either falling out of range on surShaderOut or where you’re listing the mesh connections.
I’d try printing out the value of surShaderOut and pm.listConnections(surShaderOut[1], type = “mesh”) before the meshAssign to see which of these it is.[/QUOTE]
Hi snoutling, thanks for getting back to me.
I am able to print out all out, with the following results but it fails as long as I add in [0].longName()
Some more information and that is the geo I am handling right now comes from google earth, and from the image attachment, you can see that there are 2 geos - Ref3D_607_1 and Ref3D_617_1 where it has 2 shapes
If you look at what it’s passing back it’s returning an empty array for the mesh connections for the first two surface shaders.
This will error when you try to get the first element of the array (i.e. calling pm.listConnections(surShaderOut[1], type = “mesh”)[0] ) as there is no element to pass back i.e there is no element 0
[QUOTE=snoutling;24897]If you look at what it’s passing back it’s returning an empty array for the mesh connections for the first two surface shaders.
This will error when you try to get the first element of the array (i.e. calling pm.listConnections(surShaderOut[1], type = “mesh”)[0] ) as there is no element to pass back i.e there is no element 0[/QUOTE]
I tried pm.listConnections(surShaderOut[1], type = “mesh”).longName(), this fails too as I was testing my luck here. Then can I kindly ask what can I do to rectify the situation here? Cause I need the long name for it and for some reasons when I tried using cmds.ls(l=True), it does not seems to be helping in my cause…
Are you just trying to get all the meshes that have surfaceShaders on them? If yes than what about something like this.
from itertools import chain
import pymel.core as pm
surShader = pm.ls(type='surfaceShader')
meshes = []
for i in surShader:
surShaderOut = i.outColor.outputs()
meshes.append(surShaderOut[0].inputs(type='mesh'))
meshes = list(set(chain.from_iterable(meshs))) # flattens list and removes duplicates
print meshes
[QUOTE=passerby;24901]Are you just trying to get all the meshes that have surfaceShaders on them? If yes than what about something like this.
from itertools import chain
import pymel.core as pm
surShader = pm.ls(type='surfaceShader')
meshes = []
for i in surShader:
surShaderOut = i.outColor.outputs()
meshes.append(surShaderOut[0].inputs(type='mesh'))
meshes = list(set(chain.from_iterable(meshs))) # flattens list and removes duplicates
print meshes
[/QUOTE]
Hi Passerby, yes you are right to a certain extend but as per my image attachment in my first post, so far I am able to have geos that contains 2 shapes in them and seperate them as 2 individual geos.
However, the objective of my code that I presented, is to grab the name of the meshes that have surfaceShader on them. And hence as I am using surShaderOut[1], - RefRep_609_1126_1SG to grab me the name of the mesh it is on namely, Ref3D_607_1