Channel Modifier Python Wrapper

From The Foundry MODO SDK wiki
Revision as of 18:19, 16 June 2016 by Shf (Talk | contribs)

Jump to: navigation, search

The Python SDK in MODO 10 supports creating channel modifiers. The module is lxu.chanmod.


Here is an example channel modifier that takes one input float and outputs twice that value. The core of the code is a sub-class of lxu.chanmod.Operator which declares the channels for the modifier in initialize() and performs the computation in eval(). The wrapper code binds the channels to the Value attributes on the chans object -- with the same name as the channels -- which can be read and written during evaluation. The support code needed for exporting this as a server is provided by the lxu.chanmod.MetaPackage class and the lxu.chanmod.bless_server() method, which takes the client's package class and the name of the server.

import lx
import lxu.chanmod
class TheDoubler(lxu.chanmod.Operator):
    def initialize(self, desc):
        desc.add ("input", lx.symbol.sTYPE_FLOAT)
        desc.chmod_value (lx.symbol.fCHMOD_INPUT)
        desc.add ("output", lx.symbol.sTYPE_FLOAT)
        desc.chmod_value (lx.symbol.fCHMOD_OUTPUT)
    def eval(self, chans):
        inVal = chans.input.GetFlt()
        chans.output.SetFlt(inVal * 2)
class TheDoublerPackage(lxu.chanmod.MetaPackage):
    def operator(self):
        return TheDoubler()
lxu.chanmod.bless_server(TheDoublerPackage, "cmDoubler")

Other Bindings

In addition to Value objects, input channels can also be bound to ValueArray objects, allowing multi-link channels to be read individually.

        desc.add ("input", lx.symbol.sTYPE_FLOAT)
        desc.chmod_array (lx.symbol.fCHMOD_INPUT | LXfCHMOD_MULTILINK)
        sum = 0
        for v in chans.multi:
            sum += v

Channels can also be bound to Matrix objects, and writing to a Matrix output now automatically propagates to all outputs so only a single result needs to be generated.

        desc.add ("outMat", lx.symbol.sTYPE_MATRIX4)
        desc.chmod_matrix (lx.symbol.fCHMOD_OUTPUT)
        chans.outMat.Set4 (resultMatrix)

Channel modifiers can also read from time. There's no channel needed for this, just a float to get the result.

        desc.chmod_time ()
        t = chans._time