Exemple #1
0
MStatus CXRayCameraExport::ExportCamera(const MFileObject& file)
{
	MDagPath            node; 
    MObject             component; 
    MSelectionList      list; 
    MFnDagNode          nodeFn; 
	MFnCamera			C;
	MStatus		st ;
    
	MGlobal::getActiveSelectionList( list ); 
    for ( u32 index = 0; index < list.length(); ++index ) 
    { 
        list.getDagPath			( index, node, component ); 
        nodeFn.setObject		( node ); 
		st = C.setObject		(node);
		if(st!=MStatus::kSuccess)
		{
			Msg		("Selected object is not a camera");
			return	MStatus::kInvalidParameter;
		}

    } 

	Msg("exporting camera named [%s]",	C.name().asChar());

	MTime				tmTemp,tmTemp2;
	MTime				tmQuant;

	// Remember the frame the scene was at so we can restore it later.
	MTime storedFrame	= MAnimControl::currentTime();
	MTime startFrame	= MAnimControl::minTime();
	MTime endFrame		= MAnimControl::maxTime();

	tmTemp.setUnit		(MTime::uiUnit());
	tmTemp2.setUnit		(MTime::uiUnit());
	tmQuant.setUnit		(MTime::uiUnit());
	tmQuant				= 10.0; //3 time in sec. temporary
	
	COMotion			M;
	M.SetParam			(0, (int)(endFrame-startFrame).as(MTime::uiUnit()), 30);
	
	Fvector				P,R;
	tmTemp				= startFrame;

	MObject cam_parent	= C.parent(0);
	MFnTransform		parentTransform(cam_parent);
	

	MDistance			dist;
	while(tmTemp <= endFrame)
	{
		MAnimControl::setCurrentTime( tmTemp );

		MMatrix parentMatrix	=	parentTransform.transformation().asMatrix();
		MMatrix					cv;
		cv.setToIdentity		();
		cv[2][2]				= -1.0;
		MMatrix					TM;
		TM						=  (cv*parentMatrix)*cv;
		TM						= cv*TM;
		parentMatrix			= TM;

		Msg				("frame[%d]",(int)tmTemp.as(MTime::uiUnit()));
		
		dist.setValue	(parentMatrix[3][0]);
		P.x				= (float)dist.asMeters();
		dist.setValue	(parentMatrix[3][1]);
		P.y				= (float)dist.asMeters();
		dist.setValue	(parentMatrix[3][2]);
		P.z				= (float)dist.asMeters();

		Msg				("P %3.3f,%3.3f,%3.3f",P.x,P.y,P.z);

		double			rot[3];
		MTransformationMatrix::RotationOrder rot_order = MTransformationMatrix::kXYZ;

		st 				= parentTransform.getRotation( rot, rot_order );
                             
		R.x				= -(float)rot[0];
		R.y				= -(float)rot[1];
		R.z				= -(float)rot[2];
//.		Msg				("rt %3.3f,%3.3f,%3.3f kWorld",R.x,R.y,R.z);

		tmTemp2			= tmTemp-startFrame;
		M.CreateKey		(float(tmTemp2.as(MTime::uiUnit()))/30.0f,P,R);

		if(tmTemp==endFrame)
			break;

		tmTemp			+= tmQuant;

		if(tmTemp>endFrame)
			tmTemp=endFrame;
	};


	MString 			fn_save_to = file.fullName();
	fn_save_to			+= ".anm";

	Msg("file full name [%s]", fn_save_to);
	M.SaveMotion		(fn_save_to.asChar());

	MAnimControl::setCurrentTime( storedFrame );

	return MS::kSuccess;
}
void DMPDSExporter::fillBones( DMPSkeletonData::SubSkeletonStruct* subSkel, string parent, DMPParameters* param, MDagPath& jointDag )
{
	MStatus status;
	if (jointDag.apiType() != MFn::kJoint)
	{
		return; // early out.
	}
	DMPSkeletonData::BoneStruct newBone;
	newBone.boneHandle = (unsigned int)subSkel->bones.size();
	newBone.name = jointDag.partialPathName().asUTF8();
	newBone.parentName = parent;

	MFnIkJoint fnJoint(jointDag, &status);
	//	matrix = [S] * [RO] * [R] * [JO] * [IS] * [T]
	/*
		These matrices are defined as follows:
		•[S] : scale
		•[RO] : rotateOrient (attribute name is rotateAxis)
		•[R] : rotate
		•[JO] : jointOrient
		•[IS] : parentScaleInverse
		•[T] : translate

		The methods to get the value of these matrices are:
		•[S] : getScale
		•[RO] : getScaleOrientation
		•[R] : getRotation
		•[JO] : getOrientation
		•[IS] : (the inverse of the getScale on the parent transformation matrix)
		•[T] : translation

	*/
	MVector trans = fnJoint.getTranslation(MSpace::kTransform);
	double scale[3];
	fnJoint.getScale(scale);
	MQuaternion R, RO, JO;
	fnJoint.getScaleOrientation(RO);
	fnJoint.getRotation(R);
	fnJoint.getOrientation(JO);
	MQuaternion rot = RO * R * JO; 
	
	newBone.translate[0] = trans.x * param->lum;
	newBone.translate[1] = trans.y * param->lum;
	newBone.translate[2] = trans.z * param->lum;

	newBone.orientation[0] = rot.w;
	newBone.orientation[1] = rot.x;
	newBone.orientation[2] = rot.y;
	newBone.orientation[3] = rot.z;

	newBone.scale[0] = scale[0];
	newBone.scale[1] = scale[1];
	newBone.scale[2] = scale[2];

	subSkel->bones.push_back(newBone);
	// Load child joints
	for (unsigned int i=0; i<jointDag.childCount();i++)
	{
		MObject child;
		child = jointDag.child(i);
		MDagPath childDag = jointDag;
		childDag.push(child);
		fillBones(subSkel, newBone.name, param, childDag);
	}
	// now go for animations
	if (param->bExportSkelAnimation)
	{
		for (unsigned int i = 0; i < subSkel->animations.size(); ++i)
		{
			DMPSkeletonData::TransformAnimation& anim = subSkel->animations[i];
			DMPSkeletonData::TransformTrack subTrack;
			subTrack.targetBone = newBone.name;

			MPlug		plugT;	// translate
 			MPlug		plugR;	// R
 			MPlug		plugRO;	// RO
 			MPlug		plugJO;	// JO
			MPlug		plugS;	// scale
			double		dataT[3];
			double		dataR[3];
			double		dataRO[3];
			double		dataJO[3];
			double		dataS[3];
			MFnDependencyNode	fnDependNode( jointDag.node(), &status );
			
			plugT = fnDependNode.findPlug("translate", false, &status);
 			plugR = fnDependNode.findPlug("rotate", false, &status);
 			plugRO = fnDependNode.findPlug("rotateAxis", false, &status);
 			plugJO = fnDependNode.findPlug("jointOrient", false, &status);
			plugS = fnDependNode.findPlug("scale", false, &status);

			float timeStep = param->samplerRate;
			if (param->animSampleType == DMPParameters::AST_Frame)
			{
				timeStep /= param->fps;
			}
			for (float curTime = anim.startTime; curTime <= anim.endTime; curTime += timeStep)
			{
				MTime		mayaTime;
				DMPSkeletonData::TransformKeyFrame keyframe;
				keyframe.time = curTime - anim.startTime;
				mayaTime.setUnit(MTime::kSeconds);
				mayaTime.setValue(curTime);

				// Get its value at the specified Time.
				plugT.child(0).getValue(dataT[0], MDGContext(mayaTime));
				plugT.child(1).getValue(dataT[1], MDGContext(mayaTime));
				plugT.child(2).getValue(dataT[2], MDGContext(mayaTime));

				plugR.child(0).getValue(dataR[0], MDGContext(mayaTime));
				plugR.child(1).getValue(dataR[1], MDGContext(mayaTime));
				plugR.child(2).getValue(dataR[2], MDGContext(mayaTime));

				plugRO.child(0).getValue(dataRO[0], MDGContext(mayaTime));
				plugRO.child(1).getValue(dataRO[1], MDGContext(mayaTime));
				plugRO.child(2).getValue(dataRO[2], MDGContext(mayaTime));

				plugJO.child(0).getValue(dataJO[0], MDGContext(mayaTime));
				plugJO.child(1).getValue(dataJO[1], MDGContext(mayaTime));
				plugJO.child(2).getValue(dataJO[2], MDGContext(mayaTime));

				plugS.child(0).getValue(dataS[0], MDGContext(mayaTime));
				plugS.child(1).getValue(dataS[1], MDGContext(mayaTime));
				plugS.child(2).getValue(dataS[2], MDGContext(mayaTime));

				// fill the frame.
				keyframe.translate[0] = dataT[0] * param->lum;
				keyframe.translate[1] = dataT[1] * param->lum;
				keyframe.translate[2] = dataT[2] * param->lum;
				// calculate quaternion.
				MEulerRotation	rotR(dataR[0], dataR[1], dataR[2]);
				MEulerRotation	rotRO(dataRO[0], dataRO[1], dataRO[2]);
				MEulerRotation	rotJO(dataJO[0], dataJO[1], dataJO[2]);

				MQuaternion finalRot = rotRO.asQuaternion()*rotR.asQuaternion()*rotJO.asQuaternion();
				
				keyframe.orientation[0] = finalRot.w;
				keyframe.orientation[1] = finalRot.x;
				keyframe.orientation[2] = finalRot.y;
				keyframe.orientation[3] = finalRot.z;

				keyframe.scale[0] = dataS[0];
				keyframe.scale[1] = dataS[1];
				keyframe.scale[2] = dataS[2];

				subTrack.frames.push_back(keyframe);
			}
			anim.tracks.push_back(subTrack);
		}
	}
}