Channel Mod Math Multi

From The Foundry MODO SDK wiki
Revision as of 21:52, 10 September 2013 by Adissid (Talk | contribs)

Jump to: navigation, search

Channel Mod Math Multi is a basic example plugin. This wiki page is intended as a walkthrough of the code in order to help you better understand the SDK.

This plugin is a channel modifier that takes in input and performs mathematical inputs on them. This is further explained in the functionality section below.


Below, We have three cubes that the math multi is acting upon. The modifier is taking in one cube's x-coordinate(3m) and the other's y-coordinate(3m). The result is being exported to the z-coordinate of the cube in the middle

Mathmulti shot1.png

The untransformed cubes

Mathmulti shot2.png

Here, the two inputs are being added together

Mathmulti shot3.png

Here, the two inputs are being multiplied together

Mathmulti shot4.png

Here, the two inputs are being divided

Mathmulti shot5.png

The difference of the two inputs is being taken here.

Code Walkthrough

Class Declarations

We want to have this class write out to the log, so we inherit from CLxLuxologyLogMessage.

class CMathMultiLog : public CLxLuxologyLogMessage
       CMathMultiLog () : CLxLuxologyLogMessage ("cmMathMulti") { }

       const char *	 GetFormat  () { return "Math Multiple Object"; }

To create an instance of the Math Multi object we need to implement the Math Multi Package object so we inherit from CLxPackageInstance. We also need to modify channels as part of our instance, so we inherit from CLxImpl_ChannelModItem.

class CMathMultiInstance
       public CLxImpl_PackageInstance,
       public CLxImpl_ChannelModItem
       CMathMultiLog		 log;
       CMathMultiPackage	*src_pkg;
       CLxUser_Item		 m_item;
       ILxUnknownID		 inst_ifc;

       LxResult		 pins_Initialize (ILxUnknownID item, ILxUnknownID super);
       void			 pins_Cleanup (void);
       LxResult		 pins_SynthName (char *buf, unsigned len);
       unsigned int		 cmod_Flags (ILxUnknownID item, unsigned int index);
       LxResult		 cmod_Allocate (
                                       ILxUnknownID cmod,
                                       ILxUnknownID eval,
                                       ILxUnknownID item,
                                       void **ppvData);
       void			 cmod_Cleanup (void *data);
       LxResult		 cmod_Evaluate (ILxUnknownID cmod, ILxUnknownID attr, void *data);

To set up our object we need a package, so we have this class inherit from CLxImpl_Package.

class CMathMultiPackage : public CLxImpl_Package
       static LXtTagInfoDesc		 descInfo[];
       CLxPolymorph<CMathMultiInstance> chanmod_factory;
       CMathMultiPackage ();

       LxResult		pkg_SetupChannels (ILxUnknownID addChan);
       LxResult		pkg_TestInterface (const LXtGUID *guid);
       LxResult		pkg_Attach (void **ppvObj);

Server Tags

The tags here indicate that the CMathMultisPackage class is dependent on the chanmodify SuperType with the internal name of cmMathMulti.

LXtTagInfoDesc	 CMathMultiPackage::descInfo[] = {
       { LXsPKG_SUPERTYPE,	"chanModify"	},
       { LXsSRV_LOGSUBSYSTEM,	"cmMathMulti"	},
       { 0 }


Our initialize function indicates that we will be exporting one server dependent on the CMathMultiPackage class that uses the Package and StaticDesc interfaces as well as being names cmMathMulti.

initialize ()
       CLxGenericPolymorph		*srv;

       srv = new CLxPolymorph<CMathMultiPackage>;
       srv->AddInterface (new CLxIfc_Package          <CMathMultiPackage>);
       srv->AddInterface (new CLxIfc_StaticDesc       <CMathMultiPackage>);
       thisModule.AddServer ("cmMathMulti", srv);


This function evaluates the modifiers we have used on the given channels.

CMathMultiInstance::cmod_Evaluate (
        ILxUnknownID		 cmod,		// ILxChannelModifierID
        ILxUnknownID		 attr,		// ILxAttributesID
        void			*data)		// User Data
        CLxLoc_ChannelModifier	 chanMod (cmod);
        double			 dVal, result = 0.0;
        unsigned int		 nLinks;
        unsigned		 i;
        int			 op;
//	log.Info ("cmod_Evaluate Method");
        // Read the 'operation' channel.
        chanMod.ReadInputInt (attr, 0, &op);
        // Get the number of links into the 'inputs' channel.
        chanMod.InputCount (1, &nLinks);
        for (i = 0; i < nLinks; i++) {
                // Read the value for this input link.
                chanMod.ReadInputFloatByIndex (attr, 1, i, &dVal);
                if (i == 0) {
                        result = dVal;
                else {
                        switch (op) {
                                case 0:		// Add
                                case 4:		// Average
                                        result += dVal;
                                case 1:		// Subtract
                                        result -= dVal;
                                case 2:		// Multiply
                                        result *= dVal;
                                case 3:		// Divide
                                        if (dVal != 0.0)
                                                result /= dVal;
                                case 5:		// Min
                                        if (dVal < result)
                                                result = dVal;
                                case 6:		// Max
                                        if (dVal > result)
                                                result = dVal;
        if (op == 4 && nLinks > 1)
                result /= nLinks;
        chanMod.WriteOutputFloat (attr, 0, result);
        return LXe_OK;

This function creates factories to export CMathMultiInstance.

CMathMultiPackage::CMathMultiPackage ()
       chanmod_factory.AddInterface (new CLxIfc_PackageInstance<CMathMultiInstance>);
       chanmod_factory.AddInterface (new CLxIfc_ChannelModItem<CMathMultiInstance>);