PDA

View Full Version : A problem when i dev a dynamic plugin


yaoyansi
12-01-2009, 03:30 PM
A problem when i dev a dynamic plugin

hi, all
I design a node MeltNode to simulate a sphere falling onto the ground. But the animation in maya is not right. Here is the detail.

class MeltNode : public MPxNode
{

static MObject ia_systemTime;
static MObject ia_startTime;

static MObject distanceX;
static MObject distanceY;
static MObject distanceZ;

Solver *solver;
protected:
MTime m_prevTime;
....
};


MStatus MeltNode::initialize()
{
ia_systemTime = fnUnitAttr.create( "inTime", "it", MFnUnitAttribute::kTime, 0.0, &status );
fnUnitAttr.setHidden(true);

ia_startTime = fnUnitAttr.create( "startTime", "stm", MFnUnitAttribute::kTime, 1.0, &status );

distanceX = nAttr.create( "distanceX", "distX", MFnNumericData::kFloat, 0.0 );
distanceY = nAttr.create( "distanceY", "distY", MFnNumericData::kFloat, 0.0 );
distanceZ = nAttr.create( "distanceZ", "distZ", MFnNumericData::kFloat, 0.0 );

status = addAttribute(ia_systemTime);
status = addAttribute(ia_startTime);
status = addAttribute( distanceX );
status = addAttribute( distanceY );
status = addAttribute( distanceZ );

attributeAffects( ia_systemTime, distanceX );
attributeAffects( ia_systemTime, distanceY );
attributeAffects( ia_systemTime, distanceZ );
}
MStatus MeltNode::compute( const MPlug& plug, MDataBlock& data )
{
cout << "------------------------------compute" << endl;
MStatus stat = MStatus::kSuccess;

MTime itime = data.inputValue(ia_systemTime, &stat).asTime();
MCHECKSTATUS(stat, "data.inputValue(ia_systemTime, &stat)");

MTime istartTime = data.inputValue(ia_startTime, &stat).asTime();
MCHECKSTATUS(stat, "data.inputValue(ia_startTime, &stat)");

cout << "itime="<< itime.as(MTime::kSeconds)
<< endl;

float newPos[3]={-100.0, -100.0, -100.0};
float newRot[4]={-100.0, -100.0, -100.0, 1.0};
//-----------------------------------------------------------
float px, py, pz; px=py=pz=0.0;
float rx, ry, rz, rw; rx=ry=rz=rw=0.0;
if(itime == istartTime)//start time
{
init(plug, data);//init physics engine ,add ground and a sphere
newPos[0] = 0.0;
newPos[1] = 5.0;
newPos[2] = 0.0;
}else{
double delta_frames = (itime - m_prevTime).value();
if(itime > m_prevTime )
{
cout << "sim" << endl;
double dt = (itime - m_prevTime).as(MTime::kSeconds);
solver->simulate(itime.as(MTime::kSeconds), dt);//let physics engine simulate dt seconds
solver->getTransform(1, px, py, pz, rx, ry, rz, rw);//get the simulate resulte(translate and rotation)
//we only take care translate here, and i output the translate data, it is right.

//printf("world pos2 = %f,%f,%f\n", px,py,pz );
newPos[0] = px;//translate - x
newPos[1] = py;//translate - y
newPos[2] = pz;//translate - z
printf("world pos2 = %f,%f,%f\n", newPos[0],newPos[1],newPos[2] );
//----------------------------------------------------------------
if( plug == distanceX ) //translate - x
{
MDataHandle oDistanceX = data.outputValue( distanceX, &stat );
MCHECKSTATUS(stat, "data.outputValue( distanceX, &stat );");
oDistanceX.set( newPos[0] );
data.outputValue(distanceX).set(true);
data.setClean( plug );
}
else if( plug == distanceY ){//translate - y
MDataHandle oDistanceY = data.outputValue( distanceY, &stat );
MCHECKSTATUS(stat, "data.outputValue( distanceY, &stat );");
oDistanceY.set( newPos[1] );
data.outputValue(distanceY).set(true);
data.setClean( plug );
}
else if( plug == distanceZ ){//translate - z
MDataHandle oDistanceZ = data.outputValue( distanceZ, &stat );
MCHECKSTATUS(stat, "data.outputValue( distanceZ, &stat );");
oDistanceZ.set( newPos[2] );
data.outputValue(distanceZ).set(true);
data.setClean( plug );
}

else{
stat = MS::kUnknownParameter;
}
}//if(itime > m_prevTime )

}
m_prevTime = itime;


//--
//--

return stat;
}


Then i use MeltCmd to connect MeltNode translate attributes to the sphere:
MStatus MeltCmd::redoIt()
{
stat = dgMod.connect( meltFn.findPlug( "distanceX"), transformFn.findPlug( "translateX" ));
MCHECKSTATUS(stat, "dgMod.connect(\"distanceX\", \"translateX\")");

stat = dgMod.connect( meltFn.findPlug( "distanceY"), transformFn.findPlug( "translateY" ));
MCHECKSTATUS(stat, "dgMod.connect(\"distanceY\", \"translateY\")");

stat = dgMod.connect( meltFn.findPlug( "distanceZ"), transformFn.findPlug( "translateZ" ));
MCHECKSTATUS(stat, "dgMod.connect(\"distanceZ\", \"translateZ\")");

.....
}


and i output the translate data from physics engine, it is right, the translate-y decreases, the sphere should be falling.
But the animation in maya is wrong, sphere is moving along x axis, and translate-y is Zero! Very strange, but Why?


Can you help me?
Thank you.

CGTalk Moderation
12-01-2009, 03:30 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.