pythonmayamel

Toggle X-Ray Mode in Maya using Python


I'm trying to bind some Python code to a key so I can toggle X-Ray mode in Maya.

One thing that's confusing me is that when I run this line of code...

def xrayQuery():
    cmds.modelEditor('modelPanel4', q=True, xr=True)

xrayQuery()

no result is returned, even though I've queried xray mode. But when I run just the command without the function...

cmds.modelEditor('modelPanel4', q=True, xr=True)

I get what I expected the first time, which is a boolean result based on whether or not xray mode is enabled. Can anyone tell me why this is?

I'm very new to python inside Maya, so any help will be much appreciated! Thanks!


Solution

  • You need to call return if you want the user-defined function to return the same output as called inside. Like below:

    def xrayQuery():
        return cmds.modelEditor('modelPanel4', q=True, xr=True)
    

    On a side note, if you could explain the purpose to write a function instead of calling the original function, it would be helpful to understand the use-case