xmlAssembly/FnAssetInstance.cpp
 
 
 
xmlAssembly/FnAssetInstance.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 "FnAssetInstance.h"

#include "FnAssetInstanceUtil.h"
#include "AssetInstance.h"
#include "AssetNode.h"
#include "Representation.h"
#include "XMLStrings.h"
#include "XMLRepresentation.h"

#include <maya/MObject.h>
#include <maya/MObjectArray.h>
#include <maya/MPlug.h>
#include <maya/MString.h>
#include <maya/MTransformationMatrix.h>
#include <maya/MStringArray.h>
#include <maya/MSelectionList.h>
#include <maya/MDagModifier.h>
#include <maya/MGlobal.h>

#include <maya/MFnTypedAttribute.h>
#include <maya/MFnEnumAttribute.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTransform.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnData.h>
#include <maya/MFnStringArrayData.h>

#include <maya/MNodeMessage.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnAssembly.h>


MObject FnAssetInstance::create(const AssetInstance* assetInstanceName, MObject& parent)
{
        //create container
        const MString& name = assetInstanceName->getName();
    MString cmdString = "container -type XMLAssembly -name \"";
    cmdString += name;
    cmdString += "\"";

    MString newCreatedNodePath;
    MGlobal::executeCommand( cmdString, newCreatedNodePath);

    //get the container obj;
    MSelectionList selList;
    selList.add( newCreatedNodePath);
    MObject container;
    selList.getDependNode( 0, container);

    //parent the new created container to his parent
    MDagModifier dagModifier;
    dagModifier.reparentNode( container, parent);
    dagModifier.doIt();

        MTransformationMatrix xform;
        assetInstanceName->getXform(xform);
    FnAssetInstance::setXform( container, xform);
        
        FnAssetInstance::setGUID( container, assetInstanceName->getGuid());
        FnAssetInstance::setVisibility( container, assetInstanceName->getVisibility());

        return container;
}

void FnAssetInstance::setXform( MObject& assetInstance, const MTransformationMatrix& xform)
{
        MFnTransform transFn( assetInstance);
        transFn.set( xform);
}

MTransformationMatrix FnAssetInstance::getXform( const MObject& assetInstnce)
{
        MFnTransform transFn( assetInstnce);
        MTransformationMatrix tMatrix = transFn.transformation();
        return tMatrix;
}

void FnAssetInstance::setActiveRep( MObject& assetInstance, const MString& activeRep)
{
    if( !FnAssetInstanceUtil::isAssetInstance( assetInstance))
    {
        return;
    }
        
    FnAssetInstanceUtil::setAttributeValue( assetInstance, XMLStrings::akwActiveRepLong, activeRep);
}

MString FnAssetInstance::getActiveRep( const MObject& assetInstance)
{
    MString result;

    if(assetInstance.isNull())
    {
        return result;
    }

    FnAssetInstanceUtil::getAttributeValue( assetInstance, XMLStrings::akwActiveRepLong, result);
    return result;
}

void FnAssetInstance::setGUID( MObject& assetInstance, const MString& guid)
{
        FnAssetInstanceUtil::setAttributeValue( assetInstance, XMLStrings::akwGuidLong, guid);
}

MString FnAssetInstance::getGUID( const MObject& assetInstance)
{
        MString result;
        FnAssetInstanceUtil::getAttributeValue( assetInstance, XMLStrings::akwGuidLong, result);
        return result;
}

void FnAssetInstance::setSwitchType( MObject& assetInstance, bool isDelete)
{
        FnAssetInstanceUtil::setAttributeValue( assetInstance, XMLStrings::akwSwitchRepTypeLong, isDelete);
}


bool FnAssetInstance::getSwitchType( const MObject& assetInstance)
{
        bool result;
        FnAssetInstanceUtil::getAttributeValue( assetInstance, XMLStrings::akwSwitchRepTypeLong, result);
        return result;
}

void FnAssetInstance::setVisibility( MObject& assetInstance, bool visibility)
{
        FnAssetInstanceUtil::setAttributeValue( assetInstance, XMLStrings::akwVisiblityLong, visibility);
}

bool FnAssetInstance::getVisibility( const MObject& assetInstance)
{
        bool result;
        FnAssetInstanceUtil::getAttributeValue( assetInstance, XMLStrings::akwVisiblityLong, result);
        return result;
}


//
//   Methods for Representation
//

bool FnAssetInstance::addRepInfo(MObject& assetInstance, const Representation& representation)
{
        if( !FnAssetInstanceUtil::isAssetInstance( assetInstance))
        {
                return false;
        }
        
        const MString& repName = representation.getName();
        MStringArray result;
        
        if (FnAssetInstance::findRepInfo( assetInstance, repName, result))
        {
                return false;
        }
        else
        {
                MStringArray repInfo;
                repInfo.setLength( Representation::kRepSegmentNum);
                repInfo[ Representation::kRepGuid] = representation.getNodeGuid();
                repInfo[ Representation::kRepType] = representation.getType();
                repInfo[ Representation::kRepName] = representation.getName();
        MString repData = representation.getData();
        if( repData.length() > 0)
        {
                    repInfo[ Representation::kRepData] = representation.getData();
        }
                
                MFnDagNode node( assetInstance);
                MPlug attributePlug = node.findPlug( XMLStrings::akwRepListLong);
                attributePlug.evaluateNumElements();
                unsigned int listSize = attributePlug.numElements();
                
                MPlug newPlug = attributePlug.elementByLogicalIndex( listSize);

                static MFnStringArrayData data;
                MObject dataHandle = data.create( repInfo);
                newPlug.setValue( dataHandle);
        }
        
        return true;
}

bool FnAssetInstance::removeRepInfo( MObject& assetInstance, const MString& guid )
{
    if( !FnAssetInstanceUtil::isAssetInstance( assetInstance))
    {
        return false;
    }

    MFnDagNode node( assetInstance);
    MPlug attributePlug = node.findPlug( XMLStrings::akwRepListLong);
    attributePlug.evaluateNumElements();
    unsigned int listSize = attributePlug.numElements();

    MObject dataHandle = MObject::kNullObj;
    MPlug elementPlug;
    unsigned int foundRepInfoIndex = 0;

    // Find the index of the representation in attribute list.
    for( unsigned int i = 0; i < listSize; i++)
    {
        elementPlug = attributePlug.elementByLogicalIndex(i);
        elementPlug.getValue( dataHandle);
        MFnStringArrayData data( dataHandle);
        MStringArray repInfo;
        data.copyTo( repInfo);
        if (repInfo[Representation::kRepGuid] == guid)
        {
            foundRepInfoIndex = i;
            break;
        }
    }

    MObject nextDataHandle = MObject::kNullObj;
    MPlug nextElementPlug;

    // Move attributes one step ahead.
    // 
    for (unsigned int i = foundRepInfoIndex; i < listSize - 1; i++)
    {
        elementPlug = attributePlug.elementByLogicalIndex(i);
        nextElementPlug = attributePlug.elementByLogicalIndex(i + 1);
        nextElementPlug.getValue( nextDataHandle);
        elementPlug.setValue( nextDataHandle);
    }

    // Remove the last attribute
    // 
    elementPlug = attributePlug.elementByLogicalIndex(listSize - 1);
    MString attrName = elementPlug.partialName(true, true, true);
    MString melCmd = "removeMultiInstance -break true ";
    melCmd += attrName;

    MGlobal::executeCommand(melCmd);

    return true;
}

void FnAssetInstance::getRepInfo(const MObject& assetInstance, MStringArray& outRepInfos)
{
        MFnDagNode node( assetInstance);
        MPlug attributePlug = node.findPlug( XMLStrings::akwRepListLong);
        attributePlug.evaluateNumElements();
        unsigned int listSize = attributePlug.numElements();

        MObject dataHandle = MObject::kNullObj;
        for( unsigned int i = 0; i < listSize; ++i)
        {
                MPlug elementPlug = attributePlug.elementByLogicalIndex( i);
                elementPlug.getValue( dataHandle);
                MFnStringArrayData data( dataHandle);
                MStringArray repInfo;
                data.copyTo( repInfo);
                for ( unsigned int repIndex = 0; repIndex < repInfo.length(); repIndex++)
                {
                        outRepInfos.append(repInfo[repIndex]);
                }
        }
}

void FnAssetInstance::getRepGuidList(const MObject& assetInstance, MStringArray& outRepGuids)
{
    MFnDagNode node( assetInstance);
    MPlug attributePlug = node.findPlug( XMLStrings::akwRepListLong);
    attributePlug.evaluateNumElements();
    unsigned int listSize = attributePlug.numElements();

    MObject dataHandle = MObject::kNullObj;
    for( unsigned int i = 0; i < listSize; ++i)
    {
        MPlug elementPlug = attributePlug.elementByLogicalIndex( i);
        elementPlug.getValue( dataHandle);
        MFnStringArrayData data( dataHandle);
        MStringArray repInfo;
        data.copyTo( repInfo);
        outRepGuids.append(repInfo[Representation::kRepGuid]);
    }
}

void FnAssetInstance::getRepNameList(const MObject& assetInstance, MStringArray& outRepNames)
{
    MFnDagNode node( assetInstance);
    MPlug attributePlug = node.findPlug( XMLStrings::akwRepListLong);
    attributePlug.evaluateNumElements();
    unsigned int listSize = attributePlug.numElements();

    MObject dataHandle = MObject::kNullObj;
    for( unsigned int i = 0; i < listSize; ++i)
    {
        MPlug elementPlug = attributePlug.elementByLogicalIndex( i);
        elementPlug.getValue( dataHandle);
        MFnStringArrayData data( dataHandle);
        MStringArray repInfo;
        data.copyTo( repInfo);
        outRepNames.append(repInfo[Representation::kRepName]);
    }
}



bool FnAssetInstance::findRepInfo(const MObject& assetInstance, const MString& info, MStringArray& result)
{
        MFnDagNode node( assetInstance);
        MPlug attributePlug = node.findPlug( XMLStrings::akwRepListLong);
        attributePlug.evaluateNumElements();
        unsigned int listSize = attributePlug.numElements();

        MObject dataHandle = MObject::kNullObj;
        for( unsigned int i = 0; i < listSize; ++i)
        {
                MPlug elementPlug = attributePlug.elementByLogicalIndex( i);
                elementPlug.getValue( dataHandle);
                MFnStringArrayData data( dataHandle);
                for( unsigned int j = 0; j < data.length(); ++j)
                {
                        if( info == data[ j])
                        {
                                data.copyTo( result);
                                return true;
                        }
                }
        }
        
        return false;
}

MString FnAssetInstance::getRepNodeGuid( const MObject& assetInstance, const MString& repType)
{
        MString result;

        MStringArray repInfo;
        if (FnAssetInstance::findRepInfo( assetInstance, repType, repInfo))
        {
                result = repInfo[ Representation::kRepGuid];
        }
        
        return result;
}

MString FnAssetInstance::getRepType( const MObject& assetInstance, const MString& representation)
{
        if( representation.length() > 0)
    {
        MStringArray repInfo;
        if (FnAssetInstance::findRepInfo( assetInstance, representation, repInfo))
                        return repInfo[ Representation::kRepType];
    }
        
    return "";
}