2
\$\begingroup\$

I'd like to know any ways of shortening this UI window for Maya, at the moment, because I couldn't find very detailed documentation and not very skilled with python, I have just built this UI with the knowledge that I have already, I know it's not the best way of doing it (using empty text elements to fill gaps etc)

Can someone provide me with an example on how to shorten this code or structure it properly?

import maya.cmds as cmds
class randomScatter():
    def __init__(self):
        if cmds.window("randSelection", exists=1): cmds.deleteUI("randSelection")
        cmds.window("randSelection", t="Scatter Objects - Shannon Hochkins", w=405, sizeable=0)                
        cmds.rowColumnLayout(nc=3,cal=[(1,"right")], cw=[(1,100),(2,200),(3,105)])
        cmds.text(l="Prefix ")
        cmds.textField("prefix")
        cmds.text(l="")        
        cmds.text(l="Instanced object(s) ")
        cmds.textField("sourceObj", editable=0)      
        cmds.button("sourceButton", l="Select")
        cmds.text(l="Target ")
        cmds.textField("targetObj", editable=0)        
        cmds.button("targetButton", l="Select")
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.text(l='Copy Options ')
        cmds.radioButtonGrp('copyOptions', labelArray2=['Duplicate', 'Instance'], numberOfRadioButtons=2, sl=2)
        cmds.text(l="")
        cmds.text(l="Rotation Options ")
        cmds.radioButtonGrp('orientation', labelArray2=['Follow Normals', 'Keep original'], numberOfRadioButtons=2, sl=1)
        cmds.text(l="")
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.text(l="")
        cmds.checkBox('copyCheck', l='Stick copies to target mesh.')
        cmds.text(l="")
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.text('maxNumber', l="Copies ")      
        cmds.intFieldGrp("totalCopies", nf=1, v1=10, cw1=100)
        cmds.text(l="")
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.separator( height=20, style='in')
        cmds.text(l='')
        cmds.text(l=" From                          To", align='left')             
        cmds.text(l='')
        cmds.text(l="Random Rotation")
        cmds.floatFieldGrp("rotValues", nf=2, v1=0, v2=0, cw2=[100,100])
        cmds.text(l="")   
        cmds.text(l="Random Scale")
        cmds.floatFieldGrp("scaleValues", nf=2, v1=0, v2=0, cw2=[100,100])
        cmds.text(l="")   
        cmds.setParent("..")
        cmds.rowColumnLayout(w=405)        
        cmds.separator( height=20, style='in')
        cmds.text("Progress", height=20)
        cmds.progressBar('buildGridProgBar', maxValue=100, width=250)
        cmds.separator( height=20, style='in')
        cmds.button("excute", l="Scatter Objects",  w=403, al="right")
        cmds.showWindow("randSelection")
randomScatter()
\$\endgroup\$
1
  • \$\begingroup\$ You have alot of cmds.separator lines in a group of 3. Put them in for loops. Allmost everytime when the code looks repetetive, there can be used a loop to shorten it. \$\endgroup\$
    – dragons
    Sep 30, 2013 at 13:59

2 Answers 2

1
\$\begingroup\$

(1) You define random_scatter as a class but then call it like a function, which works but is confusing. With your programme as it currently stands, there is no reason for it to be a class, so better simply to define random_scatter as a function (use def random_scatter(): at the top).

(2) You can remove repetition and make the code more readable using functions and loops, e.g. write a function

def insert_seperators(number):
    for _ in range(number):
        cmds.separator(height=20, style='in')

then you can replace

    cmds.separator( height=20, style='in')
    cmds.separator( height=20, style='in')
    cmds.separator( height=20, style='in')

with

    insert_separators(3)

(3) If you have other functions doing similar things with cmds then try to see what they have in common and put that in a single function, which accepts parameters according to what changes.

\$\endgroup\$
0
\$\begingroup\$

It's not easy to answer this question, since:

  1. it applies to very specific Maya Embedded Language commands that aren't well documented,
  2. it's difficult to run the software ourselves.

Anyway, based on other GUIs I've worked with, you don't need to worry too much about the code quality. Popular frameworks usually provide... GUIs (eg. Qt Creator) that allow you to create place your elements, and then generate code that looks like yours (not always too good). If this is possible in Maya, go for it.

You can go with that approach yourself too: separate GUI code from everything else, and use parameters in __init__ for what is likely to change in the future.

I also recommand that you follow PEP 8, especially the section on whitespaces. This is a standard that a lot of Python developers follow: it makes it way easier to work with other Python developers that share this convention.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.