Maya / Python noob question

Hello,

I’m trying to write a simple GUI to be able to have the option in Maya to use xray and wireframe on shaded only on a selected object.

I half of the script working (the xray part) but when I try to do the same thing with the wireframe on shaded its a no go. I spent half the day trying to figure it out via autodesk documentation but I’m finally reaching out for help. I get two different errors.

Error: modelEditor: Object ‘pCube1’ not found.

Traceback (most recent call last):

File “<maya console>”, line 18, in wos

RuntimeError: modelEditor: Object ‘pCube1’ not found.

and

xRayWireUI()

Error: ‘bool’ object is unsubscriptable

Traceback (most recent call last):

File “<maya console>”, line 19, in wos

TypeError: ‘bool’ object is unsubscriptable

Here is the script any help would be appreciated.

import maya.cmds as cmds

def xRayWireUI():
window_name = “xRayWIreUI”

if cmds.window(window_name, q=True, exists=True):
    cmds.deleteUI(window_name)

def xRay(*arg):
    selection = cmds.ls(sl=True)
    for selected in selection:
        xRay = cmds.displaySurface(selected, q=True, xRay=True)
        cmds.displaySurface(selected,xRay= not xRay[0])

def wos(*arg):
    selection = cmds.ls(sl=True)
    for selected in selection:
        wos = cmds.modelEditor(selected, q=True, wos=True)
        cmds.modelEditor(selected, wireframeOnShaded= not wos[0])

my_window = cmds.window(window_name, title="KMT_Toggle Xray / Wireframe on Shaded Tool", h=25, w=475)

cmds.columnLayout(parent=my_window, adj=True)
cmds.button(label="Toggle Xray", h=25, w=475, c=xRay)
cmds.button(label="Toggle Wireframe On Shaded", h=25, w=475, c=wos)
cmds.showWindow(my_window)

xRayWireUI()

You’re getting the first error (Object ‘pCube1’ not found) because the modelEditor command expects a model editor (e.g. ‘modelPanel4’), not an individual scene node. As far as I know, you can’t toggle wireframe-on-shaded on a per-object basis, at least not with the modelEditor command. Maybe somebody knows of a workaround, but I’m just going to assume it’s an all-or-nothing deal.

The second error message, incidentally, is because modelEditor returns a single bool when you query wireframeOnShaded, unlike displaySurface, which returns a list of bools when you query xRay. For xRay, you’re effectively calling [True][0] or [False][0], which gives you a bool, but for wos you’re calling True[0] or False[0], which doesn’t make any sense and so throws a TypeError. I don’t know how your code is even getting that far, but there you go.

Anyway, this is the best I can come up with for toggling wireframe on shaded, for the whole viewport:


import maya.cmds as mc

def ToggleWireframeOnShaded(panel = mc.getPanel(withLabel = 'Persp View')):
    '''
    Toggles wireframe-on-shaded viewing mode for the specified panel. If no
    panel is specified, defaults to the currently active panel.
    '''
    currentState = mc.modelEditor(panel, q = True, wireframeOnShaded = True)
    mc.modelEditor(panel, edit = True, wireframeOnShaded = not currentState)

ToggleWireframeOnShaded()

And here’s a slight variation on your XRay function that treats all the nodes as a group (i.e., it sets them all to the same XRay mode instead of toggling them individually):


import maya.cmds as mc

def ToggleXRay(nodes):
    '''
    Toggles XRay viewing mode for the given list of nodes. One call sets all
    nodes to the same state -- if none of them have XRay enabled, they'll all
    have XRay enabled. Otherwise, they'll all have XRay disabled.
    '''
    # Don't bother checking or setting anything if we don't have a valid list
    if not nodes:
        return
        
    # Query the current xray state of all objects
    xrayEnabled = [mc.displaySurface(n, q = True, xRay = True)[0] for n in nodes]

    # Set all nodes to the desired state
    desiredState = False if all(xrayEnabled) else True
    mc.displaySurface(nodes, xRay = desiredState)

ToggleXRay(mc.ls(selection = True))

This seems like the kind of thing that you’d just throw onto a shelf and/or bind to a hotkey, but creating the GUI is a nice exercise in any case. Since it’s been ages since I’ve done anything GUI-related with Maya, I decided to take a crack at it myself. Here’s a full script that includes both functions with a simple GUI: http://pastebin.com/Knd38YKB

Thank you this was very informative! I spent a lot of time trying to find solutions to this and as you said with the modelEditor I found other scripts that effected all objects in the scene (none on a per object basis) calling on a specific panel or all the panels at once and your script confirms that, which helps me understand this better. Also as you said, wos does only seem to effect everying thing (all or nothing) and havent found any other scripts to say otherwise so I may have to abandon this and try something else.

Thanks again!