Difference between revisions of "Channel Mod Math Multi"

From The Foundry MODO SDK wiki
Jump to: navigation, search
Line 9: Line 9:
 
[[File:Mathmulti shot1.png]]
 
[[File:Mathmulti shot1.png]]
  
the untransformed cubes
+
The untransformed cubes
  
 
[[File:Mathmulti shot2.png]]
 
[[File:Mathmulti shot2.png]]

Revision as of 17:53, 10 September 2013

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.

Functionality

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

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

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

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

class CMathMultiInstance
       :
       public CLxImpl_PackageInstance,
       public CLxImpl_ChannelModItem
{
       CMathMultiLog		 log;
         
   public:
       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 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 CMathMultiPackage : public CLxImpl_Package
{
   public:
       static LXtTagInfoDesc		 descInfo[];
       CLxPolymorph<CMathMultiInstance> chanmod_factory;
        
       CMathMultiPackage ();

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

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

Server Tags

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

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

Initialize

       void
initialize ()
{
       CLxGenericPolymorph		*srv;

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

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.

Implementations

        LxResult
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;
                                        break;
                        
                                case 1:		// Subtract
                                        result -= dVal;
                                        break;
                                        
                                case 2:		// Multiply
                                        result *= dVal;
                                        break;
                                        
                                case 3:		// Divide
                                        if (dVal != 0.0)
                                                result /= dVal;
                                        break;
                                
                                case 5:		// Min
                                        if (dVal < result)
                                                result = dVal;
                                        break;
                                
                                case 6:		// Max
                                        if (dVal > result)
                                                result = dVal;
                                        break;
                        }
                }
        }
        
        if (op == 4 && nLinks > 1)
                result /= nLinks;
        
        chanMod.WriteOutputFloat (attr, 0, result);
        
        return LXe_OK;
}

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

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

This function creates factories to export CMathMultiInstance.