Handling Messages (Message Interpreters)

 
 
 

Add-ins you create to handle custom messages need to return a custom message interpreter in an override of the function UserCustomBase.getInterpreter(). The message interpreter processes messages sent by Showcase and/or other add-ins. This is a Python class derived from MessageInterpreter.MessageInterpreter.

Your add-in will return a message interpreter. Each message handled by the interpreter must have a method in the interpreter of the form:

def MESSAGE_NAME( self, message ):
    (messageArg1, messageArg2, ...) = message.data

The following MessageInterpreter.MessageInterpreter implementation was taken from BackgroundCustom.py

from MessageInterpreter import MessageInterpreter
# ...

class LocalInterpreter( MessageInterpreter ):

   def __init__( self ):
      MessageInterpreter.__init__( self )
      self.__myDisplay = None

   def SET_DISPLAY(self, message):
      self.__myDisplay, = message.data

   def SET_BACKGROUND_COLOR( self, message ):
      if self.__myDisplay is None:
         return
      (r,g,b,) = message.data
      self.__myDisplay.setBackgroundColor( r, g, b, 0.0 )

   def Size( self, w, h ):
      self.sendMessage( "WINDOW_SET_SIZE", (800, 600 ) )

   def Background( self, r, g, b ):
      # Change these values if you want a different background colour
      self.sendMessage( "SET_BACKGROUND_COLOR", (r, g, b) )

   def BackgroundDefault( self, event ):
      # Change these values if you want a different background colour
      self.sendMessage( "RESET_DISPLAY_BACKGROUND_COLOR", () )

BackgroundCustom.py

Here is the BackgroundCustom.py source code for reference.

##
## (C) Copyright 2008 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.
##

# Change the background color from the usual blue, to the specified value
# 

__all__ = ['BackgroundCustom', 'instantiate']

from MessageRegistry     import theMessageRegistry
from MessageInterpreter  import MessageInterpreter
from UserCustomization   import UserCustomBase, CustomInfo

import os

SET_BACKGROUND_COLOR_Doc = \
[(
"""
There is already a SET_DISPLAY_BACKGROUND_COLOR message
with the same syntax as this one, but this is an example
of how you would implement something like that.
"""
),
[[("r"),("Red for the background color (in 0 - 1 range)")],
 [("g"),("Green for the background color (in 0 - 1 range)")],
 [("b"),("Blue for the background color (in 0 - 1 range)")],
 ]
]

theMessageRegistry.register("SET_BACKGROUND_COLOR",
  (float,float,float,), 0, SET_BACKGROUND_COLOR_Doc)


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

class BackgroundCustom (UserCustomBase):
  def __init__(self):
    self.__myCallMenu = None
    self.__myCallIdBlack = None
    self.__myCallIdWhite = None
    self.__myCallIdDef = None
    self.__myCall800x600 = None
    self.__myInterpreter = LocalInterpreter()
    bcol = os.environ.get( 'SHOWCASE_BACKGROUND_COLOR' )

    if bcol is not None:
      bcolor = bcol.split()
      if len(bcolor) == 3:
        try:
          r = float(bcolor[0])
          g = float(bcolor[1])
          b = float(bcolor[2])
          self.__myInterpreter.Background(r, g, b)
        except:
          print _("Invalid background color %s") % (bcol)


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


  def appendMenuItems(self,id,menu):
    if "View" == id:
      menu.appendSeparator()
      self.__myCallMenu = menu
      self.__myCallIdDef = menu.appendItem(_( 'Default background' ),
                         self.__onBackgroundDefault )
      self.__myCallIdBlack = menu.appendItem(_( 'Black background' ),
                           self.__onBackgroundBlack )
      self.__myCallIdWhite = menu.appendItem(_( 'White background' ),
                           self.__onBackgroundWhite )
      self.__myCall800x600 = menu.appendItem(_( 'Set size to 800x600'),
                           self.__onSize800x600)


  def enableMenuStates(self,id,enableStates):
    if "View" == id:
      enableStates[self.__myCallIdDef] = True
      enableStates[self.__myCallIdBlack] = True
      enableStates[self.__myCallIdWhite] = True
      enableStates[self.__myCall800x600] = True

  # ------------------------------------------------
  def __onBackgroundDefault( self, event ):
    self.__myInterpreter.BackgroundDefault( event )

  def __onBackgroundBlack( self, event ):
    self.__myInterpreter.Background( 0.0, 0.0, 0.0 )

  def __onBackgroundWhite( self, event ):
    self.__myInterpreter.Background( 1.0, 1.0, 1.0 )

  def __onSize800x600( self, event ):
    self.__myInterpreter.Size( 800, 600 )


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

class LocalInterpreter( MessageInterpreter ):

  def __init__( self ):
    MessageInterpreter.__init__( self )
    self.__myDisplay = None

  def SET_DISPLAY(self, message):
    self.__myDisplay, = message.data

  def SET_BACKGROUND_COLOR( self, message ):
    if self.__myDisplay is None:
      return
    (r,g,b,) = message.data
    self.__myDisplay.setBackgroundColor( r, g, b, 0.0 )

  def Size( self, w, h ):
    self.sendMessage( "WINDOW_SET_SIZE", (800, 600 ) )

  def Background( self, r, g, b ):
    # Change these values if you want a different background colour
    self.sendMessage( "SET_BACKGROUND_COLOR", (r, g, b) )

  def BackgroundDefault( self, event ):
    # Change these values if you want a different background colour
    self.sendMessage( "RESET_DISPLAY_BACKGROUND_COLOR", () )


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

def instantiate():
  return BackgroundCustom()


def info():
  customInfo = CustomInfo()
  customInfo.vendor = 'Autodesk'
  customInfo.version = '2.0'
  customInfo.api = '2008'
  customInfo.shortInfo = "Set or preset the window background color or size."
  customInfo.longInfo = \
"""An example plug-in as to how to set the window background color, or size. Menu items that \
set the background color to white, black and default have been created in the View menu. \
If the environment variable SHOWCASE_BACKGROUND_COLOR is set to three space separated values, \
the background will be set on startup.  Additionally, two menu items are created, also in \
the View menu, to set the window size to 800x600 and 1024x768.  This plug-in can be edited \
to allow more choices.
"""
  return customInfo