# Operation with list elements

#1

With for loop we get construction like:

``````take 1 element from list
do operation A
repeat for next elements
``````

How to achieve with pyMel this:

``````take 1 element from list
do operation A
take 2 element from list
do operation B
take 3 element from list
do operation C
repeat for next elements
``````

#2

You probably should read up on the basics of Python:
https://docs.python.org/2/tutorial/datastructures.html

``````
for i in list:
foo(i)

def foo(item):
#do something

``````

#3

>You probably should read up on the basics of Python:
Well, since its 100% true your answer does not helps too much

#4

Something like this?

``````
ml = [1,2,3,4,5,6,7,8,9,10, 11, 12]
for l in range(0, len(ml), 6):
value = ml[l]
doSomethingWithOneValue(value)
twoValueList = ml[l+1:l+3]
doSomethingTwoOneValue(twoValueList )
threeValueList =  ml[l+3:l+6]
doSomethingTwoOneValue(threeValueList )

``````

#5

I would have suggested either using a dictionary or a 2nd list, even though I’m unclear what you are trying to achieve.

``````elementList = ['e1', 'e2', 'e3']

def opA(e):
print 'A',e

def opB(e):
print 'B',e

def opC(e):
print 'C',e

# either dictionary
operators = {0:opA, 1:opB, 2:opC}
for i,element in enumerate(elementList):
operators[i](element)

# or 2nd list
operators = [opA, opB, opC]
for op, element in zip(operators, elementList):
op(element)

``````

David

#6

Well, i think not quite.
This code

``````ml = [1,2,3,4,5,6,7,8,9,10, 11, 12]
for l in range(0, len(ml), 6):
value = ml[l]
print 'A' + str( value)

twoValueList = ml[l+1:l+3]
print 'B' + str(twoValueList)

threeValueList =  ml[l+3:l+6]
print 'C' + str (threeValueList)
``````

produce

``````A1
B[2, 3]
C[4, 5, 6]
A7
B[8, 9]
C[10, 11, 12]
``````

I need to get

``````A [1,2,3]
B [4,5,6]
C [7,8,9]
D [10, 11, 12]

``````

#7

Im writing script for automatic creation of object IDs for Arnold
Here what i have working  for each selected object or group i create AOV, create attribute on object, make this attribute white and feed this color to AOV.
I cant get how to build it to use 3 color instead of 1, which will reduce quantity of IDs 3 times.

``````import pymel.core as pm
import mtoa.aovs as aovs

# set rebder to Arnold
renderSet = pm.PyNode('defaultRenderGlobals')
renderSet.ren.set('arnold')

def checkID(*args):
if pm.objExists('aiAOV_objectID_*'):
pm.select('aiAOV_objectID_*')
exist = pm.ls(sl = True)[-1]
exist = exist.split('_')[2]

pm.select (d = True)
else:
exist = 0
return exist

def addAOV( index ): # Creates AOV render pass
aovName = 'objectID_' + str(index)
return aovName

colorAttrNameLong = 'mtoa_constant_' + str(colorAttrName)
pm.addAttr( longName= colorAttrNameLong , niceName = colorAttrName , usedAsColor=True, attributeType='float3' )
pm.addAttr( longName='R' +  str(colorAttrName), attributeType='float', parent=colorAttrNameLong )
pm.addAttr( longName='G' +  str(colorAttrName), attributeType='float', parent=colorAttrNameLong )
pm.addAttr( longName='B' +  str(colorAttrName), attributeType='float', parent=colorAttrNameLong )
return colorAttrNameLong

def createID(index):
# create AOV and plug aiUserDatacolor
objectID  = addAOV ('%02d' % index)
objectIDFull = 'aiAOV_' + str(objectID)
ID = pm.PyNode(objectIDFull)
mskObjectMat.outColor >> ID.defaultValue

sel = pm.ls(sl = 1) #select jbject for object IDs

index = float(checkID()) + 1
for i in sel:
createID(index)
pm.select(i)
pm.runtime.SelectHierarchy()
selShapes = pm.ls(sl = 1, shapes = True)
pm.select(selShapes)

for e in selShapes: #set mask color to white
attr = str(e) + '.mtoa_constant_mask_' + str('%02d' % index)
pm.setAttr(attr, [1,1,1])

index += 1

``````

#8
``````elementList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]

def opA(e):
print 'A',e

def opB(e):
print 'B',e

def opC(e):
print 'C',e

operators = {0:opA, 1:opB, 2:opC}
for i,element in enumerate(elementList):
operators[i/3%3](element)

``````

David

#9

Ok, just saw your last post…

``````e = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]

for i in xrange(0,len(e),12):
print 'A', e[i:i+3]
print 'B', e[i+3:i+6]
print 'C', e[i+6:i+9]
print 'D', e[i+9:i+12]

``````

David

#10

Thanks David, looks like what i need!
Will try to implement this to my code :banghead:

#11

In my last post the method I suggested is pretty hard coded to find chunks of 3 elements, and I thought there must be something out there that is better. Sure enough Hamish McKenzie wrote a nice little generator function several years ago that is much better.

``````def iterBy( iterable, count ):
'''
returns an generator which will yield "chunks" of the iterable supplied of size "count".  eg:
for chunk in iterBy( range( 7 ), 3 ): print chunk

results in the following output:
[0, 1, 2]
[3, 4, 5]
[6]
'''
cur = 0
i = iter( iterable )
while True:
try:
toYield = []
for n in range( count ): toYield.append( i.next() )
yield toYield
except StopIteration:
if toYield: yield toYield
break

``````

This makes it really easy to process a list in chunks of any size…

``````x = range(20)
for i in iterBy(x,4):
print i

``````

And we get…
[0, 1, 2, 3]
[4, 5, 6, 7]
[8, 9, 10, 11]
[12, 13, 14, 15]
[16, 17, 18, 19]

Thanks again Hamish!

David

#12

Thanks, David, the last one was easy to use.
If somebody need automatic creation of object IDs for arnold:

``````# 256 pipeline tools
# create object based IDs
# select objects or groups an run script
# each item in selected group will share same ID

import pymel.core as pm
import mtoa.aovs as aovs
import sys

#check if current render is Arnold
if( pm.getAttr( 'defaultRenderGlobals.currentRenderer' ) != 'arnold' ):
pm.confirmDialog( t="Warning", message= "Set current render to Arnold!", icon='critical' )
sys.exit( "Please use Arnold render!" )

def checkID(*args):#check existing ids to create proper nubers for next ids
if pm.objExists('aiAOV_objectID_*'):
pm.select('aiAOV_objectID_*')
exist = pm.ls(sl = True)[-1]
exist = exist.split('_')[2]
pm.select (d = True)
else:
exist = 0
return exist

def iterBy( iterable, count ):#Hamish McKenzie procedure to process list in blocks of N elrmrnts
cur = 0
i = iter( iterable )
while True:
try:
toYield = []
for n in range( count ): toYield.append( i.next() )
yield toYield
except StopIteration:
if toYield: yield toYield
break

def addAOV( index ): # Creates AOV render pass
aovName = 'objectID_' + str(index)
return aovName

def deleteAOV(*args):# delete AOVs and aiUserDataColor shaders
pm.delete('aiAOV_objectID_*')

colorAttrNameLong = 'mtoa_constant_' + str(colorAttrName)
pm.addAttr( longName= colorAttrNameLong , niceName = colorAttrName , usedAsColor=True, attributeType='float3' )
pm.addAttr( longName='R' +  str(colorAttrName), attributeType='float', parent=colorAttrNameLong )
pm.addAttr( longName='G' +  str(colorAttrName), attributeType='float', parent=colorAttrNameLong )
pm.addAttr( longName='B' +  str(colorAttrName), attributeType='float', parent=colorAttrNameLong )
return colorAttrNameLong

def createID(index):
# create AOV and plug aiUserDatacolor
objectID  = addAOV ('%02d' % index)
objectIDFull = 'aiAOV_' + str(objectID)
ID = pm.PyNode(objectIDFull)
mskObjectMat.outColor >> ID.defaultValue

sel = pm.ls(sl = 1) #select object for object IDs

index = float(checkID()) + 1
for i in iterBy(sel,3):
#Create AOV and mask color to RED
createID(index)
pm.select(i[0])
pm.runtime.SelectHierarchy()
selShapes = pm.ls(sl = 1, shapes = True)
pm.select(selShapes)

for e in selShapes: #set mask color to R
attr = str(e) + '.mtoa_constant_maskOBJ_' + str('%02d' % index)
pm.setAttr(attr, [1,0,0])

pm.select(i[1])
pm.runtime.SelectHierarchy()
selShapes = pm.ls(sl = 1, shapes = True)
pm.select(selShapes)

for e in selShapes: #set mask color to G
attr = str(e) + '.mtoa_constant_maskOBJ_' + str('%02d' % index)
pm.setAttr(attr, [0,1,0])

pm.select(i[2])
pm.runtime.SelectHierarchy()
selShapes = pm.ls(sl = 1, shapes = True)
pm.select(selShapes)

for e in selShapes: #set mask color to B
attr = str(e) + '.mtoa_constant_maskOBJ_' + str('%02d' % index)
pm.setAttr(attr, [0,0,1])

index += 1
``````

And some more stuf for Arnold:
http://www.kiryha.blogspot.com/2014/02/render-notes.html

#13

Nice one. I often try to avoid selecting something in my scripts if possible. It often confuses the user if the original selection changes. To get the same result as with selectHierarchy() I use

``````geo = pm.listRelatives(selection, ad=True, type="mesh")
``````

#14

Oh, yes, for me this was also weak place, thanks Haggi, will try to fix it.

Also, one more script for shader IDs:

``````
# 256 pipeline tools
# if no shading group selected AOVs created for all shading groups

import maya.cmds as cmds
import mtoa.aovs as aovs
import pymel.core as pm
import sys

#define RGB colors
R = ( 1, 0, 0 )
G = ( 0, 1, 0 )
B = ( 0, 0, 1 )

def checkShdrID(*args):#check existing ids to create proper nubers for next ids

exist = pm.ls(sl = True)[-1]
exist = float(exist.split('_')[2]) + 1

else:
if pm.objExists('aiAOV_*'):
pm.select('aiAOV_*')
exist = pm.ls(sl = 1)
exist = len(exist)

else:
exist = 0
return exist
#checkShdrID()

def addObjAOV(index): # Creates AOV render pass
return aovName

shdrIDMatR.color.set(R)
shdrIDMatR.color.set(G)
shdrIDMatR.color.set(B)

def iterBy( iterable, count ):#Hamish McKenzie procedure to process list in blocks of N elrmrnts
cur = 0
i = iter( iterable )
while True:
try:
toYield = []
for n in range( count ): toYield.append( i.next() )
yield toYield
except StopIteration:
if toYield: yield toYield
break

# check if any Shading Group selected or not
shdrs = pm.ls(sl = 1)
if shdrs:
else:
confirm = pm.confirmDialog ( title='WARNING', message='No shading group selected, create IDs for all shades in scene?', button=['Yes','No'], defaultButton='Yes', cancelButton='No', dismissString='No' )
if confirm == 'Yes':
shdrs.remove('initialParticleSE')
else:
sys.exit()

print shdrs

index =  checkShdrID()
for i in iterBy(shdrs,3):

#create and plug R
SHD.outColor >> i[0].aiCustomAOVs[index].aovInput;
#create and plug G
if i[1]:
SHD.outColor >> i[1].aiCustomAOVs[index].aovInput;

#create and plug B
if i[2]:
SHD.outColor >> i[2].aiCustomAOVs[index].aovInput;

index +=1

``````

#15

Well, not a big surprize that shader ID script works only in ideal conditions.
In general script create Red, Green, Blue shaders, AOVs and connect shader to AOVs input attributes.

This is part, where connection is done:

``````SHD.outColor >> i[0].aiCustomAOVs[index].aovInput;
``````

Result: id_R_00.outColor aiStandard1SG.aiCustomAOVs[0].aovInput;

This index in aiCustomAOVs[index] is serial number of created AOV.
So, if we create some AOV in scene before running script, the index of first AOV is not 0 and connections mess up. I`m doing check of existing AOVs and correct index according to this, but anyway sooner or later after several script execution and AOV operation index will mess.

Im stuck. May be somebody has solved same issue with this?

#16

Well… list comprehensions is also way to go?

``````nums = [1,2,3]
fruit = ["Apples", "Peaches", "Pears", "Bananas"]
print [(i,f) for i in nums for f in fruit]
``````

[(1, ‘Apples’), (1, ‘Peaches’), (1, ‘Pears’), (1, ‘Bananas’),
(2, ‘Apples’), (2, ‘Peaches’), (2, ‘Pears’), (2, ‘Bananas’),
(3, ‘Apples’), (3, ‘Peaches’), (3, ‘Pears’), (3, ‘Bananas’)]