Setting a string channels default value

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

Introduction

This article provides the sample code for setting a default value of a string channel on a very simple item plugin.

Code

stringTest_main.cpp
//---------------------------------------------------------------------------------
//
//  Simple example plugin demonstrating setting a default value on a string.
//
//---------------------------------------------------------------------------------
 
// Include the required modo SDK headers
#include <lx_plugin.hpp>
#include <lx_package.hpp>
#include <lxidef.h>
#include <lx_item.hpp>
#include <lx_value.hpp>
 
// Define the server names.
#define SERVER				"stringTest"
#define SERVER_INST			"stringTest.inst"
#define STRING_CHAN			"stringChannel"
 
//---------------------------------------------------------------------------------
//  Classes for stringTest
//---------------------------------------------------------------------------------
 
class item_package;
 
class item_instance : public CLxImpl_PackageInstance
{
	public:
		item_package			*src_pkg;
		CLxUser_Item			item_loc;
 
		LxResult	pins_Initialize			(ILxUnknownID item, ILxUnknownID super)		LXx_OVERRIDE;
		void		pins_Cleanup			(void)						LXx_OVERRIDE;
		LxResult	pins_Newborn			(ILxUnknownID original)				LXx_OVERRIDE;
		LxResult	pins_AfterLoad			(void)						LXx_OVERRIDE;
		void		pins_Doomed			(void)						LXx_OVERRIDE;
};
 
class item_package :  public CLxImpl_Package
{
 	public:		
		static LXtTagInfoDesc		descInfo[];
		CLxUser_Item			item_loc;
 
		CLxSpawner<item_instance>	inst_spawn;
		item_package () : inst_spawn (SERVER_INST) {}
 
		LxResult	pkg_SetupChannels		(ILxUnknownID addChan)				LXx_OVERRIDE;
		LxResult	pkg_TestInterface		(const LXtGUID *guid)				LXx_OVERRIDE;
		LxResult	pkg_Attach			(void **ppvObj)					LXx_OVERRIDE;
};
 
//---------------------------------------------------------------------------------
//  Methods for stringTest
//---------------------------------------------------------------------------------
 
LxResult item_instance::pins_Initialize(ILxUnknownID item, ILxUnknownID super)
{
	/*
	 pins_Initialize is called when the instance is associated with the item.
	 The item is not part of the scene yet.
	 */
 
	item_loc.set (item);
	return LXe_OK;
}
 
LxResult item_instance::pins_Newborn (ILxUnknownID original)
{
	/*
	 pins_Newborn is called once the item is created and is part of the scene.
	 */
 
	return LXe_OK;
}
 
LxResult item_instance::pins_AfterLoad (void)
{
	/*
	 pins_Newborn is called once the item is loaded and is part of the scene.
	 */
 
	return LXe_OK;
}
 
void item_instance::pins_Doomed(void)
{
	/*
	 pins_Doomed is called once the item is destined to be deleted.
	 */
 
	item_loc.clear();
}
 
void item_instance::pins_Cleanup(void)
{
	/*
	 pins_Cleanup is called when the instance is about to be deleted.
	 */
 
	item_loc.clear();
}
 
LXtTagInfoDesc	 item_package::descInfo[] = {
	{ LXsPKG_SUPERTYPE, LXsITYPE_LOCATOR },
	{ 0 }
};
 
LxResult item_package::pkg_SetupChannels (ILxUnknownID addChan)
{
	/*
	 The pkg_SetupChannels functions is used to add channels
	 to the item plugin.
	*/
 
	// Add a CLxUser_AddChannel Interface - used for defining channels.
	CLxUser_AddChannel ac(addChan);
 
	// Create a new LXtObjectID. This will be used for storing the default value.
	LXtObjectID	obj;
 
	// CLxUser_Value SetString() method will be used to set the value of the string channel.
	CLxUser_Value	val;
 
	// Add the new channel. Define it as a string channel.
	ac.NewChannel(STRING_CHAN, LXsTYPE_STRING);
 
	// Next, set the storage type as a string. This is the type of value
	// that will be stored in the action layer.
	ac.SetStorage(LXsTYPE_STRING);
 
	// Finally we set the default object to be the LXtObjectID (obj).
	// This returns an object which we can use to store the value.
	ac.SetDefaultObj(&obj);
 
	// Localize the LXtObjectID.
	val.take(obj);
 
	// Finally, the SetString method is called to set the default
	// value to be "Some string value."
	val.SetString("Some string value.");
 
	return LXe_OK;
}
 
LxResult item_package::pkg_TestInterface (const LXtGUID *guid)
{
	return inst_spawn.TestInterfaceRC (guid);
}
 
LxResult item_package::pkg_Attach (void **ppvObj)
{    
	item_instance *inst = inst_spawn.Alloc (ppvObj);
	return LXe_OK;
}
 
//---------------------------------------------------------------------------------
//  Initialize the servers
//---------------------------------------------------------------------------------
 
void initialize ()
{
	/*
	Initialize the servers in turn.
	*/
 
	CLxGenericPolymorph	*srv;
 
	srv = new CLxPolymorph				<item_package>;
	srv->AddInterface (new CLxIfc_Package		<item_package>);
	srv->AddInterface (new CLxIfc_StaticDesc	<item_package>);
	thisModule.AddServer (SERVER, srv);
 
	srv = new CLxPolymorph				<item_instance>;
	srv->AddInterface (new CLxIfc_PackageInstance	<item_instance>);
	thisModule.AddSpawner(SERVER_INST, srv);
}
 
void cleanup()
{
 
}
 
//---------------------------------------------------------------------------------

Testing

Once the code above has been built using the modo SDK. You should be able to add the item plugin to modo.

  • Launch modo.
  • From the system menu, choose "Add Plug-in...".
  • Add the *.lx plugin file that the build process has created.
  • Modo should inform you that the package "stringTest" was successfully loaded.
  • From the item menu, choose: "Add Item > Other > stringTest". This will add the a new stringTest item to your scene.
  • Select the stringTest item and from the modo command prompt, enter: "item.channel stringChannel ?". This queries the value of the string channel on the stringTest item.
  • In the modo event log, modo should return: "Some string value.". This is the default value that was defined in pkg_SetupChannels().

More Information