Color Channel with an initial color

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

Description:

Modo GUI Controls

In Modo many GUI controls are channels shown in forms with the item.channel command.

One of them are the color channels. Like the ones shown on a Locator when it's Draw Options are active.

Color Channels.jpg

In many situations you want to create this channel and gave it an initial color by default. If you don't do it, the channel will be created with a black color [0.0, 0.0, 0.0]

Code:

This snippet shows how to create a color channel and set up it's default color inside of the pkg_SetupChannels function of a Package class:

 
def pkg_SetupChannels(self, addChan):
        """
            The SetupChannels function is called the first time an instance of this
            package is to be created, allowing the package to define the set of channels
            that it wants. Channels are given by name and type, and optional settings
            may be applied to each one.
 
            We are adding several channels to our item as an example.
        """
 
        # Cast AddChannel
        addChannel = lx.object.AddChannel(addChan)
 
        # Name of the channel
        CHAN_LCOLOR = 'LeftColor'
 
        # A green Fluorescent color
        #It will be the default initial color for the channel
        CHDEFVAL_LCOLOR = [0.7843, 0.9647, 0.0000]
 
        #Create the channel 
        addChannel.NewChannel(CHAN_LCOLOR, lx.symbol.sTYPE_COLOR1)
 
        # Use lx.symbol.sCHANVEC_RGBA to get an Alpha Value
        addChannel.SetVector(lx.symbol.sCHANVEC_RGB)  
 
        # Create a storage object
        defaultValue = lx.object.storage()
 
        # Set it's type to double, this type is NOT defined on lx.symbol is missing
        defaultValue.setType('double')
 
        # Set it's size to store 3 doubles
        defaultValue.setSize(3)
 
        # Set the values
        defaultValue.set((CHDEFVAL_LCOLOR[0],
                          CHDEFVAL_LCOLOR[1],
                          CHDEFVAL_LCOLOR[2]))
 
        # Set the default vector to the storage object
        addChannel.SetDefaultVec(defaultValue)

This is the result of that snippet:

Color Channel Fluorescent Green.jpg

To show that channel on the interface you add a control inside of your Form that uses the item.channel command in Modo.

        <list type="Control" val="cmd item.channel {your_item_name_here$LeftColor} ?">
	    <atom type="Label">Left Color</atom>
	    <atom type="Tooltip">Color used for the markers at the left side of the Guide.</atom>
	    <atom type="Desc">Color used for the markers at the left side of the Guide.</atom>
	    <atom type="StartCollapsed">0</atom>
	    <atom type="Hash">57575632771:control</atom>
        </list>

Note: This snippet will not work if you copy paste it on any of your forms.

Storage Objects

Storage objects are a way to pass certain data between Python and Modo.

If you introspect the __doc__ of some method of the api:

        lx.object.AddChannel.SetDefaultVec.__doc__

The docs will indicate that this method needs a storage object:

        SetDefaultVec(double[] defVec)

The [] symbol after the type (double) is indicating you need a storage object passed to this method.

lx.object.storage

The lx.object.storage has the following methods:

 
        # Read out the contents of the buffer as a tuple, either the full length or the specified length 
        (tuple) = lx.object.storage.get()
 
        # Set or read the offset in the buffer 
        # Read
        offset = lx.object.storage.offset()
        # Set
        lx.object.storage.offset(offset)
 
        # Assign a tuple to fill the buffer
        lx.object.storage.set()
 
        # Set the number of elements in the buffer
        lx.object.storage.setSize(integer)
 
        # Set the element type for buffer: float, double, int, byte, unsigned, pointer
        lx.object.storage.setType( 'float' | lx.symbol.sTYPE_FLOAT, 
                                   'double',                             # NOT defined in lx.symbol.sTYPE
                                   'interger' | lx.symbol.sTYPE_INTEGER,
                                   'byte',                               # NOT defined in lx.symbol.sTYPE
                                   'unsigned',                           # NOT defined in lx.symbol.sTYPE
                                   'pointer'                             # NOT defined in lx.symbol.sTYPE )
 
        # Return the size of the buffer
        size = lx.object.storage.size()
 
        # Read out the contents of the buffer as a string
        string = lx.object.storage.string()