Difference between revisions of "Example cmd sy partifymesh"

From The Foundry MODO SDK wiki
Jump to: navigation, search
m (Config: Removed help URL note)
m (Reverted edits by Synide (talk) to last revision by Adissid)
Line 353: Line 353:
 
* We specify command help details regarding what the command does.
 
* We specify command help details regarding what the command does.
 
* We define a message table for localized error messages.
 
* We define a message table for localized error messages.
* We specify a help url for this command.
+
* We specify a help url for this command. '''Note:''' ''kit relative filespecs in HelpURLs do not currently work as at build 52162. Has been bugged for future release fix.''
 
* We detail some UI elements relating to icons. See [[Icon_Resources|icon resources]] for more details.
 
* We detail some UI elements relating to icons. See [[Icon_Resources|icon resources]] for more details.
  

Revision as of 16:10, 16 September 2013

Introduction

This article details in laymans terms adding a 'new' command to modo. It has been written by an SDK end user, so may or may not be representative of 'good' or even accurate information.

Planning

It's good practice to note down a few details describing the scope of what your command does, when it does it and how it may be activated so as to give yourself some guidelines to work too. So...

    1. What should this command do?
      This command should 'apply a unique part tag to each polygon in the currently selected mesh'.
    2. What should this command be called?
      Based on what the command is supposed to do and whom has made it a good name for this command would be 'sy.partifymesh'.
    3. When should this command be allowed to run?
      This command should only be 'enabled' when the user has a 'mesh' item selected AND that mesh item has some polygons.
    4. Is the command going to possibly run from a button?
      For this particular example we'll make the command suitable for running from a button. So, we'll detail help information and images that will automatically apply when the command is mapped to a button.

Implementation

Code

A basic 'code flow' summary follows, it's not in any particular order of operation :

  • The module's initialize method is called which in turn calls the namespace's initialize method.
  • The namespace initialize registers the 'server' with appropriate interfaces for this plugin, which is how modo knows that these servers exist.
  • When the command is activated within modo the constructor sets a CLxUser_Log. The fact that the log subsystem even exists is set up via server tags (LXtTagInfoDesc ). Note that this log is purely for debugging purposes, and should not be present in shipping code.
  • The runtime acquires the flags from this plugin via the basic_CmdFlags method, which we use to report that this command performs undoable actions, in this case by calling functions that modify the mesh.
  • The runtime calls the basic_Enable method to see if this command can be run or not, and which is used to mark the command as disabled or enabled in the user interface. If the command is disabled, cmd_Execute will not be called.
  • When added to as a button in Form View, the runtime calls into basic_AddNotifiers, which in turn essentially registers what events this plugin will receive notification for and how to invalidate the user interface when a relevant event occurs. In this case, we're listening for changes to the item selection, and that we want it to notify the user interface to refresh our button's enable state when that occurs.
  • Finally, the runtime will call the plugin's cmd_Execute method if the command is enabled. Errors are reported through the command's CLxUser_Message, which is obtained through a call to the basic_Message method on our object.


sy_cmds.cpp

/* synide@rocketmail.com */
 
#include <lx_plugin.hpp>
 
namespace sy_partifymesh_cmd			{	extern void initialize();	
                                                       extern void cleanup();	};
 
 
void initialize() {
	sy_partifymesh_cmd			::initialize();
};
 
void cleanup() {
	sy_partifymesh_cmd			::cleanup();
};


sy_partifymesh_cmd.cpp

/* synide@rocketmail.com */
 
//=====================================
// Notes:
//				April, 2012 - Revised for Liki example.
//				May, 2012 - A More comprehensive implementation.
//                                               Added preprocessor directives to disabled EventLog output when not in Debug.
//                                               Added 'meshes.event' to facilitate disabling if the mesh is emptied of polygons.
//                                               Added a 'completion' message.
 
#include <time.h>
#include <string>
#include <sstream>
 
#include <lxidef.h>
#include <lx_plugin.hpp>
#include <lx_layer.hpp>
#include <lx_mesh.hpp>
#include <lx_log.hpp>
#include <lxu_command.hpp>
#include <lxu_select.hpp>
 
 
namespace sy_partifymesh_cmd
{
 
#ifndef	__NAMESPACE_STATICS
 
 
static char *SERVER_NAME                = "sy.partifymesh";
static char *SERVER_USERNAME         = "Sy PartifyMesh";
static char *LOG_NAME                     = "sy/cmds/partifymesh";
 
static CLxItemType                           iType_Mesh                 (LXsITYPE_MESH);
 
 
#endif	__NAMESPACE_STATICS
 
 
std::string fnI2S(unsigned value, bool notnullterminated = true, unsigned padding = 0, bool asHexadecimal = false, bool withHexPrefix = false)
{
	std::ostringstream os;
	std::string result;
	if (asHexadecimal) {os << std::hex;}
	if (value < 10 && padding) {os << 0;}
	if (value < 100 && padding > 1) {os << 0;}
	if (value < 1000 && padding > 2) {os << 0;}
	if (notnullterminated)
		os << value;
	else
		os << value << std::ends;
	if (asHexadecimal && withHexPrefix) {result = std::string("0x");}
	result += os.str();
	return result;
};
 
std::string fnF2Sfixed(float value, bool nullterminated = false, unsigned p = 1)
{
	std::ostringstream os;
	os.setf(std::ios::fixed);
	os.precision(p);
	if (nullterminated)
		os << value << std::ends;
	else
		os << value;
	return os.str();
};
 
 
 
/*
 * 'sy.partifymesh' command is derived from 'CLxBasicCommand'.
 *
 */
class CSyPartifyMesh : public CLxBasicCommand {
public:
	static LXtTagInfoDesc           descInfo[];
 
	CLxUser_LayerService            LayerService;
 
	#ifdef	_DEBUG
	CLxUser_Log                     PartifyMeshLog;
 
	CSyPartifyMesh() {
		PartifyMeshLog.setByName(LOG_NAME);
	};
	#endif	_DEBUG
 
 
	void basic_Notifiers() override {
		basic_AddNotify(LXsNOTIFIER_SELECT, "item +d"); /* Will be notified when Item selection changes */
		basic_AddNotify(LXsNOTIFIER_MESHES, "+p +d");   /* Will be notified on polygon changes in the mesh */
	};
 
 
	bool basic_Enable(CLxUser_Message &msg) override {
		/*
			Utilize the new (601) CLxItemSelectionType to find the first mesh item and then check if it
			has any polygons.
		*/
 
		bool result(false);
 
		CLxItemSelectionType	TypedItemSelector(LXsITYPE_MESH);
		CLxUser_Item			theItem;
		const char				*ItemName;
 
		if (TypedItemSelector.GetFirst(theItem) && theItem.test())
		{
			if (theItem.Type() == iType_Mesh.Type())
			{
				CLxUser_Mesh			theMesh;
				CLxUser_ChannelRead		aChanReader;
 
				if (aChanReader.from(theItem))
				{
					if (aChanReader.Object(theItem, LXsICHAN_MESH_MESH, theMesh))
					{
						result = theMesh.NPolygons() > 0;
						if (!result)
						{
							theItem.UniqueName(&ItemName);
							if (ItemName) {
								msg.SetCode(LXe_FAILED);
								msg.SetMessage(SERVER_NAME, "ItemHasZeroPolygons", 0);
								msg.SetArgumentString(1, ItemName);
							}
						}
					}
					else
					{
						/* Unlikely to ever get here */
						msg.SetCode(LXe_CMD_DISABLED);
						msg.SetMessage(SERVER_NAME, "NoValidMesh", 0);
					}
				}
 
				aChanReader.clear();
				theMesh.clear();
			}
			else
			{
				msg.SetCode(LXe_CMD_DISABLED);
				msg.SetMessage(SERVER_NAME, "NoValidMesh", 0);
			}
			theItem.clear();
		}
		else
		{
			msg.SetCode(LXe_CMD_DISABLED);
			msg.SetMessage(SERVER_NAME, "NoValidMesh", 0);
		}
		return result;
	};
 
 
	int basic_CmdFlags() override {
		return LXfCMD_UNDO;
	};
 
 
	void cmd_Execute(unsigned int flags) override {
		/*
		    We restrict the LayerScan's results to the 'Primary' selected mesh only so that we
		    guard against the user having multiple 'selected' meshs which could result in overload.
		*/
 
		CLxUser_LayerScan               LayerScan;
		CLxUser_Item                    theItem;
		CLxUser_Mesh                    theMesh;
		CLxUser_Polygon                 thePolygon;
		CLxUser_StringTag               theTag;
		const char                      *ItemName;
 
		LayerService.BeginScan(LXf_LAYERSCAN_PRIMARY | LXf_LAYERSCAN_WRITEMESH, LayerScan);
 
		if (LayerScan.ItemByIndex(0, theItem))
			theItem.UniqueName(&ItemName);
 
		/* As there should only ever be 1 mesh found by the LayerScan it's index should be zero. */
		if (LayerScan.EditMeshByIndex(0, theMesh))
		{
			int NoOfPolygons = theMesh.NPolygons();
 
			// Set 'thePolygon' as the polygon accessor for the 'theMesh'.
			thePolygon.fromMeshObj(theMesh);
 
			// Set 'theTag' as the 'tags' accessor for the polygon.
			theTag.set(thePolygon);
 
			std::string tag;
			clock_t timerStart = clock();
 
			#ifdef _DEBUG
			unsigned msg_stride = NoOfPolygons / 5;
			unsigned msg_counter = 1;
			#endif	_DEBUG
 
			for (int i1 = 0; i1 < NoOfPolygons; i1++)
			{
				/*Ask the polygon accessor to populate itself based on the poly index. */
				thePolygon.SelectByIndex(i1);
 
				/* Construct a unique 'part' tag name. */
				tag = "poly_" + fnI2S(i1);
 
				/* Ask the 'tags' accessor to set the 'PART' tag. */
				theTag.Set(LXi_PTAG_PART, tag.c_str());
 
				#ifdef  _DEBUG
				//logging at 20%, 40%, 60%, 80%
				if (PartifyMeshLog && (i1 == (msg_stride * msg_counter)))
				{
					clock_t timerDiff = clock() - timerStart;
					double duration = (double)timerDiff / (double)CLOCKS_PER_SEC;
					switch(msg_counter)
					{
					case 1:
						PartifyMeshLog.Message(LXe_INFO, "20\%%, %u polygons in %.6f seconds.", i1, duration);
						break;
					case 2:
						PartifyMeshLog.Message(LXe_INFO, "40\%%, %u polygons in %.6f seconds.", i1, duration);
						break;
					case 3:
						PartifyMeshLog.Message(LXe_INFO, "60\%%, %u polygons in %.6f seconds.", i1, duration);
						break;
					case 4:
						PartifyMeshLog.Message(LXe_INFO, "80\%%, %u polygons in %.6f seconds.", i1, duration);
						break;
					}
					msg_counter += 1;
				}
				#endif  _DEBUG
			}
 
			/* Tell the LayerScan that we are only 'editing' polygon tags. */
			LayerScan.SetMeshChange(0, LXf_MESHEDIT_POL_TAGS);
 
			/* Apply the mesh changes. */
			LayerScan.Apply();
 
			clock_t timerDiff = clock() - timerStart;
			double duration = (double)timerDiff / (double)CLOCKS_PER_SEC;
 
			basic_Message().SetCode(LXe_INFO);
			basic_Message().SetMessage(SERVER_NAME, "Complete", 0);
			basic_Message().SetArgumentInt(1, NoOfPolygons);
			basic_Message().SetArgumentString(2, fnF2Sfixed((float)duration,false,6).c_str());
 
			#ifdef   _DEBUG
			PartifyMeshLog.Message(LXe_INFO, "100\%%, %u polygons in %.6f seconds.", NoOfPolygons, duration);
			#endif  _DEBUG
		}
		else
		{
			/* Unlikely to ever get here */
			basic_Message().SetCode(LXe_CMD_DISABLED);
			basic_Message().SetMessage(SERVER_NAME, "NoValidMesh", 0);
 
			#ifdef  _DEBUG
			PartifyMeshLog.Message(LXe_NOTFOUND, "No valid mesh found!");
			#endif _DEBUG
		}
	};
 
 
};
 
 
LXtTagInfoDesc CSyPartifyMesh::descInfo[] = {
	{LXsSRV_LOGSUBSYSTEM, LOG_NAME},
	{LXsSRV_USERNAME, SERVER_USERNAME},
	{0}
};
 
 
void initialize() {
		CLxGenericPolymorph *srv = new CLxPolymorph<CSyPartifyMesh>;
 
		srv->AddInterface(new CLxIfc_Command    <CSyPartifyMesh>);
		srv->AddInterface(new CLxIfc_StaticDesc <CSyPartifyMesh>);
 
		lx::AddServer(SERVER_NAME, srv);
};
 
 
void cleanup() {};
 
};//namespace sy_partifymesh_cmd

Config

The following xml file is what's referred to as a config and would form part of your plugin's release files for your kit. This particular config has the following features :-

  • A new command category is created to apportion this particular command under 'Sy/Cmds' in the modo command tree listing.
  • We specify command help details regarding what the command does.
  • We define a message table for localized error messages.
  • We specify a help url for this command. Note: kit relative filespecs in HelpURLs do not currently work as at build 52162. Has been bugged for future release fix.
  • We detail some UI elements relating to icons. See icon resources for more details.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
  <atom type="Categories">
    <hash type="Category" key="Commands">
      <hash type="C" key="sy.partifymesh">Sy/Cmds</hash>
    </hash>
  </atom>
 
  <atom type="CommandHelp">
    <hash type="Command" key="sy.partifymesh@en_US">
      <atom type="UserName">PartifyMesh</atom>
      <atom type="Desc">Apply unique 'part' tag to each polygon in selected mesh.</atom>
      <atom type="Tooltip">Apply unique 'part' tag to each polygon in selected mesh.</atom>
      <atom type="ButtonName">PartifyMesh</atom>
      <atom type="Example">sy.partifymesh</atom>
    </hash>
  </atom>
 
  <atom type="Messages">
    <hash type="Table" key="sy.partifymesh.en_US">
      <hash type="T" key="ItemHasZeroPolygons">&quot;%1&quot; has zero polygons.</hash>
      <hash type="T" key="NoValidMesh">No valid mesh selected.</hash>
      <hash type="T" key="Complete">&quot;%1&quot; polygons completed in &quot;%2&quot; seconds.</hash>
    </hash>
  </atom>
 
  <atom type="HelpURLs">
    <hash type="HelpURL" key="command:sy.partifymesh">kit_SyCmds:help\index.html#partifymesh</hash>
  </atom>
 
  <atom type="UIElements">
    <hash type="Image" key="sy_cmd_icons">sy_cmds_icons.png</hash>
 
    <!-- First row of image -->
    <hash type="Icon" key="sy.partifymesh_20">         <atom type="Source">sy_cmd_icons</atom>     <atom type="Location">0 0 20 20</atom>   </hash>
 
    <!-- Second row of image -->
    <hash type="Icon" key="sy.partifymesh_32">         <atom type="Source">sy_cmd_icons</atom>     <atom type="Location">0 20 32 32</atom>  </hash>
  </atom>
 
</configuration>

Icon Resources

The following .png file is provided as an example of the icon images that commands mapped to UI buttons may implement, the display of which is an automated process. When the command returns 'false'/LXe_CMD_DISABLED from basic_Enable, the system automatically generates a desaturated version of the icon image for the button. See the icon resources article for more details.

Sy cmd icons.png - 20px on first row, 32px on second row.

More Information