Momentary plug-in example
Below is an example of a plug-in which prints info of objects. It is a simple example of a momentary plug-in.
prtNodeInfo .cpp
#include <machdefs.h>
#include <AlLiveData.h>
#include <AlFunction.h>
#include <AlFunctionHandle.h>
#include <AlUniverse.h>
#include <awString.h>
#include <AlDagNode.h>
#include <AlDagNodeExt.h>
#include <AlEditor.h>
#include <AlPickList.h>
#include <AlPickable.h>
#include <AlWindow.h>
#include <AlTM.h>
#include <AlList.h>
#include <AlGroupNode.h>
namespace
{
AlFunctionHandle h;
AlMomentaryFunction hFunc;
AlEditor* hEditor = nullptr;
struct PluginData
{
int wantId = 1;
int wantVer = 1;
};
PluginData s_data;
void
wantIdCallback( const bool newValue )
{
s_data.wantId = newValue;
}
void
wantVerCallback( const bool newValue )
{
s_data.wantVer = newValue;
}
} // namespace
//
// Returns the first picked item
//
AlObject *firstPickedItem()
{
if ( AlPickList::firstPickItem() != sSuccess )
return NULL;
return AlPickList::getObject();
}
//
// Returns the next picked item
//
AlObject *nextPickedItem()
{
if ( AlPickList::nextPickItem() != sSuccess )
return NULL;
return AlPickList::getObject();
}
static void
printNodeInformation( AlDagNode *node )
{
unsigned int version;
bool pendingUpdate;
awString::CString idString;
AlPrintf( kErrlog, "DagNodeName = %s\n", node->name());
AlPrintf( kPrompt, "DagNodeName = %s \n", node->name());
if( s_data.wantId )
{
AlDagNodeExt::getUniqueIdentifier( node->asDagNodePtr(), idString);
AlPrintf( kErrlog, " UniqueIdentifier = %s\n", idString.asChar());
AlPrintf( kPrompt, " UniqueIdentifier = %s\n", idString.asChar());
}
if( s_data.wantVer )
{
AlDagNodeExt::getVersionNumber( node->asDagNodePtr(), version, pendingUpdate );
AlPrintf( kErrlog, " VersionNumber = %d, Version number will update on File Save = %s\n", version, pendingUpdate ? "Yes" : "No" );
AlPrintf( kPrompt, " VersionNumber = %d, Version number will update on File Save = %s\n", version, pendingUpdate ? "Yes" : "No");
}
}
void prtNodeInofFunc( void )
{
if( sSuccess != AlGetInteger( "prtNodeInfo.uid", s_data.wantId ) )
s_data.wantId = 0;
if( sSuccess != AlGetInteger( "prtNodeInfo.ver", s_data.wantVer ) )
s_data.wantVer = 0;
AlObject *node = firstPickedItem();
if(!node)
{
AlPrintf( kPrompt, "Please have at least 1 node selected.");
return;
}
while(node)
{
if(node->asDagNodePtr())
{
AlDagNodeExt::iteratorDagEachNodeOnce(*(node->asDagNodePtr()), printNodeInformation);
}
node = nextPickedItem();
}
return;
};
extern "C"
PLUGINAPI_DECL int plugin_init( const char *dirName )
//
// This routine initializes the plugins and attaches it to the menu.
// It returns 0 if there is no initialization error.
//
{
// Initialize the universe. This must be done by all
// plugins. If the universe is not initialized the plugin
// will fail.
AlUniverse::initialize( );
// Create the function as a AlMomentaryFunction
// note that the icon name will be nothing_func.S, nothing_func.M
hFunc.create( "pl_PrtNodeInfo", prtNodeInofFunc );
// Allocate a function handle. The first argument is the label on
// the menu and the second is the function to invoke when the
// menu item is selected.
h.create( "Print Node Info", &hFunc );
// Define the attribute string for the attribute line below
// the prompt line.
h.setAttributeString( "prt_nodeinfo" );
// Initialize default values in the option box.
hEditor = new AlEditor( "Print Node Info", "pl_PrtNodeInfo" );
hEditor->addCheckbox(
"Unique Identifier", s_data.wantId != 0, wantIdCallback );
hEditor->addCheckbox(
"Version Number", s_data.wantVer != 0, wantVerCallback );
hEditor->create();
// Set the option box
h.setOptionBox( "Print Node Info", "pl_PrtNodeInfo" );
// Indicate which menu to add the plugin to. addToMenu()
// adds the plugin to the bottom of the menu, while
// appendToMenu() will add it to the top of the menu.
h.installOnMenu( "al_goto", FALSE /* bottom */ );
// Return a success code.
// Returning a non zero indicates an error.
// An error value ( a non-zero ) will be printed on the prompt
// line in Alias.
AlPrintf( kPrompt, "Print Node Info plug-in installed under 'Utilities' menu.");
return 0;
}
extern "C"
PLUGINAPI_DECL int plugin_exit( void )
{
// Remove edtior from the menu
h.removeFromMenu();
// Delete it
delete hEditor;
hEditor = nullptr;
h.deleteObject();
hFunc.deleteObject();
return 0;
}