Saturday, March 14, 2009

Simulating Maya Animation Curve calculation in C

It's a super old program I made for one of my friend.


#include <stdio.h>

int main(){

double x1, x2, x3, x4;
double y1, y2, y3, y4;
double t1x, t1y, t2x, t2y;
double scale;
double t, x, y;
double ax, bx, cx, dx;
double ay, by, cy, dy;

printf("Input start point and end point(P1, P4)\n");
scanf("%lf %lf %lf %lf", &x1, &y1, &x4, &y4);
printf("P1 = (%lf, %lf), P4 = (%lf, %lf)\n",
x1, y1, x4, y4);

//Input P1's keyTangent -q -ox, keyTangent -q -oy and
//P4's keyTangent -q -ix, keyTangent -q -iy values.
printf("Input outTangent of P1 and inTangent of P4\n");
scanf("%lf %lf %lf %lf", &t1x, &t1y, &t2x, &t2y);
printf("P1outTan = (%lf, %lf), P4inTan = (%lf, %lf)\n",
t1x, t1y, t2x, t2y);

//move x2 to the deviding point 1:3 between the start and end.
x2 = x1 + (x4 - x1) / 3;

//Change y2 to adjust the tangent.
scale = (x2 - x1) / t1x;
y2 = y1 + t1y * scale;

//move x3 to the deviding point 2:3 between the start and end.
x3 = x4 - (x4 - x1) / 3;
//Change y3 to adjust the tangent.
scale = (x4 - x3) / t2x;
y3 = y4 - t2y * scale;

while(1){
printf("Input curve parameter: ");
scanf("%lf", &t);

ax = - 1 * x1 + 3 * x2 - 3 * x3 + 1 * x4;
bx = 3 * x1 - 6 * x2 + 3 * x3 + 0 * x4;
cx = - 3 * x1 + 3 * x2 + 0 * x3 + 0 * x4;
dx = 1 * x1 + 0 * x2 - 0 * x3 + 0 * x4;

ay = - 1 * y1 + 3 * y2 - 3 * y3 + 1 * y4;
by = 3 * y1 - 6 * y2 + 3 * y3 + 0 * y4;
cy = - 3 * y1 + 3 * y2 + 0 * y3 + 0 * y4;
dy = 1 * y1 + 0 * y2 - 0 * y3 + 0 * y4;

x = ax * t*t*t + bx * t*t + cx * t + dx;
y = ay * t*t*t + by * t*t + cy * t + dy;

printf("x = %lf, y = %lf\n", x, y);
}

return 0;
}


----------------Test MEL Script----------------


//Set the unit to cm and second in Window->Settings/Preferences->Preferences
//before runnning the MEL script.

sphere;
currentTime 0;
setAttr "nurbsSphere1.tx" 0;
setKeyframe "nurbsSphere1.tx";
currentTime 1;
setKeyframe "nurbsSphere1.tx";
keyTangent -e -a -t 0 -outAngle 89.035812 -outWeight 1 nurbsSphere1_translateX ;
keyTangent -e -a -t 1 -inAngle 85.969596 -inWeight 1 nurbsSphere1_translateX ;

print "---tangents---\n";
selectKey -r -k -t 0 nurbsSphere1_translateX ;
print `keyTangent -q -ox`;
print `keyTangent -q -oy`;
selectKey -r -k -t 1 nurbsSphere1_translateX ;
print `keyTangent -q -ox`;
print `keyTangent -q -oy`;
print "---data---\n";
float $i;
for ($i = 0; $i <1.01; $i += 0.2){
currentTime $i;
print ( `currentTime -q`+ ": " + `getAttr "nurbsSphere1.tx"` + "\n");
}


----------------Result----------------
[Maya]

---tangents---
0.016827
0.999858
0.070286
0.997527
---data---
0: 0
0.2: 7.151379024
0.4: 7.193755345
0.6: 3.660442155
0.8: 0.08475264279
1: 0

[program]

Input start point and end point(P1, P4)
0 0 1 0
P1 = (0.000000, 0.000000), P4 = (1.000000, 0.000000)
Input outTangent of P1 and inTangent of P4
0.016827 0.999858 0.070286 0.997527
P1outTan = (0.016827, 0.999858), P4inTan = (0.070286, 0.997527)
Input curve parameter: 0.0
x = 0.000000, y = 0.000000
Input curve parameter: 0.2
x = 0.200000, y = 7.151585
Input curve parameter: 0.4
x = 0.400000, y = 7.193990
Input curve parameter: 0.6
x = 0.600000, y = 3.660601
Input curve parameter: 0.8
x = 0.800000, y = 0.084808
Input curve parameter: 1.0
x = 1.000000, y = -0.000000
Input curve parameter:

No comments: