MStatus PRTAttrs::addParameter(MFnDependencyNode & node, MObject & attr, MFnAttribute& tAttr) { if(!(node.hasAttribute(tAttr.shortName()))) { MCHECK(tAttr.setKeyable (true)); MCHECK(tAttr.setHidden(false)); MCHECK(tAttr.setStorable(true)); MCHECK(node.addAttribute(attr)); } return MS::kSuccess; }
//--------------------------------------------------- MPlug DagHelper::addAttribute ( const MObject& node, const MObject& attribute ) { MPlug plug; MFnAttribute attributeFn ( attribute ); MFnDependencyNode depFn ( node ); MStatus status = depFn.addAttribute ( attribute, MFnDependencyNode::kLocalDynamicAttr ); if ( status == MStatus::kSuccess ) { plug = depFn.findPlug ( attribute ); } return plug; }
MStatus Maya::SetNodeID( const MObject& node, const Helium::TUID& id ) { MStatus status; MFnDependencyNode nodeFn (node); // make sure we have the dynamic attribute to store our id created MObject attr = nodeFn.attribute(MString (s_TUIDAttributeName), &status); if (status == MS::kFailure) { // check to see if we are a locked node bool nodeWasLocked = nodeFn.isLocked(); if ( nodeWasLocked ) { // turn off any node locking so an attribute can be added nodeFn.setLocked( false ); } // create the attribute MFnTypedAttribute attrFn; attr = attrFn.create(MString (s_TUIDAttributeName), MString (s_TUIDAttributeName), MFnData::kString); status = nodeFn.addAttribute(attr); // reset to the prior state of wasLocked if ( nodeWasLocked ) { nodeFn.setLocked( nodeWasLocked ); } // error check if (status == MS::kFailure) { MGlobal::displayError(MString ("Unable to create TUID attribute on maya node: ") + nodeFn.name()); return status; } } MPlug plug (node, nodeFn.attribute(MString (s_TUIDAttributeName))); plug.setLocked(false); tstring s; id.ToString(s); plug.setValue(MString(s.c_str())); plug.setLocked(true); return status; }
//--------------------------------------------------- MObject DagHelper::createAttribute ( const MObject& node, const char* attributeName, const char* attributeShortName, MFnData::Type type, const char *value ) { // Before creating a new attribute: verify that an old one doesn't already exist MStatus status; MObject attribute; MFnDependencyNode nodeFn ( node ); MPlug plug = nodeFn.findPlug ( attributeShortName, status ); if ( status != MStatus::kSuccess ) { MFnTypedAttribute attr; MStatus status; attribute = attr.create ( attributeName,attributeShortName,type,&status ); if ( status != MStatus::kSuccess ) return MObject::kNullObj; attr.setStorable ( true ); attr.setKeyable ( false ); attr.setCached ( true ); attr.setReadable ( true ); attr.setWritable ( true ); status = nodeFn.addAttribute ( attribute, MFnDependencyNode::kLocalDynamicAttr ); if ( status != MStatus::kSuccess ) return MObject::kNullObj; plug = nodeFn.findPlug ( attribute, &status ); if ( status != MStatus::kSuccess ) return MObject::kNullObj; } status = plug.setValue ( value ); if ( status != MStatus::kSuccess ) return MObject::kNullObj; return attribute; }
MStatus testGlassCmd::doIt(const MArgList &args) { MStatus stat=MStatus::kSuccess; MSelectionList list; MFnDependencyNode meshDependFn; MFnDependencyNode materialDependFn; MDGModifier dgMod; //create a new material node, here is the test glass node MObject testGNode=dgMod.createNode(testGlassNode::id); materialDependFn.setObject(testGNode); MString testName=materialDependFn.name(); MGlobal::displayInfo(testName+" god, please give me the node first time"); //find the mesh node(s) from selected object(s) //create a new "yafaray material" attribute if the mesh node(s) dont have //then conect the material node to the mesh node(s) MGlobal::getActiveSelectionList( list ); MItSelectionList iter( list, MFn::kMesh ); for( ; !iter.isDone(); iter.next()) { MObject meshDependNode; MFnNumericAttribute numAttr; iter.getDependNode( meshDependNode ); meshDependFn.setObject( meshDependNode ); //MString dependName=dependFn.name(); //MGlobal::displayInfo(dependName+" is here\n"); if( !meshDependFn.hasAttribute("YafarayMaterial") ) { MObject attrYafarayMaterial=numAttr.create("YafarayMaterial","yama",MFnNumericData::kBoolean); numAttr.setDefault( true ); numAttr.setStorable( true ); meshDependFn.addAttribute(attrYafarayMaterial,MFnDependencyNode::kLocalDynamicAttr); } //find the source plug and the result plug, then connect them //if the result plug as already had a sourse plug connected to it, disconnect first MPlug glassPlug=materialDependFn.findPlug("OutGlass"); MPlug meshPlug=meshDependFn.findPlug("YafarayMaterial"); MPlugArray srcMeshPlug; if(meshPlug.connectedTo(srcMeshPlug, true, false)) { MPlug srcPlug; //if(srcMeshPlug.length!=0) //{ srcPlug=srcMeshPlug[0]; //} dgMod.disconnect(srcPlug, meshPlug); } dgMod.connect(glassPlug,meshPlug); } dgMod.doIt(); //why still cant got the name here? MGlobal::displayInfo(testName+" god, please give me the node second time"); return stat; }