Interactive/MaterialTypeCustom.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 2009 or later.
#
 
#
# You will get a number of menu items on the material in scene elements.
# You can persistently choose what aspects of that material you want
# editable.

__all__ = ['MaterialTypeCustom', 'instantiate']

from GenericPopupMenu          import GenericPopupMenuItem
from MessageInterpreter        import MessageInterpreter
from MaterialList              import MaterialListUiItem
from ParameterConstant         import ParameterConstant as PC
from ParameterGroupUtilities   import ModifyParameter
from RTFapi                    import Parameter
from UserCustomization         import UserCustomBase, CustomInfo

import MaterialIO

class MaterialType( UserCustomBase ):
    def __init__( self ):
        self.__myInterpreter = LocalInterpreter()

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

    def appendPopupMenuItems( self, id, popupMenu, item ):
        if "MaterialSelector" == id:
            if isinstance( item, MaterialListUiItem ):
                menuItem = GenericPopupMenuItem(_( 'Edit As... Generic'),
                                                self.__setGeneric,
                                                item )
                popupMenu.append( menuItem )

                menuItem = GenericPopupMenuItem(_( 'Edit As... Original Type'),
                                                self.__setStandard,
                                                item )
                popupMenu.append( menuItem )

                menuItem = GenericPopupMenuItem(_( 'Edit As... No Parameters'),
                                                self.__setNoParameters, item )
                popupMenu.append( menuItem )
                menuItem = GenericPopupMenuItem(_( 'Edit As... Add Color'),
                                                self.__addDiffuse, item )
                popupMenu.append( menuItem )
                menuItem = GenericPopupMenuItem(_( 'Edit As... Add Highlight'),
                                                self.__addSpecular, item )
                popupMenu.append( menuItem )
                menuItem = GenericPopupMenuItem(_( 'Edit As... Add Clear Coat'),
                                                self.__addSpecular2, item )
                popupMenu.append( menuItem )
                menuItem = GenericPopupMenuItem(_( 'Edit As... Add Transparency'),
                                                self.__addTransparency, item )
                popupMenu.append( menuItem )
                menuItem = GenericPopupMenuItem(_( 'Edit As... Add Reflectivity'),
                                                self.__addReflect, item )
                popupMenu.append( menuItem )
                menuItem = GenericPopupMenuItem(_( 'Edit As... Add Bump'),
                                                self.__addBump, item )
                popupMenu.append( menuItem )

    def __setGeneric( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(), True, "" )

    def __setStandard( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(), True, None )

    def __addDiffuse( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  False, "diffuse" )

    def __addSpecular( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  False, "specular" )

    def __addSpecular2( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  False, "specular2" )

    def __addTransparency( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  False, "transparency" )

    def __addReflect( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  False, "reflect" )

    def __addBump( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  False, "bump" )

    def __setNoParameters( self, item ):
        if self.__myInterpreter is not None:
            self.__myInterpreter.changeToGeneric( item.name(),
                                                  True, "noParameters" )


def instantiate():
    return MaterialType()


class LocalInterpreter( MessageInterpreter ):

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

    def SET_DOCUMENT( self, message ):
        document, = message.data
        self.__myMaterials = document.get(MaterialIO.id)

    def APPLICATION_CLOSE_SCENE( self, message ):
        self.__myMaterials = None

    def changeToGeneric( self, name, reset, how ):
        # If how is None, remove the parameter
        # otherwise set the value to it, assuming
        # that it's a string.

        # If reset is False, unless how is None, append
        # how to the current value, rather than replacing it

        # Send the SELECTION_CHANGED message to trick UI
        # into rebuilding
        materials = self.__myMaterials
        result = False
        if materials is not None and name in materials:
            result = True
            material = materials[name]

            if how is not None:
                if not reset:
                    p = material.getParameterByName(PC.kParameterEditOverride)
                    if p:
                        current = p.getValueAsString()
                        if current != "" and current != "noParameters":
                            how = current + " " + how

                ModifyParameter( 
                    material, 
                    PC.kParameterEditOverride,
                    Parameter.kString,
                    str(how),
                    True 
                    )
                self.sendMessage( "SELECTION_CHANGED", () )
            else:
                p = material.getParameterByName( PC.kParameterEditOverride )
                if p:
                    material.removeParameter( p )
                    self.sendMessage( "SELECTION_CHANGED", () )

        return result



def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '1.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Override what is shown in the material editor."
    customInfo.longInfo = \
"""Add a number of menu items on the material in scene elements. You can persistently choose what \
aspects of that material you want editable in the material editor.
"""
    return customInfo