pythonarraysmayaautodesk

Maya Python Won't Accept Two Values as the Same?


Using Python in Autodesk Maya, I'm trying to implement a system that puts planes in predefined quadrants. All the quadrants have xyz coordinates that I'm storing inside of arrays. Using a for loop, I want to iteratively step through pos1 through pos4, but maya won't recognize (for example) "f'pos{i}'+'[0]" being the same as "pos1[0]".

import maya.cmds as cmds
import random as r

cmds.select(all=True)
cmds.delete()

xNeg = -6
xPos = 6
zNeg = -6
zPos = 6

pos1=[xNeg,0,zNeg]
pos2=[xPos,0,zNeg]
pos3=[xPos,0,zPos]
pos4=[xNeg,0,zPos]

def planes(planesNum):
    for i in range(1,planesNum):
        cmds.polyPlane(w=10,h=10,sw=5,sh=5,n=f'plane_{i}')
        #maya doesn't like the version below this line
        cmds.move(f'pos{i}'+'[0]',f'pos{i}'+'[1]',f'pos{i}'+'[2]', f'plane_{i}')
        #cmds.move(pos1[0],pos1[1],pos1[2]) - it likes this version
        print(f'pos{i}[0]')
        print(pos1[0])

planes(2)

You can see that the outputs of them are effectively the same; basically, I'm trying to output a variable name and have it recognize the value of the variable, if that makes sense? I'm very new to this, so I'd appreciate any help!


Solution

  • You could use a list-of-lists for the positions instead, so you don't need to do "variable variables".

    This will always generate up to 4 planes (and silently not generate any more) by slicing the positions list by the number of planes requested.

    from maya import cmds
    
    cmds.select(all=True)
    cmds.delete()
    
    xNeg = -6
    xPos = 6
    zNeg = -6
    zPos = 6
    
    positions = [
        [xNeg, 0, zNeg],
        [xPos, 0, zNeg],
        [xPos, 0, zPos],
        [xNeg, 0, zPos],
    ]
    
    
    def planes(planesNum):
        for i, pos in enumerate(positions[:planesNum]):
            cmds.polyPlane(w=10, h=10, sw=5, sh=5, n=f"plane_{i}")
            cmds.move(pos[0], pos[1], pos[2])
    
    
    planes(2)