Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / SDK / Hello All, Please I need some technical help, Maya API?
  RSS 2.0 ATOM  

Hello All, Please I need some technical help, Maya API?
Rate this thread
 
58266
 
Permlink of this thread  
avatar
  • Shylon
  • Posted: 21 July 2011 05:44 AM
  • Total Posts: 16
  • Joined: 15 December 2006 04:24 PM

Hi, I writing a node for Maya, and actually I encountor a strange problem,getting values from MFnCompoundAttribute in compute() function, please see this tester code, when I want to use it in Maya for example connect output to a transform object, when i change parameters the transform move strange, I did whatever you thing, but I still have problem, I would be so appreciated if you help me?
Thank you.

#pragma once
#include <maya/MPxNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnGenericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnCompoundAttribute.h>
#include <maya/MVector.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MTypeId.h>

//---------------------------------------------------------------------------
// Test Node class
//---------------------------------------------------------------------------
class TestNode : public MPxNode
{
public:
TestNode();
virtual ~TestNode();

virtual MStatus compute( const MPlug& plug, MDataBlock& data );

static void* creator();
static MStatus initialize();

public:

static MObject input1;
static MObject input2;
static MObject input3;
static MObject output;
static MObject compTrgt;

static MTypeId id;
};

#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>

//
#include “TestNode.h”

// Static members
MTypeId TestNode::id( 0x81c00 ); //something for test
MObject TestNode::input1;
MObject TestNode::input2;
MObject TestNode::input3;
MObject TestNode::output;
MObject TestNode::compTrgt;
//-------------------------------------------------------

TestNode::TestNode()
{
}
TestNode::~TestNode()
{
}
//---------------------------------------------------------------------------
//
//
//---------------------------------------------------------------------------
void* TestNode::creator()
{
return new TestNode();
}

//---------------------------------------------------------------------------
// Initialize()
//
//---------------------------------------------------------------------------
MStatus TestNode::initialize()
{
MStatus stat;
MFnNumericAttribute nAttr;

input1 = nAttr.createPoint( “input1”, “in1” );
nAttr.setKeyable(true);

input2 = nAttr.createPoint( “input2”, “in2” );
nAttr.setKeyable(true);

input3 = nAttr.createPoint( “input3”, “in3");
nAttr.setKeyable(true);

//------------------
MFnCompoundAttribute compAttr;
compTrgt = compAttr.create("compTrgt", “ctgt");

compAttr.addChild(input1);
compAttr.addChild(input2);
compAttr.addChild(input3);
compAttr.setArray(true);
//compAttr.setUsesArrayDataBuilder(true);

//compAttr.setIndexMatters(true);

// Output
output = nAttr.createPoint( “output”, “out” );
nAttr.setReadable(true);
nAttr.setWritable(false);
nAttr.setStorable(false);

// Add the attributes we have created to the node
stat = addAttribute( compTrgt );
if (!stat) { stat.perror("addAttribute"); return stat;}

stat = addAttribute( output );
if (!stat) { stat.perror("addAttribute"); return stat;}

//Connect effect
/*
stat = attributeAffects( input3, output );
if (!stat) { stat.perror("attributeAffects"); return stat;}
*/
stat = attributeAffects( compTrgt, output );
if (!stat) { stat.perror("attributeAffects"); return stat;}

return MS::kSuccess;

}
//---------------------------------------------------------------------------
// Compute()
//---------------------------------------------------------------------------
MStatus TestNode::compute( const MPlug& plug, MDataBlock& data )

{
MStatus returnStatus;

if( plug ==TestNode::output )
{
MVector result;

//MObject currNode = thisMObject();
//MPlug plugin1( currNode, input1);
//MPlug plugin2( currNode, input2);
//MPlug plugin3( currNode, input3);

MArrayDataHandle inputArrayData = data.inputArrayValue( compTrgt, &returnStatus );

if( returnStatus != MS::kSuccess )
MGlobal::displayError( “Node TestNode cannot get value\n” );

else
{
unsigned int i, count = inputArrayData.elementCount();
for(i =0; i < count; i++)
{
//inputArrayData.jumpToArrayElement(i);
MVector in1, in2, in3;
//
MDataHandle currElementHandle = inputArrayData.inputValue( &returnStatus );

//
in1 = currElementHandle.child(input1).asVector();
//MGlobal::displayInfo( MString("in1.x = “) + in1.x + MString(” in1.y = “) + in1.y + MString(” in1.z = “) + in1.z );
in2 = currElementHandle.child(input2).asVector();
//MGlobal::displayInfo( MString("in2.x = “) + in2.x + MString(” in2.y = “) + in2.y + MString(” in2.z = “) + in2.z );
in3 = currElementHandle.child(input3).asVector();
//MGlobal::displayInfo( MString("in3.x = “) + in3.x + MString(” in3.y = “) + in3.y + MString(” in3.z = “) + in3.z );

result += in1 + in2+ in3;

//=======================
//if( !inputArrayData.next()) break;
//inputArrayData.setAllClean();
inputArrayData.next();
}

//Set out vector
MDataHandle outputHandle = data.outputValue( TestNode::output );
outputHandle.setMVector( result );

//clean
data.setClean(plug);
outputHandle.setClean();

}// end else

}
else
return MS::kUnknownParameter;

return MS::kSuccess;
}
//---------------------------------------------------------------------------



Replies: 0
avatar

didn’t see where you do addAttribute() on input1, input2, input3. Maybe that’s causing problems



Replies: 1
/userdata/avatar/d96u2224y.jpg

Thanks, But I did, and when adding compound-Attribute it will add its children and also affect attribute.
I confused the problem is from compute() and seems getting value will change address or cause getting whole array
I don’t know.

Author: Shylon

Replied: 21 July 2011 06:21 PM  
avatar

first of all: no, you didn’t addAttribute for the child plugs. You only called addAttribute() on compTrgt and output. If you look carefully at Maya examples, you need to call addAttribute() on input1, input2 and input3. One indicator you are doing something funny is when I unload the plugin with the code you provided, Maya crashed.

secondly: I think the odd result you get is related to createPoint().
If you use
input1 = nAttr.create("input1”,"in1", MFnNumericData::k3Double, 0, &status);
input2 = nAttr.create("input2”,"in2", MFnNumericData::k3Double, 0, &status);
input3 = nAttr.create("input3”,"in3", MFnNumericData::k3Double, 0, &status);
output = nAttr.create("output”,"out", MFnNumericData::k3Double, 0, &status);
you should be able to get the correct result. The attribute “translate” on a transform node is of type double3, so I do believe this is the more appropriate type to create. I’ve never used createPoint() so I can only guess between MPoint, MVector and double3 one of the conversions is wrong and thus causing the weird result.



Replies: 1
/userdata/avatar/d96u2224y.jpg

I really really appreciate and thank you, It works and I can’t do anything except THANKS and few links that may useful for you and helps your workflows. I will complete the plug-in and send to area, and inform you.
Just another question, what about “id”, how can I get a unique id? I mean global id.
Cheerful and have a good time.

My Scripts for Maya:
http://area.autodesk.com/downloads/scripts/snbatchconstraintcmd
http://area.autodesk.com/downloads/scripts/snrandomkey
http://area.autodesk.com/downloads/scripts/sndividejointcurvejoint
http://area.autodesk.com/downloads/scripts/snmapchecker
http://area.autodesk.com/downloads/scripts/snarrangemultitileuvmaps
http://area.autodesk.com/downloads/scripts/snbatchsetdrivenkey
softimage
http://area.autodesk.com/downloads/plugins/snislandextractor

Author: Shylon

Replied: 22 July 2011 05:38 PM