Interactive/CallScriptCustom.py

##
# (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.
##

# This is an example of a custom add-in that calls a batch script defined
# in the same file.

__all__ = ['CallScriptCustom', 'instantiate']

from ActionRegistry      import theActionRegistry
from MessageInterpreter  import MessageInterpreter
from RTFapi              import Event
from ScriptRunner        import ScriptRunner
from UserCustomization   import UserCustomBase, CustomInfo


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

class SynchronizedScript( ScriptRunner ):
    def __init__( self, scriptName, args ):
        ScriptRunner.__init__( self, scriptName, args )

    def Main( self ):
        self.sendMessageSync( 'STATUSBAR_SET_TEXT',
                              (("Example Script"),))

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

class CallScriptBatch:
    def instantiate( self, scriptName, args ):
        return SynchronizedScript( scriptName, args )

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

class CallScriptCustom (UserCustomBase):
    def __init__(self):
        self.__myCallMenu = None
        self.__myCallId = None
        self.__myInterpreter = LocalInterpreter()

    def getInterpreter(self,isInteractive):
        if isInteractive:
            return self.__myInterpreter
        return None

    def appendMenuItems(self,id,menu):
        if "Edit" == id:
            self.__myCallMenu = menu
            self.__myCallId = menu.appendItem(_( 'Call Script\tQ' ),
                                              self.__onCallScript )

            # While at it, add the hotkey...
            theActionRegistry.register( 'Global',
                                        Event.BUTTON_q, (), True,
                                        Event.BUTTONUP,
                                        'CallScript' )
            theActionRegistry.register( 'Spectating',
                                        Event.BUTTON_q, (), True,
                                        Event.BUTTONUP,
                                        'CallScript' )

    def enableMenuStates(self,id,enableStates):
        if "Edit" == id:
            enableStates[self.__myCallId] = True

    # ------------------------------------------------
    def __onCallScript( self, event ):
        self.__myInterpreter.CallScript( event )


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

class LocalInterpreter( MessageInterpreter ):

    def __init__( self ):
        MessageInterpreter.__init__( self )

    def CallScript( self, event ):
        module = CallScriptBatch()
        self.sendMessage( "EXECUTE_MODULE", ("Doc", module) )



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

def instantiate():
    return CallScriptCustom()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '2.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "A sample add-in to call a batch script."
    customInfo.longInfo = \
"""To go with the addition of EXECUTE_MODULE, the sample add-in allows us to call a batch \
script with blocking message processing.
"""
    return customInfo