Home Technical Talk

[maya] [mel] Convert rotation values into a vector

greentooth
Offline / Send Message
Froyok greentooth
Hi guys,

I'm currently wondering how I could do this little trick. Converting the rotations of the perspective camera in a vector (giving me the direction of the camera).

I'm so dumb in maths that I can't figure it out. :poly136:
I already know how to get the camera values and how to play with vectors in Mel, but doing this conversion is the step where I block.

Replies

  • Warheart
    Options
    Offline / Send Message
    Warheart polycounter lvl 17
    I take it you want the forward (unit) vector of the rotation matrix?

    If so, in mel you can get this as follows:
    [php]
    // Get the worldspace transform of the "persp" camera (returns an array of 16 values)
    float $TM[] = `xform -q -ws -matrix "persp"`;

    // From that transform matrix the components of the Z rotation vector will be indexes 8, 9 and 10
    float $vecZ[] = {$TM[8], $TM[9], $TM[10]};

    // Since cameras in Maya face -Z you will want the inverse of that vector
    float $camForwardVec[] = {-$vecZ[0], -$vecZ[1], -$vecZ[2]};
    [/php]

    Obviously that's written rather verbosely for clarity. You could inverse the values on the second line at the same time you grab them out of the matrix.

    Hopefully that's of some use. :)
  • Froyok
    Options
    Offline / Send Message
    Froyok greentooth
    Haa, thanks ! It works fine ! :)

    I have solved myself the problem by an other way :
    float $camFoc[3] = `camera -q -worldCenterOfInterest persp`;
        float $camPos[3] = `camera -q -position persp`;
        
        vector $one = <<$camFoc[0], $camFoc[1], $camFoc[2]>>;
        vector $two = <<$camPos[0], $camPos[1], $camPos[2]>>;
        vector $final = ($two - $one);
    
        $directionVector = unit($final);
    

    Of course this solution only works if I use the persp camera. Your method will works with everything ! Thanks again ! :)
Sign In or Register to comment.