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
  07 July 2013
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
matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]

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:
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:
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:
miaShade001.result >> layTextname.inputs[0].color


Thank you for anyone who can help.
 
  07 July 2013
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:

#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:
for shad in range(shadLen-1):
    for row in shaders:
        row = row[0] + row[1]
        print "ROW_result:" + row
        shadGrp.append(row)

-ChrisZ
 
  07 July 2013
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:

#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.
 
  07 July 2013
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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
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:09 PM.


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