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

__all__ = ['AssignPriorityCustom', 'instantiate']

from awSupportApi        import ApplicationMode
from MessageInterpreter  import MessageInterpreter
from MessageRegistry     import theMessageRegistry
from Modes               import SelectionMode
from RTFapi              import LodGroup
from SceneGraphUtilities import CastToOriginalType, \
                                CollectLeafNodes, \
                                GetNodesFromIds
from UserCustomization   import UserCustomBase, CustomInfo

import ModelIO


############################################
# Register a new message
############################################

SET_PRIORITY_Doc = \
[(
"""
Set object priority
"""
),
[[("ids"),("List of nodes for which to set the priority.")],
 [("priority"),("A number from 1-4.")]
 ]
]

theMessageRegistry.register("SET_PRIORITY", (tuple,int), 0, SET_PRIORITY_Doc)

class AssignPriorityCustom (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.__myPriorityLowId = menu.appendItem(_( 'Assign Priority: Low' ), self.__onLow )
            self.__myPriorityMediumId = menu.appendItem(_( 'Assign Priority: Medium' ), self.__onMedium )
            self.__myPriorityHighId = menu.appendItem(_( 'Assign Priority: High' ), self.__onHigh )

        if "Edit" == id:
            menu.AppendSeparator()
            self.__mySelectLowId = menu.appendItem(_( 'Select Priority: Low' ), self.__onSelectLow )
            self.__mySelectMediumId = menu.appendItem(_( 'Select Priority: Medium' ), self.__onSelectMedium )
            self.__mySelectHighId = menu.appendItem(_( 'Select Priority: High' ), self.__onSelectHigh )
            self.__mySelectUndefinedId = menu.appendItem(_( 'Select Priority: Undefined' ), self.__onSelectUndefined )

    def enableMenuStates(self,id,enableStates):
        if "Appearance" == id:
            enabled = (self.__myInterpreter.Enabled() if self.__myInterpreter else False)

            enableStates[self.__myPriorityLowId] = enabled
            enableStates[self.__myPriorityMediumId] = enabled
            enableStates[self.__myPriorityHighId] = enabled

        if "Edit" == id:
            enabled = (self.__myInterpreter.SelectEnabled() if self.__myInterpreter else False)

            enableStates[self.__mySelectLowId] = enabled
            enableStates[self.__mySelectMediumId] = enabled
            enableStates[self.__mySelectHighId] = enabled

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

    def __assignPriority(self, priority):
        if self.__myInterpreter:
            self.__myInterpreter.AssignPriority(priority)

    def __selectPriority(self, priority):
        if self.__myInterpreter:
            self.__myInterpreter.SelectPriority(priority)

    def __onLow( self, event ):
        self.__assignPriority(LodGroup.kLow)

    def __onMedium( self, event ):
        self.__assignPriority(LodGroup.kMedium)

    def __onHigh( self, event ):
        self.__assignPriority(LodGroup.kHigh)

    def __onSelectLow( self, event ):
        self.__selectPriority(LodGroup.kLow)

    def __onSelectMedium( self, event ):
        self.__selectPriority(LodGroup.kMedium)

    def __onSelectHigh( self, event ):
        self.__selectPriority(LodGroup.kHigh)

    def __onSelectUndefined( self, event ):
        self.__selectPriority(LodGroup.kUndefined)

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

class LocalInterpreter( MessageInterpreter ):

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

    def Enabled( self ):
        return ApplicationMode.instance().isAuthoringMode() \
           and self.__myModels is not None \
           and not self.__myModels.selected.empty()

    def SelectEnabled( self ):
        return ApplicationMode.instance().isAuthoringMode() \
           and self.__myModels is not None 

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

    def SET_DOCUMENT( self, message ):
        document, = message.data
        self.__myModels = document.get( ModelIO.id )

    def AssignPriority( self, priority ):
        if self.__myModels is not None:
            isLodNode = [lambda node: node.getNodeType() in ['LodGroup','LodCollapse']]
            lodNodeIds = []
            for node in GetNodesFromIds(self.__myModels.selected):
                lodNodeIds.extend( CollectLeafNodes( node, isLodNode, idsOnly=True ) )
            self.sendMessage( "SET_PRIORITY", ( tuple(lodNodeIds), priority ) )

    def SelectPriority( self, priority ):
        if self.__myModels is not None:
            lodPriority = [lambda node: node.getNodeType() in ['LodCollapse','LodGroup'] and node.getPriority()==priority ]
            lodNodeIds  = CollectLeafNodes( self.__myModels.root, lodPriority, idsOnly=True )
            self.sendMessage( "SELECT", ( tuple(lodNodeIds), SelectionMode.kReplace ) )

    def SET_PRIORITY( self, message ):
        """
        Set priority of giving objects to a given value.
        """
        ( targetIds, priority ) = message.data
        if 0 == len(targetIds):
            return

        isLodNode = [lambda node: node.getNodeType() in ['LodGroup','LodCollapse']]
        for node in GetNodesFromIds( targetIds, isLodNode ):
            lodNode = CastToOriginalType( node )
            lodNode.setPriority( priority )

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

def instantiate():
    return AssignPriorityCustom()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '1.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Assign and select by object priorities."
    customInfo.longInfo = \
"""A sample add-in to assign priority to an object and to select objects with a given priority.
"""
    return customInfo