Command arguments notifier

From The Foundry MODO SDK wiki
Jump to: navigation, search

Description:

Demonstrates a custom command with arguments whose queriable values update in a form when the command is executed. To test, create a new form and add the following command strings as controls:

mm.valuetest mm.valuetest arg1:? mm.valuetest arg2:?

The first one adds the command to the form as a button, the second two add the commands arguments as values. Clicking the commands button will update the values of the arguments and update the form.

Code:

import lx
import lxifc
import lxu.command
 
# global variable. Stores the click count of the command button and is used to calculate
# the values of the command's "arguments" for this example.
count = 0.0
 
 
class MyTestNotifier(lxifc.Notifier, lxifc.CommandEvent):
    '''The notifier class.'''
    masterList = {}
 
    def noti_Name(self):
        return "my.notifier"
 
    def noti_AddClient(self,event):
        self.masterList[event.__peekobj__()] = event
 
    def noti_RemoveClient(self,event):
        del self.masterList[event.__peekobj__()]
 
    def Notify(self, flags):
        for event in self.masterList:
            evt = lx.object.CommandEvent(self.masterList[event])
            evt.Event(flags)
 
lx.bless(MyTestNotifier,"mm.notifier")
 
 
class CmdNotifiers(lxu.command.BasicHints):
    '''UIValueHints class to return my.notifier with no arguments so that the Form View 
    will refresh the view.
 
    '''
    def __init__(self):
        self._notifiers = [('mm.notifier','')]
 
 
class Command(lxu.command.BasicCommand):
    '''The custom Command class.'''
    def __init__(self):
        lxu.command.BasicCommand.__init__(self)
 
        # add a couple of arguments. These are the values that will display in the form.
        self.dyna_Add('arg1', lx.symbol.sTYPE_FLOAT)
        self.dyna_Add('arg2', lx.symbol.sTYPE_FLOAT)
 
        # they need to be both queriable and optional.
        self.basic_SetFlags(0, lx.symbol.fCMDARG_QUERY{{!}}lx.symbol.fCMDARG_OPTIONAL)
        self.basic_SetFlags(1, lx.symbol.fCMDARG_QUERY{{!}}lx.symbol.fCMDARG_OPTIONAL)
 
    def arg_UIValueHints(self, index):
        '''returns the UIValueHints class required to force a form update'''
        return CmdNotifiers()    
 
    def basic_Enable(self, msg):
        return 1
 
    def basic_Execute(self, msg, flags):
        # command has fired, for the purpose of this example we need to increment the
        # value of the global variable 'count'. Depending on the requirements of your
        # command you may not actually need to do anything since calculation of the
        # argument values to be displayed in the form is performed in cmd_Query(). In
        # which case the only thing that is required to be performed by your
        # basic_Execute method is to fire Notify() on your notify class with the
        # correct flag
        global count
        count = count + 1
 
        # post notification that our argument values need updating.
        MyTestNotifier().Notify(lx.symbol.fCMDNOTIFY_VALUE)
 
    def cmd_Query(self, index, vaQuery):
        va = lx.object.ValueArray()
        va.set(vaQuery)
 
        if index == 0:
            va.AddFloat(count * 0.5)
        else:
            va.AddFloat(count * 0.33)
            return lx.result.OK
 
lx.bless(Command, "mm.valuetest")