Commands

This example covers how to create and manage command in Guerilla.

Creating a new command

Let’s implement a ‘print all plugs’ command

from guerilla import Modifier, Document, pwarning
from guerilla import command, Node

class Printplugs(command):

    @staticmethod
    def isenabled(luaObj, window):

        d = Document()
        sel = d.selection()

        return True if sel and list(sel[0].plugs()) else False

    @staticmethod
    def action(luaObj, window, x, y, suffix):

        d = Document()
        sel = d.selection()
        if sel:
            print '%s plugs:' % sel[0].name
            for c in sel[0].plugs():
                print c.name

cmd = Printplugs('Print plugs')
cmd.install('Help', 'Debug')

Defining commands at startup

For all users:

  • Add your py scripts to the Guerilla plugins folder (Ask you system administrator if you don’t know its location)

For the current user:

  • Add a folder with your py scripts in Preferences -> Directories -> User Plugins

Note

Guerilla will print a message before sourcing a script (“Execute file […]/foo.py”)

Dealing with commands

Guerilla already comes with a bunch of commands

from guerilla import command
cdict = command.getshortcommands()
for k in sorted(cdict.keys()):
    print k, cdict[k]

Let’s use one

from guerilla import Modifier, command
with Modifier() as mod:
        n1 = mod.createnode('foo'); n2 = mod.createnode('bar')
        mod.select([n1, n2])
command.executebyshortname('Group')

Monkey patching

What about implementing an alternate ‘About Guerilla Render’ command?

from guerilla import command, pwarning, lua, version
class PrintAbout(command):

    @staticmethod
    def action(luaObj, window, x, y, suffix):
        v = version ()
        pwarning('== Guerilla Render Version %s ==' % v)

cmd = PrintAbout('About Guerilla Render')
cmd.install('Help')

Going further

See also

guerilla.command for details