Interactive/EchoMessagesCustom.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.

# You will need Autodesk Showcase 2007 (version 2007.11 or later).
#
 
#

__all__ = ['EchoMessagesCustom', 'instantiate']

from UserCustomization  import UserCustomBase, CustomInfo
from MessageInterpreter import MessageInterpreter
from GenericPopupMenu   import GenericPopupMenuItem
from ListUi             import ListUiGroup, ListUiItem
from MaterialList       import MaterialListUiItem
from MessageRegistry    import theMessageRegistry

SOME_CUSTOM_THING_Doc = \
[(
"""
Print a "Callback..." string which is related to material to the console.
"""
),
[[("item"),("Menu item of Material.")]
 ]
]

theMessageRegistry.register("SOME_CUSTOM_THING",
    (ListUiItem,), 0, SOME_CUSTOM_THING_Doc)

SOME_ENVIRONMENT_THING_Doc = \
[(
"""
Print a "Callback..." string which is related to environment to the console.
"""
),
[[("item"),("Menu item of Environment.")]
 ]
]

theMessageRegistry.register("SOME_ENVIRONMENT_THING",
    (ListUiItem,), 0, SOME_ENVIRONMENT_THING_Doc)

class EchoMessages( UserCustomBase ):
    def __init__( self ):
        #UserCustomBase.__init__(self)
        self.__myInterpreter = EchoInterpreter()

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

    def appendPopupMenuItems( self, id, popupMenu, item ):
        if "MaterialSelector" == id:
            menuItem = GenericPopupMenuItem(_( 'Some custom thing'),
                                            self.__materialSelector,
                                            item )
            popupMenu.append( menuItem )
        elif "EnvSceneSelector" == id or "EnvLibrarySelector" == id:
            menuItem = GenericPopupMenuItem(_( 'Some environment thing'),
                                            self.__environmentSelector,
                                            item )
            popupMenu.append( menuItem )

    def __materialSelector( self, item ):
        if self.__myInterpreter:
            self.__myInterpreter.OnMaterialSelector( item )

    def __environmentSelector( self, item ):
        if self.__myInterpreter:
            self.__myInterpreter.OnEnvironmentSelector( item )


def instantiate():
    return EchoMessages()


class EchoInterpreter( MessageInterpreter ):
    def wantsMessage( self, message ):
        # Skip most of them...
        return not("FRAME_"                     in message.id \
                or "NAVIGATION_"                in message.id \
                or "APPLICATION_"               in message.id \
                or "VIEWER_"                    in message.id \
                or "DISPLAY"                    in message.id \
                or "HOST"                       in message.id \
                or "PORT"                       in message.id \
                or "SET_"                       in message.id \
                or "SHADOW_LIGHT"               in message.id \
                or "BOOKMARK"                   in message.id \
                or "TEST"                       in message.id \
                or "REFRESH"                    == message.id \
                or "STARTUP"                    == message.id \
                or "MODEL_IMPORT_STATE_CHANGED" == message.id \
                or "MATERIAL_UI_UPDATE"         == message.id \
                or "UI_TOOLTIP"                 == message.id)

    def processMessage( self, message ):
        print "%s [%s]" % ( message.id, message.data )
        return MessageInterpreter.processMessage(self, message)

    def OnMaterialSelector( self, item ):
        if isinstance( item, ListUiGroup ):
            print "Callback on the material tab %s" % item.name()
        elif isinstance( item, MaterialListUiItem ):
            print "Callback on the scene material %s" % item.name()
        else:
            print "Callback on the library material %s" % item.name()

    def OnEnvironmentSelector( self, item ):
        if isinstance( item, ListUiGroup ):
            print "Callback on the environment tab %s" % item.name()
        elif isinstance( item, ListUiItem ):
            print "Callback on the scene environment %s" % item.name()
        else:
            print "Callback on some environment thing %s" % item.name()

    def SOME_CUSTOM_THING( self, message ):
        ( item, ) = message.data
        self.OnMaterialSelector( item )

    def SOME_ENVIRONMENT_THING( self, message ):
        ( item, ) = message.data
        self.OnEnvironmentSelector( item )


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '3.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Echo the messages to stdout."
    customInfo.longInfo = \
"""A sample add-in to echo most of the messages to stdout.
"""
    return customInfo