Skip to content

Instantly share code, notes, and snippets.

@mattcox
Created October 21, 2015 16:07
Show Gist options
  • Save mattcox/1ffe6ab52babe2813fdd to your computer and use it in GitHub Desktop.
Save mattcox/1ffe6ab52babe2813fdd to your computer and use it in GitHub Desktop.
A pointless command that demonstrates how to use a notifier without arguments
#!/usr/bin/env python
import lx
import lxu.command
import modo
class Command (lxu.command.BasicCommand):
def __init__ (self):
lxu.command.BasicCommand.__init__ (self)
self.not_svc = lx.service.NotifySys ()
self.notifier = lx.object.Notifier ()
def basic_Enable (self, msg):
selected = modo.current ().selectedByType (lx.symbol.sITYPE_MESH)
return len (selected) > 0
def cmd_Flags (self):
return 0
def basic_Execute (self, msg, flags):
selected = modo.current ().selectedByType (lx.symbol.sITYPE_MESH)
for item in selected:
lx.out (item.name)
def cmd_NotifyAddClient (self, argument, object):
self.notifier = self.not_svc.Spawn ("select.event", "item +d")
self.notifier.AddClient (object)
def cmd_NotifyRemoveClient (self, object):
self.notifier.RemoveClient (object)
lx.bless (Command, "mesh.printName")
@jangellx
Copy link

I think it should go like this instead:

First, initialize the notifier to None instead of lx.object.Notifier(), just so we can tell that it isn't set to anything.

self.notifier = None

cmd_NotifyAddClient() should then only Spawn() the notifier if it is not None:

    def cmd_NotifyAddClient (self, argument, object):
        if self.notifier == None:
              self.notifier = self.not_vc.Spawn ("select.event", "item +d")

        self.notifier.AddClient (object)

This ensures that multiple clients can be added to a given notifier and will get properly removed later, and we don't allocate extra notifier instances that we don't need.

@jangellx
Copy link

Also worth noting is the argument index. This will be -1 for the entire command, and may be called again will the index of a particular argument (usually the one queried in the form).

This can be used together with the notifiers in UIValueHints, which only operate on specific arguments.

@ivogrig
Copy link

ivogrig commented Oct 26, 2015

On a different note: there is a shortcut for common item types, so
selected = modo.current ().selectedByType (lx.symbol.sITYPE_MESH)

can be substituted with
selected = modo.Scene().meshes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment