Last active
April 9, 2017 18:24
-
-
Save JFlynnXYZ/c9e03706fb91b22280fc346f9e49e194 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""Hello World ToolBar UI for use in Maya Plugins | |
This module demonstrates a method of tracking instances used in Maya Plugins. | |
This method stores all instances that the plugin creates in the base class and | |
uses static methods to delete all of those instances. | |
Example: | |
If you wish to test this code without using the plugin simple execute the | |
same code shown below: | |
>>> HelloWorldToolBar.mayaCreation() # Creates the UI | |
>>> HelloWorldToolBar.mayaDeletion() # Deletes the UI | |
.. _Registering UI to Maya Python plugins: | |
http://jfdesigner.co.uk/2017/04/09/register-ui-maya-python-plugin/ | |
""" | |
from pymel.core.uitypes import PyUI | |
from pymel.core.windows import toolBar, window, formLayout, button, \ | |
confirmDialog | |
from pymel.core.system import warning | |
class HelloWorldToolBar(object): | |
"""The HelloWorldToolBar for our Maya Plugin | |
This class contains the implementation of our Hello World ToolBar. | |
Attributes: | |
_instances (set): All the instances of the HelloWorldToolBar | |
number (int, optional): The number of the toolbar to | |
easily differentiate them. | |
window (:obj: `pymel.core.uitypes.Window`): The window used by the | |
toolbar. | |
formLayout (:obj: `pymel.core.uitypes.FormLayout`): The form layout used | |
by the toolbar. | |
button (:obj: `pymel.core.uitypes.Button`): The button used to print out | |
the `displayString`. | |
toolBar (:obj: `PyUI`): The toolbar that is created in the Maya UI. | |
displayString(str): The string displayed by the button. | |
""" | |
_instances = set() | |
def __init__(self, number=-1): | |
"""Initializes the toolbar with default values. | |
The toolbar doesn't create any of the UI elements here in case we wished | |
to adapt this UI for batch processing in anyway. It also stores itself | |
in the base class when needed for deletion. | |
Args: | |
number (int, optional): The number of the toolbar to easily | |
differentiate them. | |
""" | |
HelloWorldToolBar._instances.add(self) | |
self.number = number | |
self.window = None | |
self.formLayout = None | |
self.button = None | |
self.toolBar = None | |
self.displayString = "Hello World: {}".format(self.number) | |
def __repr__(self): | |
return "<{}: {}>".format(self.__class__.__name__, self.toolBar) | |
def create(self): | |
"""Creates the UI elements for the toolbar. | |
We use the PyMel functions for creating the elements so that we can | |
track any name changes that might happen externally from this class. | |
Note: | |
The PyMel toolBar command doesn't return a PyUI child class but a | |
normal Maya string result. This is why we create our own PyUI class. | |
Returns: | |
self | |
""" | |
self.window = window(self.displayString) | |
self.formLayout = formLayout(parent=self.window) | |
self.button = button(self.displayString, parent=self.formLayout, | |
command=self.helloWorld) | |
self.toolBar = PyUI(toolBar(area='top', content=self.window, | |
allowedArea=("top", "bottom"))) | |
return self | |
def delete(self): | |
"""Deletes the UI elements of the toolbar. | |
Note: | |
Technically we could just delete the `toolBar` and this would delete | |
all children. However if another application decided to re-parent | |
the widgets we would have left over garbage. I've chosen to delete | |
all widgets individually for this reason. | |
""" | |
for widget in (self.button, self.formLayout, self.window, self.toolBar): | |
try: | |
widget.delete() | |
except RuntimeError: | |
pass | |
# Remove the instance from `_instances` | |
HelloWorldToolBar._discardInstance(self) | |
def helloWorld(self, checked): | |
"""Simply presents the `displayString`. | |
Prints and shows a dialog with the contents of the `displayString`. | |
Args: | |
checked (bool): Whether the button is checked or not. NOT USED. | |
Returns: | |
None | |
""" | |
print self.displayString | |
confirmDialog(message=self.displayString, title=self.displayString) | |
@staticmethod | |
def _discardInstance(inst): | |
"""Removes the specified instance from the class if present | |
Args: | |
inst (:obj: `HelloWorldToolBar`): The toolBar instance to remove | |
from the class | |
Returns: | |
None | |
""" | |
HelloWorldToolBar._instances.discard(inst) | |
@staticmethod | |
def deleteAllInstances(): | |
"""Deletes and discards all instances from the class | |
Returns: | |
None | |
""" | |
for inst in tuple(HelloWorldToolBar._instances): | |
inst.delete() | |
@staticmethod | |
def mayaDeletion(): | |
"""The function that is run when the plugin is unloaded. | |
Note: | |
This simply deletes all the instances of the UI but could contain | |
more code in advanced examples. | |
Returns: | |
None | |
""" | |
HelloWorldToolBar.deleteAllInstances() | |
@staticmethod | |
def mayaCreation(): | |
"""The function that is run when the plugin is loaded | |
This creates three toolbars for demoing the code. | |
Returns: | |
None | |
""" | |
for i in range(3): | |
HelloWorldToolBar(i).create() | |
if __name__ == "__main__": | |
# Create the new ui | |
HelloWorldToolBar.mayaCreation() | |
# Delete the new ui | |
HelloWorldToolBar.mayaDeletion() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""Hello World ToolBar Plugin | |
This module demonstrates how to register UI when using a Maya Plugin. | |
Example: | |
To use and load the plugin make sure that this file is in your | |
MAYA_PLUG_IN_PATH then do the following: | |
>>> import maya.cmds | |
>>> maya.cmds.loadPlugin("HelloWorldToolBarPlugin.py") | |
.. _Registering UI to Maya Python plugins: | |
http://jfdesigner.co.uk/2017/04/09/register-ui-maya-python-plugin/ | |
""" | |
import sys | |
import maya.OpenMayaMPx as OpenMayaMPx | |
from HelloWorldToolBar import HelloWorldToolBar | |
# Initialize the script plug-in | |
def initializePlugin(plugin): | |
pluginFn = OpenMayaMPx.MFnPlugin(plugin, "Jonathan Flynn", "1.0.0") | |
# Register the UI | |
try: | |
pluginFn.registerUI(HelloWorldToolBar.mayaCreation, | |
HelloWorldToolBar.mayaDeletion) | |
except: | |
sys.stderr.write("Failed to register UI: HelloWorldToolBar\n") | |
raise | |
# Uninitialize the script plug-in | |
def uninitializePlugin(plugin): | |
# Don't actually do anything when we uninitialize | |
# pluginFn = OpenMayaMPx.MFnPlugin(plugin) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment