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
  how to call an object in another function in Maya bstout 0 416 Apr-05-2021, 07:12 PM
Last Post: bstout
  PySide2 Hotkey works only once in Maya LalaGhost 0 562 May-19-2020, 05:05 PM
Last Post: LalaGhost
  How to clean session mqtt SayHiii 0 768 Dec-09-2019, 07:56 AM
Last Post: SayHiii
  run into issues while configuring visual-studio code on a win 7 machine. apollo 3 1,734 Sep-12-2019, 12:25 AM
Last Post: snippsat
  how to clean up unstarted processes? Skaperen 2 868 Aug-27-2019, 05:37 AM
Last Post: Skaperen
  String slicing in python from reverse ift38375 1 1,096 Apr-29-2019, 06:58 AM
Last Post: perfringo
  Slicing Python list of strings into individual characters Drone4four 5 1,633 Apr-17-2019, 07:22 AM
Last Post: perfringo
  sched.scheduler -> clean denisit 1 1,534 Nov-28-2018, 09:52 AM
Last Post: Gribouillis
  clean script by code fen1c5 8 2,295 Oct-16-2018, 05:11 AM
Last Post: volcano63
  clean up list elements and replace metalray 7 2,343 Aug-30-2018, 08:13 AM
Last Post: metalray

Forum Jump:

User Panel Messages

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