Python Forum
Code Clean-up and Maya/Python Slicing issues
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code Clean-up and Maya/Python Slicing issues
#1
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?
Reply
#2
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can i clean this code ? BSDevo 8 941 Oct-28-2023, 05:50 PM
Last Post: BSDevo
  Clean Up Script rotw121 2 1,006 May-25-2022, 03:24 PM
Last Post: rotw121
Shocked Issues Installing Pyenv/Python 3.9.1 Brandon_Contactum 1 2,517 Feb-22-2022, 06:32 PM
Last Post: snippsat
  How to clean UART string Joni_Engr 4 2,477 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  how to call an object in another function in Maya bstout 0 2,077 Apr-05-2021, 07:12 PM
Last Post: bstout
  PySide2 Hotkey works only once in Maya LalaGhost 0 1,780 May-19-2020, 05:05 PM
Last Post: LalaGhost
  How to clean session mqtt SayHiii 0 1,997 Dec-09-2019, 07:56 AM
Last Post: SayHiii
  run into issues while configuring visual-studio code on a win 7 machine. apollo 3 3,661 Sep-12-2019, 12:25 AM
Last Post: snippsat
  how to clean up unstarted processes? Skaperen 2 2,230 Aug-27-2019, 05:37 AM
Last Post: Skaperen
  String slicing in python from reverse ift38375 1 2,394 Apr-29-2019, 06:58 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020