xmlAssembly/FnAssetInstanceUtil.cpp
 
 
 
xmlAssembly/FnAssetInstanceUtil.cpp
//-
//**************************************************************************/
// Copyright 2012 Autodesk, Inc. All rights reserved. 
//
// Use of this software is subject to the terms of the Autodesk 
// license agreement provided at the time of installation or download, 
// or which otherwise accompanies this software in either electronic 
// or hard copy form.
//**************************************************************************/
//+

#include "FnAssetInstanceUtil.h"
#include "FnAssetInstance.h"
#include "AssetGuidManager.h"
#include "XMLStrings.h"
#include "AssemblyStrings.h"

#include <maya/MObject.h>
#include <maya/MObjectArray.h>
#include <maya/MPlug.h>
#include <maya/MDagPath.h>
#include <maya/MPointArray.h>
#include <maya/MIntArray.h>
#include <maya/MGlobal.h>

#include <maya/MFnContainerNode.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnMesh.h>
#include <maya/MDagModifier.h>
#include <maya/MStringResource.h>

namespace FnAssetInstanceUtil
{

/*---------------------------------Util function-------------------------------------------------*/


void getAttributePlug( const MObject& container, const MString& attributeName, MPlug& outPlug)
{
    if(container.isNull())
    {
        return;
    }
    
    MStatus status;
    MString strErrorString = "";

    MFnDependencyNode depNode( container, &status);
    if ( MS::kSuccess != status ) {
        strErrorString = MStringResource::getString(rFailToGetContainerFunctionErrorMsg, status);
        MGlobal::displayError(strErrorString);
        return;
    }

    MObject attr = depNode.attribute( attributeName, &status);
    if (status == MS::kSuccess)
        outPlug = MPlug( container, attr);
}

void containerManipulator( MObject& container, const MObjectArray& nodes, bool isAdd, bool isForce)
{
    bool isInstance = isAssetInstance( container);
    unsigned int nodesLength = nodes.length();
    if( isInstance && nodesLength > 0)
    {
        MString flag;
        if( isAdd)
        {
            flag = "-addNode";
        }
        else
        {
            flag = "-removeNode";
        }
        MString cmd = "container -edit ";
        if( isForce)
        {
            cmd += "-force ";
        }
        cmd += flag + " {\"";
        for( unsigned int i = 0; i < nodesLength; ++i)
        {
            MFnDagNode node( nodes[ i]);
            cmd += node.fullPathName();
            if( i < nodesLength - 1)
            {
                cmd += "\",\"";
            }
        }
        cmd += "\"} ";
        MDagPath dagPath = MDagPath::getAPathTo(container);
        cmd += dagPath.fullPathName();
        //MFnContainerNode container( container);
        //cmd += container.name();

        bool isSuccess = false;
        MGlobal::executeCommand( cmd, isSuccess);
    }
}

bool addNodes( MObject& containerName, const MObjectArray& nodes, bool isForce)
{
    bool isInstance = isAssetInstance( containerName);
    unsigned int nodesLength = nodes.length();
    if( isInstance && nodesLength > 0)
    {
        containerManipulator( containerName, nodes, true, isForce);
        return true;
    }

    return false;
}

bool addNodes( MObject& containerName, const MObject& node, bool isForce)
{
    MObjectArray nodeList;
    nodeList.append( node);
    return addNodes( containerName, nodeList, isForce);
}

void removeNodes( MObject& container, MObjectArray& nodeList)
{
    MObject child = MObject::kNullObj;
    for( unsigned int i = 0; i < nodeList.length(); ++i)
    {
        child = nodeList[ i];
        //remove the node from theGuidManager
        if( isAssetInstance( child))
        {
            MFnContainerNode containerNode( child);
            MObjectArray childNodeList;
            containerNode.getMembers( childNodeList);
            removeNodes( child, childNodeList);
        }

        if( child.hasFn(MFn::kTransform))
        {
            theGuidManager.removeItemByObj( child);    
            MGlobal::deleteNode(child);

            // FIXME: comment this out until MAYA-11460 is fixed.
            //modifier.deleteNode( child);
            //modifier.doIt();
            //~
        }
    }
}

void hideNodes( MObject& containerName, MObjectArray& nodeList)
{
    for( unsigned int i = 0; i < nodeList.length(); ++i)
    {
        setAttributeValue( nodeList[ i], XMLStrings::akwVisiblityLong, false);
    }
}

void showNodes( MObject& containerName, MObjectArray& nodeList)
{
    for( unsigned int i = 0; i < nodeList.length(); ++i)
    {
        setAttributeValue( nodeList[ i], XMLStrings::akwVisiblityLong, true);
    }
}

bool isAssetInstance( const MObject& dagNode)
{
    MFnDependencyNode node(dagNode);
    MString typeName = node.typeName();
    if( MObject::kNullObj != dagNode && typeName == "XMLAssembly")
        return true;

    return false;
}

void findAssetMembers( const MObject& containerName, const MObjectArray& nodeList, MObjectArray& results)
{
        if( isAssetInstance( containerName))
        {
                MStatus status;
                MString strErrorString = "";
                MFnContainerNode node( containerName, &status);
                if ( MS::kSuccess != status ) {
                        strErrorString = MStringResource::getString(rFailToGetContainerNodeErrorMsg, status);
                        MGlobal::displayError(strErrorString);
                }
                MObjectArray members;
                status = node.getMembers( members);
                if ( MS::kSuccess != status ) {
                        strErrorString = MStringResource::getString(rFailToGetContainerMemberErrorMsg, status);
                        MGlobal::displayError(strErrorString);
                }
                getIntersection( members, nodeList, results);
        }
}


/*---------------------------------Util function-------------------------------------------------*/
}