Skip to content

Instantly share code, notes, and snippets.

@mattcox
Created October 17, 2013 15:12
Show Gist options
  • Save mattcox/7026698 to your computer and use it in GitHub Desktop.
Save mattcox/7026698 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
This sample Python plugin demonstrates how to use notifiers in Python.
To use, embed the command in a form and change item selection, the command
should update.
'''
import lx
import lxifc
import lxu.select
import lxu.command
SERVER_NAME = "item.setSize"
class Command (lxu.command.BasicCommand):
def __init__ (self):
'''
Initialize command class. Adds a single queryable argument for
setting and getting the current selected item size.
'''
lxu.command.BasicCommand.__init__(self)
self.dyna_Add ('size', lx.symbol.sTYPE_FLOAT)
self.basic_SetFlags(0, lx.symbol.fCMDARG_QUERY)
self.scn_svc = lx.service.Scene ()
self.sel_svc = lx.service.Selection ()
self.not_svc = lx.service.NotifySys ()
self.notifier = lx.object.Notifier ()
self.locator_type = self.scn_svc.ItemTypeLookup (lx.symbol.sITYPE_LOCATOR)
def cmd_Flags (self):
'''
This command performs scene altering operations.
'''
return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO
def basic_Enable (self, msg):
'''
Only enable the command if at least one locator type item is selected.
'''
item_sel = lxu.select.ItemSelection().current()
for item in item_sel:
if item.TestType (self.locator_type) == True:
return True
return False
def cmd_Query (self, index, vaQuery):
'''
Query the command value by reading size channel on all selected
locator items. The values are added to an array and if the values
all differ, modo will show "(mixed)" on the control.
'''
if index != 0:
return
item_sel = lxu.select.ItemSelection().current()
scene = lxu.select.SceneSelection().current()
val_array = lx.object.ValueArray (vaQuery)
chan_read = lx.object.ChannelRead (scene.Channels (lx.symbol.s_ACTIONLAYER_EDIT, self.sel_svc.GetTime ()))
for item in item_sel:
if item.TestType (self.locator_type) == True:
size = chan_read.Double (item, item.ChannelLookup (lx.symbol.sICHAN_LOCATOR_SIZE))
val_array.AddFloat (size)
def basic_Execute(self, msg, flags):
'''
In the execute method, the channel value is set on each of the size
channels.
'''
item_sel = lxu.select.ItemSelection().current()
scene = lxu.select.SceneSelection().current()
if self.dyna_IsSet (0) == False:
return
chan_write = lx.object.ChannelWrite (scene.Channels (lx.symbol.s_ACTIONLAYER_EDIT, self.sel_svc.GetTime ()))
size = self.attr_GetFlt (0)
for item in item_sel:
if item.TestType (self.locator_type) == True:
chan_write.Double (item, item.ChannelLookup (lx.symbol.sICHAN_LOCATOR_SIZE), size)
def cmd_NotifyAddClient (self, argument, object):
'''
Add a single notifier to the command. This will send disable events
when the item selection changes.
'''
self.notifier = self.not_svc.Spawn ("select.event", "item +d")
self.notifier.AddClient (object)
def cmd_NotifyRemoveClient (self, object):
'''
Remove the notifier.
'''
self.notifier.RemoveClient (object)
'''
Initialize the plugin.
'''
lx.bless(Command, SERVER_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment