List will only append the last item

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 07 July 2013   #1
List will only append the last item

Hello,

This is my first "on my own" script I am writing and I am having an issue with making a list through a for loop. I have gone through the Pymel reference for lists and came across this:
Pymel REF
Code:
matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]

Code:
transposed = [] for i in range(4): # the following 3 lines implement the nested listcomp transposed_row = [] for row in matrix: transposed_row.append(row[i]) transposed.append(transposed_row) transposed


What I am trying to do.
The problem I am having is that it will only return the last item the script goes through in the for loop. My Script:
Code:
import pymel.core as pm #Declaring the names shaderName = "TESTMIA003" layTextname = "LAYEREDTEXTURETEST003" checkerTextname = "CHECKERTEST003" #create the materials miaShade001 = pm.shadingNode('mia_material_x_passes', name=shaderName, asShader=True) layText = pm.shadingNode('layeredTexture', name = layTextname, asShader =True) checkerText = pm.shadingNode('checker', name = checkerTextname, at = True) #Making Connections to the Shaders #shader names + connections(seperated) connections = 'layText.name()' + '.inputs[0]' shaders = [['miaShade001.name()','.result'],[connections,".color"] ,('checkerText.name()', '.outAlpha'), (connections,".alpha")] print shaders #length of shader group shadLen = len(shaders[0]) print "shadLen_result:" + str(shadLen) #grouped shader Names + connections shadGrp = [] #combining the shader group for shad in range(shadLen-1): for row in shaders: row = row[0] + row[1] print "ROW_result:" + row shadGrp.append(row) print "shadGrp_result" + str(shadGrp)

The result of code:
Code:
shadLen_result:2 ROW_result:miaShade001.name().result ROW_result:layText.name().inputs[0].color ROW_result:checkerText.name().outAlpha ROW_result:layText.name().inputs[0].alpha shadGrp_result['layText.name().inputs[0].alpha']


The overall result I want is to be able to connect shaders together like:
Code:
miaShade001.result >> layTextname.inputs[0].color


Thank you for anyone who can help.
 
Old 07 July 2013   #2
If I understand correctly, the problem is where you append the row to the list. It is outside of the for loop. Remember, with Python indentation is important. Here is your original code:

Code:
#combining the shader group for shad in range(shadLen-1): for row in shaders: row = row[0] + row[1] print "ROW_result:" + row shadGrp.append(row)

To add every row to the list, the append call needs to be part of the for loop:
Code:
for shad in range(shadLen-1): for row in shaders: row = row[0] + row[1] print "ROW_result:" + row shadGrp.append(row)

-ChrisZ
 
Old 07 July 2013   #3
Thank you.
I was able to figure it out right before coming here. I was able to use the code that you gave me and compress my version even further and came up with this:

Code:
#grouped shader Names + connections shadGrp = [] #combining the shader group for shad in range(1): for row in shaders: rows = row[0] + row[1] #print "ROW_result:" + rows shadGrp.append(rows) print "shadGrp_result:" + str(shadGrp)


I think its coming together now.
 
Old 07 July 2013   #4
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:43 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.