PDA

View Full Version : Exporting animation matrix from maya.... Need help ^^


Dryden
03-04-2009, 03:47 PM
Hi,

I'm working since few weeks on a custom exporter to a real time engine.

I can export mesh, textures without issue. But about animation I still have trouble.

The following code show the matrix creation (performed for each bones), so I guess I'm gonna have issue with parenting animation.

If u have any idea about how to deal with... (translation matrix is the only one who works actually)


string $return = "";

//Gathering data from current bones
// Gathering data from world space for translation, to try to go trought the parent inherit translation
float $t[] = `xform -q -ws -t $bonesName`;
float $s[] = `getAttr ($bonesName+".s")`;
float $r[] = `getAttr ($bonesName+".r")`;

//Initialize matrices
matrix $translate [4][4];
matrix $scale [4][4];
matrix $rotateX [4][4];
matrix $rotateY [4][4];
matrix $rotateZ [4][4];
matrix $multi [4][4];

//Fill Matrices
$scale = <<$s[0],0,0,0; 0,$s[1],0,0; 0,0,$s[2],0; 0,0,0,1>>;
$translate = <<1,0,0,0; 0,1,0,0; 0,0,1,0; $t[0],$t[1],$t[2],1>>;
$rotateX = <<1,0,0,0; 0,`cos $r[0]`,`sin $r[0]`,0; 0,-(`sin $r[0]`),`cos $r[0]`,0; 0,0,0,1>>;
$rotateY = <<`cos $r[1]`,0,-(`sin $r[1]`),0; 0,1,0,0; `sin $r[1]`,0,`cos $r[1]`,0; 0,0,0,1>>;
$rotateZ = <<`cos $r[2]`,`sin $r[2]`,0,0; -(`sin $r[2]`),`cos $r[2]`,0,0; 0,0,1,0; 0,0,0,1>>;

//Combine them.
$multi = $scale * $rotateX * $rotateY * $rotateZ * $translate; // ????

for($i=0;$i<4;$i++)
{
for($j=0;$j<4;$j++)
{
if(($i == 3) && ($j == 3))
{
$return += "M"+($i+1)+"."+($j+1)+":"+$multi[$i][$j];
}else{
$return += "M"+($i+1)+"."+($j+1)+":"+$multi[$i][$j]+",";
}
}
}

Robert Bateman
03-04-2009, 03:52 PM
The API is sooooo much easier for this stuff.

Anyhow, it's not going to work. Have a look at the DG node reference for how to properly combine the matrices (you've ignored the rotation axis, joint orients, pivots, shearing, etc).

Luckily though, you don't have to do any of that at all.

Just use the xform command to return the matrices to you. (which will be as arrays of 16 floats). No one actually uses the matrix or vector types in mel, because they are pretty useless.

float $matrix[] = `xform -q -os -m $boneName`;

Dryden
03-04-2009, 04:27 PM
Is that really easier in API ?
I'm quite used to mel, and have knowledge in C#, if u have any link for a start in API =)

Still got issue... I suppose this extracted matrix is wright, so it should came from my importer. So...Checking ^^.

(But still looking for good links about API, or books)

Dryden
03-04-2009, 04:48 PM
Do you have any idea about how maya create this matrix ? (order of matrix mixing ?)
Will be a lot easier to know how it's made for the importer side.

Keilun
03-04-2009, 06:15 PM
Matrix construction is all documented in the API. Have a look at the docs for MFnTransform:

http://download.autodesk.com/us/maya/2009help/API/class_m_fn_transform.html

And yes it is much easier in the API. The matrix structure is easier to work with than in MEL.

Dryden
03-05-2009, 07:28 AM
So now, the exported matrice is wright :)

But stille issue with importer. Here's a snippet of the importer code (C#):


public void Update(GameTime gameTime)
{
this._globalTime += (uint)gameTime.ElapsedGameTime.Milliseconds;
this.AnimationManager.Update(gameTime);

for (int i = 0; i < this.Bones.Length; i++)
{
if (Bones[i].Parent != null)
continue;
//Matrix matrix = Matrix.CreateTranslation(this.Bones[i].Pivot);
//this.Bones[i].Matrix = matrix * this.Bones[i].Matrix;
for (int j = 0; j < this.Bones[i].Children.Length; j++)
{
ComputeBoneMatrix(this.Bones[i].Children[j]);
}
}

for (int i = 0; i < this.Bones.Length; i++)
{
this.Bones[i].TransformedPivot = Vector3.Transform(this.Bones[i].Pivot, this.Bones[i].Matrix);
this.BonesMatrix[i] =this.Bones[i].Matrix;
}
}

private void ComputeBoneMatrix(Bone bone)
{
// Matrix matrix = Matrix.CreateTranslation(bone.Pivot);
// bone.Matrix = matrix*bone.Matrix;
bone.Matrix = bone.Parent.Matrix*bone.Matrix;

for (int i = 0; i < bone.Children.Length; i++)
{
ComputeBoneMatrix(bone.Children[i]);
}
}


The display in engine do not match with the display in maya, but can't find out why, cause I juste handle the matrice the way it's send by maya, just multiply the bones by the matrix, do you have any idea about what can I do ?

Robert Bateman
03-05-2009, 01:24 PM
Matrix construction is all documented in the API. Have a look at the docs for MFnTransform:

Which is only half of the story. (And the info is also in the node and attr reference as I said previously). The other bit you need to look at is the docs for MFnIkJoint or the joint node attr reference - since joints are evaluated differently to transforms.

The display in engine do not match with the display in maya, but can't find out why, cause I juste handle the matrice the way it's send by maya, just multiply the bones by the matrix, do you have any idea about what can I do ?

Assuming you've unit tested the exporter, the next thing is unit testing you matrix code to make sure it actually works. The way you handle pivots looks very wrong imho, but again there's not really any help I can give you unless you know that your code works correctly. Start with 1 bone, test just rotations first, then scaling + translation. Having done that, add more tests for a simple 2 bone chain. If that all works, your problem will no longer be there.

Dryden
04-02-2009, 02:23 PM
After a lot of time spend on testing, I figure out than I can't find a correct way to export all that.

As Robert Bateman say, I'm quite sure than it came from joint, but I do not have enought knowledge to fixe that.

Does someone have any exemple on how joint/animation should be handled ? (export of static meshes work very well)

I'm not sure off the exporter side, so if I can have something working, it will be a lot easier for me to adapt the engine importer. (my c# knowledge is a lot better than my mel's one)

Anyone can help ?

Robert Bateman
04-02-2009, 02:38 PM
Does someone have any exemple on how joint/animation should be handled ? (export of static meshes work very well)

Again, every single piece of information you require, is in the documentation for MFnTransform and MFnIkJoint. It is repeatable, since I have used the exact method they have documented there and it works. To me it sounds like:

1. Your matrix routines need testing
2. You have not followed the equations listed in the documentation, or
3. Your matrix multi is the other way around to maya's (eg a*b v.s. b*a).

Dryden
04-02-2009, 03:24 PM
The main issue is than I've never use API for maya, is there any way to use those classes in mel ?

Or do you have any link's to start learning API ? (time isn't a problem, so I can start learning it)

Robert Bateman
04-02-2009, 04:59 PM
It's a hell of a lot easier in the API, but you can do it all in mel by querying attribute values. You can also look at the node and attribute reference (as I've already said a few times), and that will tell you which attributes to query.

API resources
|
|
|
V

CGTalk Moderation
04-02-2009, 04:59 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.