Search code examples
pythonmaya

How to duplicate special multiple groups using python in maya


I am having a question for my college assignment, in which I am creating a plane pattern using polygons, and I successfully create one line of the pattern on the x-axis and group the objects in a group, and I duplicate the group one time on the z-axis. While I have two groups now, the part of my code of duplicating the groups looks like this:

cmds.select(all=True)
cmds.group(name='group#')
cmds.select('group1')
cmds.duplicate('group1')
cmds.move(0.9, 0, 1.6)
cmds.select('group1','group2')
cmds.group(name='group#')
cmds.select('group3')
cmds.duplicate('group3')
cmds.move( 0, 0, 3.2 )
cmds.duplicate( st=True )
cmds.duplicate( st=True )
cmds.duplicate( st=True )
cmds.duplicate( st=True )

'''

So how can I simplify this code by using a loop?


Solution

  • Based on your comment above, if we ignore the grouping you are doing and just focus on looping, you could do something like this to create a cube-grid of poly cubes:

    import maya.cmds as cmds
    
    iterations = 10
    distanceIncrement = 2
    
    for x in range(iterations-1):
        cube = cmds.polyCube()
        cmds.setAttr('{}.translate'.format(cube[0]), x*distanceIncrement, 0, 0)
        
        for y in range(iterations-1):
            cube = cmds.polyCube()
            cmds.setAttr('{}.translate'.format(cube[0]), x*distanceIncrement, y*distanceIncrement, 0)
            
            for z in range(iterations-1):
                cube = cmds.polyCube()
                cmds.setAttr('{}.translate'.format(cube[0]), x*distanceIncrement, y*distanceIncrement, z*distanceIncrement)
    

    You can use the same technique of nested loops to create a variety of patterns, so hopefully this lets you work towards a solution to your specific problem.