checking the ".color" for incoming texture if not exists then get the rgb values. ( getting error )

942 views
Skip to first unread message

Nitin Singh

unread,
Jan 11, 2014, 11:04:43 AM1/11/14
to python_in...@googlegroups.com
hey Guys,

i m trying  to get the color node incoming connection so that i can store it for later use,
ever thing works well before for loop but when i run for loop to chk the type then it give me error :

# Traceback (most recent call last):
#   File "<string>", line 12, in <module>
#   File "<string>", line 17, in <module>
# TypeError: 'NoneType' object is not iterable

and i have no idea how to fix it, please help.

thanks a lot.
have a good weekend. :)



import maya.cmds as cmds

# getting the shape node from transform node
selectedObj = cmds.ls(sl = True )[0]
selectType = cmds.listRelatives( selectedObj, shapes = True )
#getting the name of shader connected to it
listConnect = cmds.listConnections(selectType, type="shadingEngine")[0]
shadername = (listConnect + ".surfaceShader")
shader = cmds.listConnections(shadername)[0]

colorAttribute = (shader+".color")
colorValues = cmds.getAttr(colorAttribute)
print colorValues

inputConnectionCheck = cmds.listConnections(colorAttribute,destination = True)
print inputConnectionCheck

for incoming in inputConnectionCheck:
    if cmds.objectType( incoming, isType='file'):
        filePath = cmds.getAttr(incoming+".fileTextureName")
        print filePath
    else:
        colorValues = cmds.getAttr(colorAttribute)
        RGBvalues = colorValues[0]
        redValue = RGBvalues[0]
        greenValue = RGBvalues[1]
        blueValue = RGBvalues[2]
        print (redValue, greenValue, blueValue)

Nils Lerin

unread,
Jan 11, 2014, 11:16:57 AM1/11/14
to python_in...@googlegroups.com
The error happends  because cmds.listConnections(colorAttribute,destination = True) returns None because there are no such connections. Do you have anything connected to the shaders color-attribute?

If you want the script to work even if nothing is connected to shader.color you can change:
inputConnectionCheck = cmds.listConnections(colorAttribute,destination = True)
to
inputConnectionCheck = cmds.listConnections(colorAttribute,destination = True) or []

That way the for loop will just not run (because the list will be empty) but you'll always have a list there.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/6f64d6a1-4329-4115-b9e0-1aeec25e58ce%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Justin Israel

unread,
Jan 11, 2014, 2:09:07 PM1/11/14
to python_in...@googlegroups.com

I've always found it so annoying that the cmds module has functions that return inconsistently. It makes no sense to me that they wouldn't always just return the empty list. Some functions do it and some don't.

Nitin Singh

unread,
Jan 11, 2014, 4:39:52 PM1/11/14
to python_in...@googlegroups.com
"inputConnectionCheck" is already a list. I don't know why its giving me error. i tried every thing i know, also tried to look online at other places didn't found anything.

for loop give error on first run itself.
when i select a shader with a file node connected to it, it spits out the name of the file node and map, what it is suppose to do when it enters for loop BUT when i select a shader without a file node connected to it, it give error as it enters for loop even when i just put a print statement in it. and that is very weird to me and have no idea why ..

still trying to figure out :(

i have attached a maya file this time so that you can check . maybe it will help you to see what i am doing.

thanks
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Carbon_Fiber_by_Mandalore_Knight.jpg
test.ma

Justin Israel

unread,
Jan 11, 2014, 4:56:04 PM1/11/14
to python_in...@googlegroups.com
I think you are confused about how the logic is working in your code. You say "inputConnectionCheck" is already a list. But it is *only* a list if the color attribute for which you are calling listConnections() actually has destination connections. Otherwise it is None. Just because you put a print statement inside your for-loop doesn't mean it should still work. You cannot even being the for-loop over a None value. You even print the "inputConnectionCheck" value just before hitting the for-loop, so you should be seeing that when you have selected geometry that uses a shader with no connections on its color attribute, that you get nothing for that return value.

That is the reason Nils suggested formatting your call like this:

    inputConnectionCheck = cmds.listConnections(colorAttribute,destination = True) or []

It means that if you call listConnections on an attribute that has no connection, you will get back None, which means the OR will proceed and give you an empty list instead. This prevents you from hitting an exception when you then try to blindly loop on the None type.

Just to help your test a bit, try printing something that is a bit more easy to see:

    inputConnectionCheck = cmds.listConnections(colorAttribute,destination = True)
    print repr(inputConnectionCheck), type(inputConnectionCheck)

Hope that helps clarify it a bit.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1a514087-42e2-4c85-96ad-6f13da26c656%40googlegroups.com.

Nitin Singh

unread,
Jan 13, 2014, 1:29:35 PM1/13/14
to python_in...@googlegroups.com
Thank Justin and Nil it helped me a lot, i am thankful to you guys for taking time and explaining the reason to me.
I am new to python in Maya. Thanks a lot once again. :)
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages