Home Technical Talk

[Maya][Python] Logic Revision in a Display Toggle Script

polycounter
Offline / Send Message
Justo polycounter
I wanted to see how to shorten some of these expressions if possible as I'm still learning...It´s a script that'll toggle meshes between High Quality Display and Low Quality Display. Some of my bigger concerns are three:

1-To set something to HQ, I believe the only flag needed is -polygonObject, even though in the Script Editor when you press the default shortcut it also changes -divisionU, -divisionV, -pointsWire and -pointsShaded.  I didn't really find a use for these...
2-Some objects for some reason will sometimes say their polygonObject (PO) flag value is 0 and others 1 (whyyyy), thus I had to include this in the script with an or.
3-I wanted the script to set objects back to PO=1 ONLY if all objects were in High Quality first. So I made a for expression first that would check this before assigning values, and created two boolean variables to act as switches. Can this be shortened, somehow?

<div>isLow  = False
isHigh = False
sel = cmds.ls(sl=True)


#Check in the selection if there are HQ and LQ displays mixed 
for obj in sel:
    cmds.select(obj, r=True)  #Replace selection to be examined
    getPOValue = cmds.displaySmoothness( q=1, po=1 )  #Get flag value from the selection
    if  getPOValue == [3L]:
        isHigh = True      
    if  getPOValue ==  [1L] or getPOValue ==  [0L]:
        isLow = True 


#Assign PO Values
for obj in sel:
    cmds.select(obj, r=True)
    getPOValue = cmds.displaySmoothness( q=1, po=1 )
    if  getPOValue ==  [1L] or getPOValue ==  [0L]:
        cmds.displaySmoothness( po=3 )
    else:  
        #Only set everything to LQ if everything is HQ first
        if isHigh == True and isLow == False:
            cmds.displaySmoothness( po=1 )
        
cmds.select(sel, r=True) #Return to the initial selection<br></div>

Anyway, thanks to anyone who takes the time to read this :) !

Replies

  • throttlekitty
    Options
    Offline / Send Message
    1) Those options are all for nurbs objects, the hotkey is designed as a catch-all. So if you're only going to be using polygons, you don't need to worry about it.

    2) I'm not sure about polygonobject = 0, I thought maybe it was some legacy thing or used in subdiv surfaces, but a fresh primitive or import return 0 until I press 1. I'd go with the assumption that on a polygon object that 0 and 1 are the same thing. Checking for getPOValue <=  [1L] will simplify looking for both 0 and 1.

    3) I'm no grand scripter, but that seems like the best way to go, it's a fairly quick way to sort through the two possible sets of numbers in a list and do something with them in turn. I could think of less elegant ways to do that with the sort command. :)
  • RN
    Options
    Offline / Send Message
    RN sublime tool
    Justo said:
    3-I wanted the script to set objects back to PO=1 ONLY if all objects were in High Quality first. So I made a for expression first that would check this before assigning values, and created two boolean variables to act as switches. Can this be shortened, somehow?
    The thing with toggling is that if the selected objects have different modes then it's best to set everyone to the same mode first, and then only toggle when all objects have the same mode. This is how most software do it.
    Also, displaySmoothness seems to take an object list as parameter, and return lists as well, maybe you can use that to avoid loops? (I'm asking because the docs don't show an example of it. Someone with Maya would need to test it).

    If it does accept and return object lists, then you could do something like this:
    selObjects = cmds.ls(sl=True)
    poValues = cmds.displaySmoothness(query=True, polygonObject=True)
    
    lowPO = []
    highPO = []
    for index, v in enumerate(poValues):
        if v <= 1:
            lowPO.append( selObjects[index] )
        else:
            highPO.append( selObjects[index] )
    
    if lowPO and highPO:
        # If there's mixed display smoothnesses, set everyone to low.
        cmds.displaySmoothness(sel, polygonObject=1)
    else:
        if lowPO:
            cmds.displaySmoothness(lowPO, polygonObject=3)
        if highPO:
            cmds.displaySmoothness(highPO, polygonObject=1)

    PS: Gotta love the Maya docs, "displaySmoothness is undoable (...). At present, this command is NOT un-doable." Wat
  • Justo
    Options
    Offline / Send Message
    Justo polycounter
    @throttlekitty Thank you for the answers man :) You're totally right with using the getPOValue <=  [1L] expression too!

    @RN
    RN said:
    The thing with toggling is that if the selected objects have different modes then it's best to set everyone to the same mode first, and then only toggle when all objects have the same mode. This is how most software do it.

     And that's the exact behaviour I'm doing here, no?

    Also it's the first time I see a for expression using that kind of syntax, I'll have to read a bit to understand that haha...Thanks for showing me more ways, doable or not, of doing this. And yeah, Autodesk docs in general can be frustrating... 

  • RN
    Options
    Offline / Send Message
    RN sublime tool
    In the code you posted, if objects have po=3 then they will only be toggled to 1 if no other object is 1.
    Let me know if there's something you didn't understand. The strangest thing might be the enumerate function, which returns a tuple (a read-only array) that is being unpacked into the 'index, v' variables (aka multiple assignment, the values in the tuple are being orderly assigned to the variables in that comma list).
Sign In or Register to comment.