filteredAsciiFile.cpp
#include <maya/MPxMayaAsciiFilter.h>
#include <maya/MFnPlugin.h>
#include <maya/MFileIO.h>
#include <maya/MFileObject.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MSyntax.h>
#include <maya/MArgDatabase.h>
#include <maya/MPxCommand.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <vector>
#define kFileNameFlag "-fn"
#define kFileNameFlagLong "-fileName"
#define commandName "customSourceFile"
class filteredAsciiFile : public MPxMayaAsciiFilter
{
public:
static void* creator();
static void setPluginName(const MString& name);
static MString translatorName();
MString defaultExtension() const;
filteredAsciiFile();
static MString fExtension;
static MString fPluginName;
static MString fTranslatorName;
protected:
MStatus processReadOptions(const MString& optionsString);
MStatus processWriteOptions(const MString& optionsString);
bool writesRequirements() const;
bool writesCreateNode( const MObject& node ) const;
bool writesSelectNode( const MObject& node ) const;
bool writesFileReference( const MFileObject& referenceFile ) const;
bool writesConnectAttr( const MPlug& srcPlug,
const MPlug& destPlug ) const;
bool writesSetAttr( const MPlug& srcPlug ) const;
MStatus writePostRequires( MPxMayaAsciiFilterOutput& fileIO );
MStatus writePostCreateNodesBlock( MPxMayaAsciiFilterOutput& fileIO );
private:
bool isNodeTypeIncluded(const MString& type) const;
bool isNodeNameExcluded(const MObject& node) const;
MStringArray includedNodeTypesArray;
MStringArray excludedNodeTypesArray;
MStringArray sourcedFilesArray;
MStringArray excludedNamesArray;
bool outputRequirements;
bool outputReferences;
};
MString filteredAsciiFile::fExtension = "faf";
MString filteredAsciiFile::fPluginName = "";
MString filteredAsciiFile::fTranslatorName = "filteredAsciiFile";
inline void filteredAsciiFile::setPluginName(const MString& name)
{ fPluginName = name; }
inline MString filteredAsciiFile::translatorName()
{ return fTranslatorName; }
inline MString filteredAsciiFile::defaultExtension() const
{ return fExtension; }
filteredAsciiFile::filteredAsciiFile() :
includedNodeTypesArray()
, outputRequirements(false)
, outputReferences(true)
{
}
MStatus filteredAsciiFile::processReadOptions(const MString& optionsString)
{
return MStatus::kSuccess;
}
MStatus filteredAsciiFile::processWriteOptions(const MString& optionsString)
{
MStringArray optionsArray;
includedNodeTypesArray.clear();
excludedNodeTypesArray.clear();
sourcedFilesArray.clear();
excludedNamesArray.clear();
outputRequirements = true;
outputReferences = true;
optionsString.split(';', optionsArray);
for (unsigned i = 0; i < optionsArray.length(); i++)
{
const MString& option = optionsArray[i];
MStringArray optionArray;
option.split(' ', optionArray);
if (optionArray[0] == "includeNodeType" && optionArray.length() > 1)
{
includedNodeTypesArray.append(optionArray[1]);
}
else if (optionArray[0] == "excludeNodeType" && optionArray.length() > 1)
{
excludedNodeTypesArray.append(optionArray[1]);
}
else if (optionArray[0] == "sourceFile" && optionArray.length() > 1)
{
sourcedFilesArray.append(optionArray[1]);
}
}
return MStatus::kSuccess;
}
void* filteredAsciiFile::creator()
{
return new filteredAsciiFile();
}
bool filteredAsciiFile::writesRequirements() const
{
return outputRequirements;
}
bool filteredAsciiFile::writesCreateNode( const MObject& node ) const
{
bool result = false;
MFnDependencyNode depNode(node);
if (depNode.isFromReferencedFile() && !outputReferences)
{
return false;
}
if (isNodeNameExcluded(node))
{
return false;
}
if (!result)
{
result = isNodeTypeIncluded(node.apiTypeStr());
}
return result;
}
bool filteredAsciiFile::writesConnectAttr( const MPlug& srcPlug,
const MPlug& destPlug ) const
{
return (writesCreateNode(srcPlug.node()) && !isNodeNameExcluded(destPlug.node()));
}
bool filteredAsciiFile::writesSelectNode( const MObject& node ) const
{
return writesCreateNode(node);
}
bool filteredAsciiFile::writesFileReference( const MFileObject& referenceFile ) const
{
return outputReferences;
}
bool filteredAsciiFile::writesSetAttr( const MPlug& srcPlug ) const
{
return writesCreateNode(srcPlug.node());
}
MStatus filteredAsciiFile::writePostRequires( MPxMayaAsciiFilterOutput& fileIO )
{
fileIO << "requires " << fPluginName.asChar() << " \"1.0\";\n";
return MS::kSuccess;
}
MStatus filteredAsciiFile::writePostCreateNodesBlock( MPxMayaAsciiFilterOutput& fileIO )
{
for (unsigned int i = 0; i < sourcedFilesArray.length(); i++)
{
fileIO << "eval (\"customSourceFile -fileName \\\"";
fileIO << sourcedFilesArray[i].asChar();
fileIO << "\\\";\");\n";
}
return MS::kSuccess;
}
bool filteredAsciiFile::isNodeTypeIncluded(const MString& nodeType) const
{
if ( (excludedNodeTypesArray.length() == 0 ) &&
(includedNodeTypesArray.length() == 0 ) )
{
return true;
}
bool result = (excludedNodeTypesArray.length() > 0);
for (unsigned int i = 0; i < includedNodeTypesArray.length(); i++)
{
if (nodeType == includedNodeTypesArray[i])
{
result = true;
break;
}
}
for (unsigned int j = 0; j < excludedNodeTypesArray.length(); j++)
{
if (nodeType == excludedNodeTypesArray[j])
{
result = false;
break;
}
}
return result;
}
bool filteredAsciiFile::isNodeNameExcluded(const MObject& node) const
{
for (unsigned int i = 0; i < excludedNamesArray.length(); i++)
{
MFnDependencyNode depNode(node);
if (excludedNamesArray[i] == depNode.name())
{
return true;
}
}
return false;
}
class sourceFileCmd : public MPxCommand
{
public:
sourceFileCmd();
virtual ~sourceFileCmd();
MStatus doIt( const MArgList& args );
static MSyntax newSyntax();
static void* creator();
private:
};
sourceFileCmd::sourceFileCmd()
{
}
sourceFileCmd::~sourceFileCmd()
{
}
MStatus
sourceFileCmd::doIt (const MArgList& args )
{
MString fileName;
MStatus status = MS::kSuccess;
MArgDatabase argData(syntax(), args);
if (argData.isFlagSet(kFileNameFlag))
{
status = argData.getFlagArgument(kFileNameFlag, 0, fileName);
if (status == MS::kSuccess)
{
MString currFile = MFileIO::fileCurrentlyLoading();
MStringArray pathDirectories;
currFile.split ('/', pathDirectories);
if (pathDirectories.length() > 0)
{
MString expandedFileName;
for (unsigned int i = 0; i < pathDirectories.length()-1; i++)
{
expandedFileName += pathDirectories[i];
expandedFileName += "/";
}
expandedFileName += fileName;
MGlobal::sourceFile(expandedFileName);
}
}
}
return status;
}
void*
sourceFileCmd::creator()
{
return (void*) new sourceFileCmd();
}
MSyntax sourceFileCmd::newSyntax()
{
MSyntax syntax;
syntax.addFlag(kFileNameFlag, kFileNameFlagLong, MSyntax::kString);
return syntax;
}
MStatus initializePlugin(MObject obj)
{
MFnPlugin plugin(obj, PLUGIN_COMPANY, "1.0", "Any");
filteredAsciiFile::setPluginName(plugin.name());
plugin.registerFileTranslator(
filteredAsciiFile::translatorName(),
NULL,
filteredAsciiFile::creator,
NULL,
NULL,
false
);
MStatus status = plugin.registerCommand( commandName,
sourceFileCmd::creator,
sourceFileCmd::newSyntax);
return MS::kSuccess;
}
MStatus uninitializePlugin(MObject obj)
{
MFnPlugin plugin( obj );
plugin.deregisterFileTranslator(filteredAsciiFile::translatorName());
return MS::kSuccess;
}