Interactive/TemplatePlugin.py

################################################################
# Shows how to add a new menu item to the menu bar in Showcase 2013. 
# Also shows how to add a new menu.
################################################################

# (C) Copyright 2012 Autodesk, Inc.  All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk license
# agreement provided at the time of installation or download, or which
# otherwise accompanies this software in either electronic or hard copy
# form.

__all__ = ['TemplatePlugin', 'instantiate']

from ActionRegistry     import theActionRegistry
from Application         import theApplication
from GenericMenu         import GenericMenu
from MessageInterpreter  import MessageInterpreter
from UserCustomization   import UserCustomBase, CustomInfo, \
                                theUserCustomization
from RTFapi              import Event

kTemplateHotkey = 'TemplateHotkey'

keyMappings = (
  ( Event.BUTTON_w, (),  True, Event.BUTTONDOWN, 'onButtonPressHotkey' )
, ( Event.BUTTON_s, (), True, Event.BUTTONDOWN, 'onLocalMenuButtonPressHotkey' )
)
theActionRegistry.registerList( kTemplateHotkey, keyMappings )

class TemplatePlugin(UserCustomBase):
    def __init__(self):
        self.__myInterpreter = LocalInterpreter()
    
    def getInterpreter(self,isInteractive):
        return (self.__myInterpreter if isInteractive else None)
    
    def appendMenuItems(self,id,menu):
        if "Appearance" == id:
            menu.AppendSeparator()
            self.__myButtonExampleTemplate = menu.appendItem(_( 'Button Example Template' ), self.__onButtonPress )
        
        if "Local Menu" == id:
            menu.AppendSeparator()
            self.__myLocalMenuButtonExampleTemplate = menu.appendItem(_( 'LocalMenu Button Example Template' ), self.__onLocalMenuButtonPress )
    
    def enableMenuStates(self,id,enableStates):
        if "Appearance" == id:
            enabled = (self.__myInterpreter.Enabled() if self.__myInterpreter else False)
            
            enableStates[self.__myButtonExampleTemplate] = enabled
            
        if "Local Menu" == id:
            enabled = (self.__myInterpreter.Enabled() if self.__myInterpreter else False)
            
            enableStates[self.__myLocalMenuButtonExampleTemplate] = enabled
            
    # ------------------------------------------------
    
    def appendMenu(self, menuInterpreter):
        self.__myLocalMenu = menuInterpreter.appendMenu(LocalMenu)
        # This line ensures that menu items defined in other add-ins will
        # also be able to append to this menu ("LocalMenu").
        theUserCustomization.appendMenuItems( "Local Menu",self.__myLocalMenu )
        self.__myMenuInterpreter = menuInterpreter
        
    # ------------------------------------------------
        
    def __onButtonPress( self, event ):
        if self.__myInterpreter:
            self.__myInterpreter.onButtonPress(event)   
            
    # ------------------------------------------------
        
    def __onLocalMenuButtonPress( self, event ):
        if self.__myInterpreter:
            self.__myInterpreter.onLocalMenuButtonPress(event)  

# -----------------------------------------------------------------------

class LocalInterpreter(MessageInterpreter):
    def __init__( self ):
        MessageInterpreter.__init__( self )
        self.__myModels = None

    def Enabled( self ):
        """
        Condition under which the button is enabled. 
        """
        return True

    def DOCUMENT_LOADED( self, message ):
        self.__myModels = theApplication.getCurrentDocument().getModels()
        theActionRegistry.addSetToStack( kTemplateHotkey )
        theActionRegistry.activateSet( kTemplateHotkey, True )
        
    def APPLICATION_CLOSE_SCENE( self, message ): 
        self.__myModels = None
        theActionRegistry.activateSet( kTemplateHotkey, False )
        theActionRegistry.removeSetFromStack( kTemplateHotkey )

    def onButtonPress( self, event ):
        """
        Action associated with 'Button Example Template' menu item under Appearance menu
        """
        pass
    
    def onLocalMenuButtonPress( self, event ):
        """
        Action associated with 'Button Example Template' menu item under Appearance menu
        """
        pass
    
    def onButtonPressHotkey( self, event ):
        self.onButtonPress(event)   
    
    def onLocalMenuButtonPressHotkey( self, event ):
        self.onLocalMenuButtonPress(event)  

# --------------------------------------------------------------------

class LocalMenu(GenericMenu):
    def __init__(self, parentWindow):
        GenericMenu.__init__(self, parentWindow, menuId=u'LocalMenu')

    def getLabel(self):
        return 'Local Menu'

# --------------------------------------------------------------------

def instantiate():
    return TemplatePlugin()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '2.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Adding new menu item to menu bar. Also adding new menu."
    customInfo.longInfo = \
"""A sample add-in that demonstrates how a new menu item to menu bar is added. Also adding new menu.
"""
    return customInfo