#include "sceneAssemblyStrings.h"
#include "adskSceneMetadataCmd.h"
#include <maya/MSyntax.h>
#include <maya/MGlobal.h>
#include <maya/adskDataAssociations.h>
#include <maya/adskDataAccessor.h>
#include <maya/adskDataAccessorMaya.h>
#include <maya/adskDataStream.h>
#include <iostream>
#include <string>
using namespace std;
const char gStructureName [] = "adskSceneMetadataCmdStructure";
const char gMemberName [] = "adskSceneMetadataCmdDataString";
const char gStreamName [] = "adskSceneMetadataCmdStream";
AdskSceneMetadataCmd::AdskSceneMetadataCmd()
: fMode(kEdit)
{
}
AdskSceneMetadataCmd::~AdskSceneMetadataCmd()
{
}
void* AdskSceneMetadataCmd::creator()
{
return new AdskSceneMetadataCmd();
}
MSyntax AdskSceneMetadataCmd::cmdSyntax()
{
return syntax;
}
const char* AdskSceneMetadataCmd::name()
{
return "adskSceneMetadataCmd";
}
{
if ( !status )
{
return status;
}
if (argsDb.isEdit())
{
if (argsDb.isQuery())
{
return MS::kFailure;
}
fMode = kEdit;
}
else if (argsDb.isQuery())
{
fMode = kQuery;
}
fChannelName.parse ( argsDb, "-channelName" );
fData.parse ( argsDb, "-data" );
if ( !fData.isModeValid(fMode) )
{
return MS::kFailure;
}
if ( !fChannelName.isSet() || !fChannelName.isArgValid() || fChannelName.arg() == "" )
{
return MS::kFailure;
}
status = argsDb.getObjects( objs );
switch ( fMode )
{
case kEdit: status = doEdit( scenePath ); break;
case kQuery: status = doQuery( scenePath ); break;
default: break;
}
return status;
}
{
return setMetadata( scenePath );
}
{
return getMetadata( scenePath );
}
{
std::string errors;
std::auto_ptr< Accessor > accessor( getAccessorForScene( scenePath ) );
if ( NULL == accessor.get() )
{
return MS::kFailure;
}
if ( MS::kSuccess != getSceneAssociations(*(accessor.get()), associations) )
{
return MS::kFailure;
}
std::string channelName( fChannelName.arg().asChar() );
if ( NULL == channel )
{
setResult( "" );
return MS::kSuccess;
}
if ( 0 == channel->dataStreamCount() )
{
errorString.
format( errorString, fChannelName.arg() );
displayError( errorString );
return MS::kFailure;
}
Stream *stream = channel->dataStream( 0 );
{
errorString.
format( errorString, fChannelName.arg() );
displayError( errorString );
return MS::kFailure;
}
{
errorString.
format( errorString, gMemberName, fChannelName.arg() );
displayError( errorString );
return MS::kFailure;
}
if ( Member::kString != handle.
dataType() )
{
errorString.
format( errorString, fChannelName.arg() );
displayError( errorString );
return MS::kFailure;
}
setResult( strings[0] );
return MS::kSuccess;
}
{
std::string errors;
std::auto_ptr< Accessor > accessor( getAccessorForScene( scenePath ) );
if ( NULL == accessor.get() )
{
setResult( false );
return MS::kFailure;
}
if ( MS::kSuccess != getSceneAssociations(*(accessor.get()), associations) )
{
setResult( false );
return MS::kFailure;
}
for ( Structure::ListIterator itrStructList = Structure::allStructures().begin(); itrStructList != Structure::allStructures().end(); ++itrStructList )
{
if ( std::string((*itrStructList)->name()) == std::string(gStructureName) )
{
structure = *itrStructList;
break;
}
}
if ( NULL == structure )
{
structure = Structure::create();
structure->
setName( gStructureName );
structure->
addMember( Member::kString, 1, gMemberName );
Structure::registerStructure( *structure );
}
if ( accessorStructures.find( structure ) == accessorStructures.end() )
{
updatedStructures.insert( structure );
accessor->setStructures( updatedStructures );
}
std::string channelName( fChannelName.arg().asChar() );
Stream stream( *structure, std::string( gStreamName ) );
channel.setDataStream( stream );
std::string dataString( fData.arg().asChar() );
if ( 0 != handle.
fromStr( dataString, 0, errors ) )
{
errorString.
format( errorString, fChannelName.arg(), errors.c_str() );
displayError( errorString );
setResult( false );
return MS::kFailure;
}
if ( !accessor->write( errors ) )
{
displayError( errorString );
setResult( false );
return MS::kFailure;
}
setResult( true );
return MS::kSuccess;
}
std::auto_ptr< Accessor > AdskSceneMetadataCmd::getAccessorForScene(
MString scenePath )
{
std::string errors;
std::string stdScenePath( scenePath.
asChar() );
std::auto_ptr< Accessor > accessor( Accessor::accessorByExtension( stdScenePath ) );
if ( NULL == accessor.get() )
{
errorString.
format( errorString, scenePath );
displayError( errorString );
return std::auto_ptr< Accessor >();
}
std::set< std::string > wantedAssociations;
wantedAssociations.insert( AccessorMaya::getSceneAssociationsName() );
if ( !accessor->read( stdScenePath,
NULL,
&wantedAssociations,
errors ) )
{
errorString.
format( errorString, scenePath, errors.c_str() );
displayError( errorString );
return std::auto_ptr< Accessor >();
}
return accessor;
}
{
Accessor::AssociationsMap::const_iterator iterAssociations = associationsMap.find( AccessorMaya::getSceneAssociationsName() );
if ( iterAssociations == associationsMap.end() )
{
out_associations = Associations::create();
accessor.
associations()[AccessorMaya::getSceneAssociationsName()] = out_associations;
}
else
{
out_associations = (*iterAssociations).second;
}
return MS::kSuccess;
}