AutoExtract/AutoExtract.cpp

#include "AutoExtract.h"

// The single instance of this class will sit in the memory for the whole session, and watch for map extraction nodes.
// When it finds a map extraction node with a specific name, it will execute it, then exit mudbox.
AutoExtract g_cInstance;

IMPLEMENT_CLASS( AutoExtract, Node, "autoextract" );

AutoExtract::AutoExtract( void ) :
    FileEvent( this )
{
    // The FileEvent attribute of this node is connected to Mudbox's main FileEvent node.
    // When anything happens involving files, this node will be notified, and the OnNodeEvent
    // method will be called.
    FileEvent.Connect( Kernel()->FileEvent );
};

void AutoExtract::OnNodeEvent( const Attribute &a, NodeEventType t )
{
    // This method is called when any kind of event happens to any attribute of this node.
    // So we need to check to make sure it is the right kind of event on the right
    // attribute.  We are interested in the "post-read" event, which is called after any kind of file
    // is read into Mudbox.  We look at the file name to be sure it is a scene (.mud) file, because
    // this same event will be triggered when Mudbox reads image files, stamps, stencils, etc.
    if ( t == etValueChanged && a == FileEvent && FileEvent.Value() )
    {
        if ( FileEvent->m_eType == FileEventNotifier::typePostRead && FileEvent->m_sFileName.right( 4 ) == NTRQ( ".mud" ) )
        {
            // We could go ahead here and do the extraction, but instead we are going to
            // request a "deferred" event, just in case Mudbox is still in the middle of
            // setting up the new scene.  When you do this, the same event is sent back
            // when Mudbox is idle, with "t" set to "etDeferred".  This allows you to 
            // process the event when everything has settled down.
            //
            RequestDeferredEvent( FileEvent );
        };
    };

    // If we have received the deferred event, it is time to extract the maps.
    if ( t == etDeferred && a == FileEvent )
    {
        bool bHadExtraction = false;

        // Look at all the nodes in the mudbox scene, and find the map extraction nodes.
        // (An "operation" in the UI is stored as a Node in the scene.)
        for ( Node *pNode = Node::First(); pNode; pNode = pNode->Next() )
        {
            if ( pNode->IsKindOf( MapExtractor::StaticClass() ) )
            {
                MapExtractor *pE = dynamic_cast<MapExtractor *>( pNode );
                MB_SAFELY( pE )
                {
                    // Look for the string "autoextract" in the node's name, and disregard
                    // any nodes that do not contain it.
                    QString sName = pE->Name();
                    if ( sName.indexOf( "autoextract" ) != -1 )
                    {
                        // Execute the map extraction in silent mode, which means the UI will not be visible (but the progress bar will)
                        // and there will be no confirmation dialog boxes that could stop it from running.
                        pE->Execute( false );

                        bHadExtraction = true;
                    };
                };
            };
        };

        // If any extractions were done, quit mudbox.
        if ( bHadExtraction )
            QCoreApplication::exit();
    };
};