Python Forum

Full Version: Code Clean-up and Maya/Python Slicing issues
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. First time posting in a forum and first time independently coding something, so if this is not the correct place please let me know.

I am currently working to create a plug-in for Maya which a part of it needs a cylinder to be generated, an edge loop beveled and moved down to create a platform. I have that code working, but it is long and repetitive.

import maya.cmds as cmds

def platform():
    cmds.polyCylinder()
    cmds.rename('pCylinder1','platform')
    cmds.setAttr('platform.scale', 5, .5, 5)
    cmds.setAttr('platform.translateY', -.5)

    cmds.polyBevel( 'platform.e[20:39]')
    cmds.polyMoveEdge('platform.e[21]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[26:27]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[30]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[33]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[36]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[39]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[42]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[45]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[48]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[51]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[54]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[57]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[60]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[63]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[66]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[69]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[72]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[75]', t=(0, -.1, 0))
    cmds.polyMoveEdge('platform.e[78]', t=(0, -.1, 0))
    
platform()
I would think that the long list of moveEdge commands can be condensed down into a list since its a range of numbers going by every third, but I can't seem to get the command to work. No matter if I attempt to write it either as the code below, or attempting to create a selection based on the edges, or any other weird combination I have attempted.

cmds.polyMoveEdge('platform.e[30:78:3]', t=(0, -.1, 0))
Clearly, I'm not doing slicing right but I can't seem to figure out how to do it properly.

The error if it helps:
Error:
# Error: TypeError: file <maya console> line 10: Object platform.e[30:78:3] is invalid #
My main questions are if there a way to condense down the polyMoveEdge command so that it isn't the same line repeated over and over again? Or is how it is written alright for such a short function?
range can give you the required numbers
for number in range(30, 81, 3):
    print(number)
Output:
30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78
Using an f string to replace the number in a loop
for number in range(30, 81, 3):
    cmds.polyMoveEdge(f'platform.e[{number}]', t=(0, -.1, 0))