PDA

View Full Version : Problem removing plug-in UI when Maya exits


Mad Snowman
10-18-2010, 05:02 AM
I'm new to Maya plug-in development and have a question regarding UI cleanup. I'm writing a plug-in that creates a new shelf (with various shelfButtons attached to external scripts). When the plug-in loads and unloads, I want to add or remove this shelf from the Maya UI. This is working without any issue. However, I also want to clear my plug-in shelf when Maya exits (if the plug-in is set to Auto-Load it will simply load again on restart...if Auto-Load is not set, I don't want the shelf to appear when Maya starts).

This should be possible by registering a callback (OpenMaya.MSceneMessage.kMayaExiting) during the 'initializePlugin' method. The following code works without any issues if I attach the callback to a different scene message (such as kAfterSave), but when I use kMayaExiting it doesn't remove the shelf. Strangely, if I do a quick print statement (on exit) to test if the shelf has been removed it appears to have removed it...but when I restart Maya the shelf appears once again (without loading the plugin). I'm not completely sure about the print statement either because the output flies by pretty quickly when Maya exits (is it possible to log the ScriptEditor text to a file when exiting?). Thanks for any help! I've been banging my head against this issue all day. :-P


import maya
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx

fMayaExitingCB = None

# Remove the UI when Maya exits
def removeUI(clientData):
maya.cmds.setParent(maya.mel.eval("global string $gShelfTopLevel;$temp = $gShelfTopLevel"))
if maya.cmds.layout("TestPluginShelf", exists=True):
maya.cmds.deleteUI("TestPluginShelf", layout=True)


# Initialize the script plug-in
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject, "TestPlugin", "1.0", "Any")

# Create shelf and buttons
maya.cmds.setParent(maya.mel.eval("global string $gShelfTopLevel;$temp = $gShelfTopLevel"))

if maya.cmds.layout("TestPluginShelf", exists=True) == False:
maya.cmds.shelfLayout("GreenButton", style="textOnly")
maya.cmds.shelfButton(image1="scriptIcon.png",
label="script", annotation="externalScript",
command="import ExternalScript\nreload(ExternalScript)",
sourceType="python")

# Add callback to clean up UI when Maya exits
global fMayaExitingCB
fMayaExitingCB = OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kMayaExiting, removeUI)


# Uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)

# Cleanup the UI and remove the callback when plug-in is unloaded
maya.cmds.setParent(maya.mel.eval("global string $gShelfTopLevel;$temp = $gShelfTopLevel"))
if maya.cmds.layout("TestPluginShelf", exists=True):
maya.cmds.deleteUI("TestPluginShelf", layout=True)

global fMayaExitingCB
if (fMayaExitingCB != None):
OpenMaya.MSceneMessage.removeCallback(fMayaExitingCB)


Cheers,
Josiah

red_oddity
10-18-2010, 08:23 AM
Could it be that Maya saves the shelves in your preferences before it dispatches the kMayaExiting event message?

What happens when you disable the saving of preferences on exit in Maya? (make sure to have your shelves present when starting Maya, other wise his test is kinda moot :) )

Mad Snowman
10-18-2010, 09:34 PM
Thanks red_oddity! If I either set the Shelf Editor options to "Save Only On Request" or uncheck "Save all preferences and settings when Maya exits" in the general Save Actions settings then this works without a hitch.

There doesn't seem to be any other callback that would work in this situation. It definitely looks like the preferences are being saved before kMayaExiting is called. I suppose I could just leave it up to user preferences for now, though parsing and removing the appropriate lines from the userPrefs file is an option (I'd rather not touch that though).

Thanks for pointing that out!

HapG
05-04-2011, 07:55 PM
I think your problem may be related to your use of deleteUI instead of deleteShelfTab.

deleteUI only removes the shelfLayout UI element. When maya is relaunched, it will read/load the shelf_.mel file and create the shelfLayout again.

deleteShelfTab does a thorough removal (delete the shelfLayout, fix prefs, rename shelf_.mel to .deleted)

The only issue with deleteShelfTab is it pops a confirm dialog, which you may not want. In that case, you could just copy the relevant bits from that script.

anandvtra
06-08-2011, 05:24 AM
The only issue with deleteShelfTab is it pops a confirm dialog, which you may not want. In that case, you could just copy the relevant bits from that script.

Can u explain what u really meant with the above sentence "copy the relevant bits from that script" with a small example...I too confronted with this problem...

CGTalk Moderation
06-08-2011, 05:24 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.