Operation with list elements

 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA Operation with list elements 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`````` __________________ 3D animation blog share quote
 08 August 2014 Borgfriend portfolio Neal Bürger Ludwigs Maximilians Universität Munich, Germany 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`````` __________________ www.lost-triangle.com share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA >You probably should read up on the basics of Python: Well, since its 100% true your answer does not helps too much __________________ 3D animation blog Last edited by kiryha : 08 August 2014 at 01:30 PM. share quote
 08 August 2014 haggi Expert 3d freelancer 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 )`````` __________________ www.renderwiki.com - www.openmaya.net share quote
 08 August 2014 djx Where's my pony?   David Johnson vfx & creature td Melbourne, Australia 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 __________________ http://www.djx.com.au share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA 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]`````` __________________ 3D animation blog share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA 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) aovs.AOVInterface().addAOV( aovName ) return aovName def addColor(index): #create color attribute colorAttrName = 'mask_' + str(index) 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 mskObjectMat = pm.shadingNode('aiUserDataColor', asShader=True, name = 'catchMask_' + str('%02d' % index) ) mskObjectMat.colorAttrName.set('mask_' + str('%02d' % index)) 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) msk = addColor('%02d' %index) 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`````` __________________ 3D animation blog share quote
 08 August 2014 djx Where's my pony?   David Johnson vfx & creature td Melbourne, Australia ``````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 __________________ http://www.djx.com.au share quote
 08 August 2014 djx Where's my pony?   David Johnson vfx & creature td Melbourne, Australia 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 __________________ http://www.djx.com.au Last edited by djx : 08 August 2014 at 03:12 PM. share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA Thanks David, looks like what i need! Will try to implement this to my code __________________ 3D animation blog share quote
 08 August 2014 djx Where's my pony?   David Johnson vfx & creature td Melbourne, Australia 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 __________________ http://www.djx.com.au share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA 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) aovs.AOVInterface().addAOV( aovName ) return aovName def deleteAOV(*args):# delete AOVs and aiUserDataColor shaders pm.delete('aiAOV_objectID_*') pm.delete('catchMask_*') def addColor(index): #create color attribute colorAttrName = 'maskOBJ_' + str(index) 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 mskObjectMat = pm.shadingNode('aiUserDataColor', asShader=True, name = 'catchMask_' + str('%02d' % index) ) mskObjectMat.colorAttrName.set('maskOBJ_' + str('%02d' % index)) 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) msk = addColor('%02d' %index) 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) msk = addColor('%02d' %index) 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) msk = addColor('%02d' %index) 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...nder-notes.html __________________ 3D animation blog share quote
 08 August 2014 haggi Expert 3d freelancer 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")`` __________________ www.renderwiki.com - www.openmaya.net share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA 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 # create shader IDs for selected shading groups # 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 if pm.objExists('aiAOV_shaderID_*'): pm.select('aiAOV_shaderID_*') 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 aovName = 'shaderID_' + str(index) aovs.AOVInterface().addAOV( aovName ) return aovName # addObjAOV('%02d' % 0) def createShaderR(index): shdrIDMatR = pm.shadingNode('aiUtility', asShader=True, name = 'id_R_' + str(index)) shdrIDMatR.shadeMode.set(2) shdrIDMatR.color.set(R) def createShaderG(index): shdrIDMatR = pm.shadingNode('aiUtility', asShader=True, name = 'id_G_' + str(index)) shdrIDMatR.shadeMode.set(2) shdrIDMatR.color.set(G) def createShaderB(index): shdrIDMatR = pm.shadingNode('aiUtility', asShader=True, name = 'id_B_' + str(index)) shdrIDMatR.shadeMode.set(2) shdrIDMatR.color.set(B) #createShaderR('%02d' % 0) 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: print 'Shading groups selected manualy' 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 = pm.ls(type='shadingEngine') shdrs.remove('initialParticleSE') shdrs.remove('initialShadingGroup') else: sys.exit() print shdrs index = checkShdrID() for i in iterBy(shdrs,3): addObjAOV('%02d' %index) #create AOV #create and plug R createShaderR('%02d' %index) shader = 'id_R_%02d' %index SHD = pm.PyNode(shader) SHD.outColor >> i[0].aiCustomAOVs[index].aovInput; #create and plug G if i[1]: createShaderG('%02d' %index) shader = 'id_G_%02d' %index SHD = pm.PyNode(shader) SHD.outColor >> i[1].aiCustomAOVs[index].aovInput; #create and plug B if i[2]: createShaderB('%02d' %index) shader = 'id_B_%02d' %index SHD = pm.PyNode(shader) SHD.outColor >> i[2].aiCustomAOVs[index].aovInput; index +=1 `````` __________________ 3D animation blog Last edited by kiryha : 08 August 2014 at 04:00 PM. share quote
 08 August 2014 kiryha Expert   portfolio Kiryha Krysko 3D artist USA 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? __________________ 3D animation blog share quote

 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 vBulletinCopyright ©2000 - 2006, Jelsoft Enterprises Ltd.