1. Пользоваться форумом на планшетах и телефонах стало удобнее благодаря Tapatalk

matrica preobrazowaniya

Тема в разделе "Maya", создана пользователем -, 21 апр 2002.

Модераторы: Dark™, Skif
  1. Guest

    Proshu pros4eniya za 6rift 4to to ne tak s nim.
    A vopros vot kakoi bil:
    Matrica preobrazowaniya ,kak s nei rabotat
    i kak primeniat etot script.Kak on rabotaet Ya ponimau
    on rabotaet s matricami vizualno(peremnojaet i t.d.)
    no kuda mojno ego primenit?
    // matrixPlayground.mel v1.2 (12 Jan 2001)
    //
    // MEL script for Maya
    //
    // by Bryan Ewert
    // http://www.ewertb.com
    // bryan@ewertb.com
    //
    // H2O Entertainment Corporation
    // Vancouver, Canada
    // http://www.h2oent.com

    // History:
    // v1.2 (12 Jan 2001)
    // - added Precision option.
    // - got rid of traceMe() calls (internal to H2O)
    //
    // v1.1 (4 Dec 2000)
    // - bug fix: Matrix Add requesting matrix contents with
    // form name instead of letter ID
    //
    // v1.0 (3 Oct 2000)

    global proc matrix doubleToMatrix44( float $darray[] )
    // Input: array of 16 floats
    // Output: 4Ч4 matrix
    //
    // Fills the matrix by column, then row;
    // e.g. matrix[0][0], matrix[0][1], matrix[0][2], ..., matrix[1][0], ...
    {
    matrix $matrix[4][4];

    for ( $i = 0; $i < 4; $i++ )
    {
    for ( $j = 0; $j < 4; $j++ )
    {
    $matrix[$i][$j] = $darray[ ($i*4) + $j ];
    }
    }

    return $matrix;
    }

    proc string getMatrixForm( string $which )
    // Returns the formLayout containing the specified Matrix ("A", "B" or "C")
    {
    string $matrixForm = "";
    string $matrixFrame = ( "frameMatrix" + $which );

    if ( `frameLayout -q -exists $matrixFrame` )
    {
    string $parent = `setParent -q`;
    setParent $matrixFrame;

    string $childArray[] = `frameLayout -q -childArray $matrixFrame`;

    if ( `formLayout -q -exists $childArray[0]` )
    {
    $matrixForm = $childArray[0];
    }

    if ( "NONE" != $parent )
    setParent $parent;
    }

    return $matrixForm;
    }

    proc matrix getMatrix( string $which )
    // Returns the 4Ч4 matrix for the specified Matrix ("A", "B" or "C")
    {
    matrix $matrix44[4][4] = << 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0 >>;
    int $size = 3;

    string $parent = `setParent -q`;

    string $form = getMatrixForm( $which );

    setParent $form; // So relative names of childArray set proper set of floatFields

    string $matrixCells[] = `formLayout -q -childArray $form`;

    if ( size( $matrixCells ) == 16 )
    $size = 4;

    for ( $i = 0; $i < $size; $i++ )
    {
    for ( $j = 0; $j < $size; $j++ )
    {
    $matrix44[$i][$j] = `floatField -q -value $matrixCells[$i*$size+$j]`;
    }
    }

    setParent $parent;

    return $matrix44;
    }

    proc populateMatrix( string $which, matrix $cMatrix, string $annotation )
    // Fills the specified Matrix ("A", "B" or "C") with the supplied $cMatrix,
    // and changes the frameLayout label to $annotation
    {
    string $childArray[];
    int $size = 3;

    string $formLayout = getMatrixForm( $which );

    if ( "" != $formLayout )
    {
    string $parent = `setParent -q`;
    setParent $formLayout;
    $childArray = `formLayout -q -childArray $formLayout`;

    if ( size( $childArray ) == 16 )
    $size = 4;

    for ( $i = 0; $i < $size; $i++ )
    {
    for ( $j = 0; $j < $size; $j++ )
    {
    floatField -e -value $cMatrix[$i][$j] $childArray[$i*$size+$j];
    }
    }

    frameLayout -e -label ( "Matrix " + $which + " - " + $annotation ) ( "frameMatrix" + $which );

    setParent $parent;
    }

    if ( $which == "C" )
    {
    if ( `checkBoxGrp -q -value1 matrixAutoEcho` )
    populateMatrix( "A", $cMatrix, $annotation );
    if ( `checkBoxGrp -q -value2 matrixAutoEcho` )
    populateMatrix( "B", $cMatrix, $annotation );
    }
    }

    proc float determinant( matrix $mMatrix, int $size )
    // Derives the Determinant of the provided $mMatrix.
    // The $size must be specified (2, 3 or 4).
    {
    float $determinant = 0.0;

    if ( $size == 2 )
    {
    $determinant = ( ( $mMatrix[0][0] * $mMatrix[1][1] ) - ( $mMatrix[0][1] * $mMatrix[1][0] ) );
    }
    if ( $size == 3 )
    {
    $determinant = ( $mMatrix[0][0] * $mMatrix[1][1] * $mMatrix[2][2] +
    $mMatrix[1][0] * $mMatrix[2][1] * $mMatrix[0][2] +
    $mMatrix[0][1] * $mMatrix[1][2] * $mMatrix[2][0] ) -
    ( $mMatrix[2][0] * $mMatrix[1][1] * $mMatrix[0][2] +
    $mMatrix[1][0] * $mMatrix[0][1] * $mMatrix[2][2] +
    $mMatrix[2][1] * $mMatrix[1][2] * $mMatrix[0][0] );

    }
    if ( $size == 4 )
    {
    // Determinant of 4Ч4 matrix:
    // < A B C D > < 00 01 02 03 >
    // < E F G H > < 10 11 12 13 >
    // < I J K L > < 20 21 22 23 >
    // < M N O P > < 30 31 32 33 >
    //
    // Use first row < A B C D > and use Laplace Expansion against corresponding 3Ч3 matrices
    // det = ( A | < F G H > | ) - ( B | < E G H > | ) + ( C | < E F H > | ) - ( D | < E F G > | )
    // < J K L > < I K L > < I J L > < I J K >
    // < N O P > < M O P > < M N P > < M N O >
    //
    // det = ( A | < 11 12 13 > | ) - ( B | < 10 12 13 > | ) + ( C | < 10 11 13 > | ) - ( D | < 10 11 12 > | )
    // < 21 22 23 > < 20 22 23 > < 20 21 23 > < 20 21 22 >
    // < 31 32 33 > < 30 32 33 > < 30 31 33 > < 30 31 32 >

    float $detA, $detB, $detC, $detD;

    $detA = ( $mMatrix[1][1] * $mMatrix[2][2] * $mMatrix[3][3] +
    $mMatrix[1][2] * $mMatrix[2][3] * $mMatrix[3][1] +
    $mMatrix[1][3] * $mMatrix[2][1] * $mMatrix[3][2] ) -
    ( $mMatrix[1][1] * $mMatrix[2][3] * $mMatrix[3][2] +
    $mMatrix[1][2] * $mMatrix[2][1] * $mMatrix[3][3] +
    $mMatrix[1][3] * $mMatrix[2][2] * $mMatrix[3][1] );

    $detB = ( $mMatrix[1][0] * $mMatrix[2][2] * $mMatrix[3][3] +
    $mMatrix[1][2] * $mMatrix[2][3] * $mMatrix[3][0] +
    $mMatrix[1][3] * $mMatrix[2][0] * $mMatrix[3][2] ) -
    ( $mMatrix[1][0] * $mMatrix[2][3] * $mMatrix[3][2] +
    $mMatrix[1][2] * $mMatrix[2][0] * $mMatrix[3][3] +
    $mMatrix[1][3] * $mMatrix[2][2] * $mMatrix[3][0] );

    $detC = ( $mMatrix[1][0] * $mMatrix[2][1] * $mMatrix[3][3] +
    $mMatrix[1][1] * $mMatrix[2][3] * $mMatrix[3][0] +
    $mMatrix[1][3] * $mMatrix[2][0] * $mMatrix[3][1] ) -
    ( $mMatrix[1][0] * $mMatrix[2][3] * $mMatrix[3][1] +
    $mMatrix[1][1] * $mMatrix[2][0] * $mMatrix[3][3] +
    $mMatrix[1][3] * $mMatrix[2][1] * $mMatrix[3][0] );

    $detD = ( $mMatrix[1][0] * $mMatrix[2][1] * $mMatrix[3][2] +
    $mMatrix[1][1] * $mMatrix[2][2] * $mMatrix[3][0] +
    $mMatrix[1][2] * $mMatrix[2][0] * $mMatrix[3][1] ) -
    ( $mMatrix[1][0] * $mMatrix[2][2] * $mMatrix[3][1] +
    $mMatrix[1][1] * $mMatrix[2][0] * $mMatrix[3][2] +
    $mMatrix[1][2] * $mMatrix[2][1] * $mMatrix[3][0] );

    $determinant = ( $mMatrix[0][0] * $detA ) - ( $mMatrix[0][1] * $detB ) + ( $mMatrix[0][2] * $detC ) - ( $mMatrix[0][3] * $detD );
    }

    return $determinant;
    }

    proc matrix matrixCollapseRowColumn( matrix $aMatrix, int $size, int $row, int $column )
    // Removes the specified $row and $column from $aMatrix;
    // e.g. collapses a 4Ч4 matrix to a 3Ч3 matrix.
    // Note: $row & $column are 1-index
    {
    matrix $cMatrix[4][4] = << 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0 >>;

    for ( $i = 0, $m = 0; $i < $size; $i++, $m++ )
    {
    // Skip over collapsed row
    if ( $m+1 == $row )
    $m++;

    for ( $j = 0, $n = 0; $j < $size; $j++, $n++ )
    {
    // Skip over collapsed column
    if ( $n+1 == $column )
    $n++;

    if ( $m < 4 && $n < 4 )
    $cMatrix[$i][$j] = $aMatrix[$m][$n];
    else
    $cMatrix[$i][$j] = 0.0;
    }
    }

    return $cMatrix;
    }

    proc matrix transpose( matrix $aMatrix, int $size )
    // Returns a matrix which is the Transpose of $aMatrix.
    // The $size must be specified (typically 3 or 4 for this script).
    {
    matrix $tMatrix[4][4] = << 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0 >>;

    for ( $i = 0; $i < $size; $i++ )
    {
    for ( $j = 0; $j < $size; $j++ )
    {
    $tMatrix[$i][$j] = $aMatrix[$j][$i];
    }
    }

    return $tMatrix;
    }

    global proc matrixTranspose( string $which )
    // The UI callback to perform a Transpose on the specified Matrix ("A", "B" or "C")
    {
    matrix $aMatrix[4][4];
    matrix $tMatrix[4][4];

    int $size = ( `radioButtonGrp -q -select matrix_dimensions` + 2 );

    $aMatrix = getMatrix( $which );

    $tMatrix = transpose( $aMatrix, $size );

    populateMatrix( "C", $tMatrix, "Transpose of Matrix A" );
    }

    proc matrix adjoint( matrix $aMatrix, int $size )
    // Returns a matrix which is the Adjoint of $aMatrix.
    // The $size of the input matrix must be specified (3 or 4).
    {
    matrix $cMatrix[4][4] = << 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0 >>;

    int $detSize = ( $size - 1 );

    if ( $size > 2 )
    {
    // Cofactor of top-left 3Ч3 matrix
    $cMatrix[0][0] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 1, 1 ), $detSize );
    $cMatrix[0][1] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 1, 2 ), $detSize );
    $cMatrix[0][2] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 1, 3 ), $detSize );

    $cMatrix[1][0] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 2, 1 ), $detSize );
    $cMatrix[1][1] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 2, 2 ), $detSize );
    $cMatrix[1][2] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 2, 3 ), $detSize );

    $cMatrix[2][0] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 3, 1 ), $detSize );
    $cMatrix[2][1] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 3, 2 ), $detSize );
    $cMatrix[2][2] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 3, 3 ), $detSize );
    }

    if ( $size > 3 )
    {
    // Cofactor of 4th column
    $cMatrix[0][3] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 1, 4 ), $detSize );

    $cMatrix[1][3] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 2, 4 ), $detSize );

    $cMatrix[2][3] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 3, 4 ), $detSize );

    // Cofactor of 4th row
    $cMatrix[3][0] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 4, 1 ), $detSize );
    $cMatrix[3][1] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 4, 2 ), $detSize );
    $cMatrix[3][2] = -determinant( matrixCollapseRowColumn( $aMatrix, $size, 4, 3 ), $detSize );
    $cMatrix[3][3] = determinant( matrixCollapseRowColumn( $aMatrix, $size, 4, 4 ), $detSize );
    }

    // Adjoint is TRANSPOSE of matrix containing cofactors
    $cMatrix = transpose( $cMatrix, $size );

    return $cMatrix;
    }

    global proc matrixAdjoint( string $which )
    // The UI callback to perform a Adjoint on the specified Matrix ("A", "B" or "C")
    {
    matrix $aMatrix[4][4];
    matrix $cMatrix[4][4];
    float $determinant;

    int $size = ( `radioButtonGrp -q -select matrix_dimensions` + 2 );

    $aMatrix = getMatrix( $which );

    $cMatrix = adjoint( $aMatrix, $size );

    populateMatrix( "C", $cMatrix, "Adjoint of Matrix A" );

    }

    proc matrix inverse( matrix $aMatrix, int $size )
    // Returns a matrix which is the Inverse of $aMatrix.
    // The $size of the input matrix must be specified (3 or 4).
    {
    matrix $iMatrix[4][4] = << 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0 >>;

    float $determinant = determinant( $aMatrix, $size );

    if ( $determinant != 0.0 )
    $iMatrix = ( 1 / determinant( $aMatrix, $size ) ) * adjoint( $aMatrix, $size );

    return $iMatrix;
    }

    global proc matrixInverse( string $which )
    // The UI callback to perform a Inverse on the specified Matrix ("A", "B" or "C")
    {
    matrix $aMatrix[4][4];
    matrix $iMatrix[4][4];
    float $determinant;
    string $message = "Inverse of Matrix A";

    int $size = ( `radioButtonGrp -q -select matrix_dimensions` + 2 );

    $aMatrix = getMatrix( $which );

    $iMatrix = inverse( $aMatrix, $size );

    if ( 0 == determinant( $aMatrix, $size ) )
    $message = "Matrix A is singular and has no inverse";

    populateMatrix( "C", $iMatrix, $message );

    }

    global proc matrixTrace( string $which )
    // Returns the Trace of the specified Matrix ("A", "B" or "C")
    {
    float $trace = 0.0;

    string $form = getMatrixForm( $which );

    if ( "" != $form )
    {
    string $parent = `setParent -q`;
    setParent $form;

    string $matrixCells[] = `formLayout -q -childArray $form`;

    $trace = `floatField -q -value $matrixCells[0]`;

    if ( size( $matrixCells ) == 16 )
    {
    $trace += `floatField -q -value $matrixCells[5]`;
    $trace += `floatField -q -value $matrixCells[10]`;
    $trace += `floatField -q -value $matrixCells[15]`;
    }

    if ( size( $matrixCells ) == 9 )
    {
    $trace += `floatField -q -value $matrixCells[4]`;
    $trace += `floatField -q -value $matrixCells[8]`;
    }

    setParent $parent;
    }

    floatField -e -value $trace -enable true matrixTrace;
    }

    global proc matrixDeterminant( string $which )
    // The UI callback to perform a Determinant on the specified Matrix ("A", "B" or "C")
    {
    float $determinant = 0.0;

    int $size = ( `radioButtonGrp -q -select matrix_dimensions` + 2 );

    matrix $matrix[4][4] = getMatrix( $which );

    $determinant = determinant( $matrix, $size );

    floatField -e -value $determinant -enable true matrixDet;
    }

    global proc matrix multiplyMatrix( matrix $aMatrix, matrix $bMatrix )
    // Returns a matrix which is the product of multiplying $aMatrix and $bMatrix.
    // Note: As matrices in this script are always square (3Ч3 or 4Ч4) no error-checking
    // is supplied to assert that the sizes of the two matrices are applicable for
    // multiplication.
    {
    matrix $cMatrix[4][4] = << 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0 >>;

    for ( $i = 0; $i < 4; $i++ )
    {
    for ( $j = 0; $j < 4; $j++ )
    {
    for ( $k = 0; $k < 4; $k++ )
    {
    $cMatrix[$i][$j] = $cMatrix[$i][$j] + ( $aMatrix[$i][$k] * $bMatrix[$k][$j] );
    }
    }
    }
    return $cMatrix;
    }

    global proc matrixMultiply()
    // The UI callback to perform a Product of Matrix A and Matrix B
    {
    matrix $aMatrix[4][4];
    matrix $bMatrix[4][4];
    float $determinant;
    string $message = "Inverse of Matrix A";

    int $size = ( `radioButtonGrp -q -select matrix_dimensions` + 2 );

    string $matrixFormA = getMatrixForm( "A" );
    string $matrixFormB = getMatrixForm( "B" );

    if ( "" != $matrixFormA && "" != $matrixFormB )
    {
    $aMatrix = getMatrix( "A" );
    $bMatrix = getMatrix( "B" );

    $aMatrix = multiplyMatrix( $aMatrix, $bMatrix );

    populateMatrix( "C", $aMatrix, "Product ( A * B )" );
    }
    }

    global proc matrix addMatrix( matrix $aMatrix, matrix $bMatrix )
    // Returns a matrix which is the Sum or $aMatrix and $bMatrix.
    {
    for ( $i = 0; $i < 4; $i++ )
    {
    for ( $j = 0; $j < 4; $j++ )
    {
    $aMatrix[$i][$j] = $aMatrix[$i][$j] + $bMatrix[$i][$j];
    }
    }
    return $aMatrix;
    }

    global proc matrixAdd()
    // The UI callback to perform a Sum of Matrix A and Matrix B
    {
    matrix $aMatrix[4][4];
    matrix $bMatrix[4][4];

    int $size = ( `radioButtonGrp -q -select matrix_dimensions` + 2 );

    string $matrixFormA = getMatrixForm( "A" );
    string $matrixFormB = getMatrixForm( "B" );

    if ( "" != $matrixFormA && "" != $matrixFormB )
    {
    // v1.1 -- bug fix - getMatrix() called with form name instead of letter ID
    $aMatrix = getMatrix( "A" );
    $bMatrix = getMatrix( "B" );

    $aMatrix = addMatrix( $aMatrix, $bMatrix );

    populateMatrix( "C", $aMatrix, "Sum ( A + B )" );
    }
    }

    global proc matrixAcquire( string $which, int $worldSpace )
    // Acquires the Transformation Matrix from the first item in Maya's
    // selection list and populates the specified Matrix ("A", "B" or "C")
    // with its elements.
    {
    string $select[] = `ls -sl`;

    if ( size( $select ) )
    {
    float $floatA[];

    if ( $worldSpace )
    $floatA = `xform -q -ws -matrix $select[0]`;
    else
    $floatA = `xform -q -matrix $select[0]`;

    matrix $mMatrix[4][4] = doubleToMatrix44( $floatA );
    populateMatrix( $which, $mMatrix, $select[0] );

    }
    }

    global proc matrixApply( string $which, int $worldSpace )
    // Applies the specified Matrix ("A", "B" or "C") to the first item
    // in Maya's selection list.
    {
    string $select[] = `ls -sl`;

    if ( size( $select ) )
    {
    matrix $mMatrix[4][4] = getMatrix( $which );
    float $f[16];
    for ( $i = 0; $i < 4; $i++ )
    {
    for ( $j = 0; $j < 4; $j++ )
    {
    $f[($i*4)+$j] = $mMatrix[$i][$j];
    }
    }

    string $command;

    if ( $worldSpace )
    $command = "xform -ws -matrix ";
    else
    $command = "xform -matrix ";

    for ( $i = 0; $i < 16; $i++ )
    $command = ( $command + $f[$i] + " " );
    $command += $select[0];

    eval $command;

    }
    }

    global proc matrixCopy( string $whichFrom, string $whichTo )
    // Copies the elements of the specified "From" Matrix ("A", "B" or "C")
    // to the specified "To" Matrix ("A", "B" or "C").
    // Note: As this function is logically called only from menu items
    // that know better, no error-checking is provided to assert that
    // the "From" matrix is not the same as the "To" matrix.
    // Attempting to do so would not cause an error or crash, but would
    // be less than productive.
    {
    matrix $fMatrix[4][4] = getMatrix( $whichFrom );

    populateMatrix( $whichTo, $fMatrix, ( "Copy of Matrix " + $whichFrom ) );
    }

    global proc matrixSwap( string $whichFrom, string $whichTo )
    // Swaps the elements of the specified "From" Matrix ("A", "B" or "C")
    // with the specified "To" Matrix ("A", "B" or "C").
    // Note: As this function is logically called only from menu items
    // that know better, no error-checking is provided to assert that
    // the "From" matrix is not the same as the "To" matrix.
    // Attempting to do so would not cause an error or crash, but would
    // be less than productive.
    {
    matrix $fMatrix[4][4] = getMatrix( $whichFrom );
    matrix $tMatrix[4][4] = getMatrix( $whichTo );

    populateMatrix( $whichTo, $fMatrix, "" );
    populateMatrix( $whichFrom, $tMatrix, "" );
    }

    global proc matrixRotate( string $which )
    // Applies a Rotation Transformation to the specified Matrix ("A", "B" or "C").
    // The rotation value and unit is derived from the current UI settings.
    // Note: The rotation matrices used may seem Transposed to those typically
    // documented, but they are correct for this implementation within Maya,
    // specifically in regard to the Acquire and Apply functions (above).
    {
    string $form = getMatrixForm( $which );

    float $fRotate = `floatField -q -value matrixRotate`;
    float $rotate = $fRotate;
    int $unit = `radioButtonGrp -q -select matrixRotateUnit`;
    int $axis = `radioButtonGrp -q -select matrixRotateAxis`;
    string $sAxis[3] = { "X", "Y", "Z" };
    float $kRADIAN = 57.295779513082320876798154814105;
    matrix $aMatrix[4][4] = << 1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1 >>;
    matrix $rMatrix[4][4] = << 1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1 >>;

    if ( $unit == 1 ) // must convert to degrees
    $rotate = $fRotate / $kRADIAN;

    float $cos = `cos $rotate`;
    float $sin = `sin $rotate`;

    switch ( $axis )
    {
    case 1: // X axis
    $rMatrix = << 1, 0, 0, 0; 0, $cos, $sin, 0; 0, -$sin, $cos, 0; 0, 0, 0, 1 >>;
    break;

    case 2: // Y axis
    $rMatrix = << $cos, 0, -$sin, 0; 0, 1, 0, 0; $sin, 0, $cos, 0; 0, 0, 0, 1 >>;
    break;

    case 3: // Z axis
    $rMatrix = << $cos, $sin, 0, 0; -$sin, $cos, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1 >>;
    break;
    }

    $aMatrix = getMatrix( $which );
    $rMatrix = multiplyMatrix( $aMatrix, $rMatrix );

    populateMatrix( "C", $rMatrix, ( "Rotate Matrix A " + $fRotate + ( $unit == 1 ? " deg" : " rad" ) + " on " + $sAxis[$axis-1] ) );

    }

    global proc matrixMakeIdentity( string $which )
    // Populates the specified Matrix ("A", "B" or "C") with an Identity Matrix.
    {
    float $cellValues44[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
    float $cellValues33[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };

    string $parent = `setParent -q`;

    string $form = getMatrixForm( $which );
    setParent $form; // So relative names of childArray set proper set of floatFields

    string $matrixCells[] = `formLayout -q -childArray $form`;

    if ( size( $matrixCells ) == 16 )
    {
    for ( $i = 0; $i < 16; $i++ )
    {
    floatField -e -value $cellValues44[$i] $matrixCells[$i];
    }
    }

    if ( size( $matrixCells ) == 9 )
    {
    for ( $i = 0; $i < 9; $i++ )
    {
    floatField -e -value $cellValues33[$i] $matrixCells[$i];
    }
    }

    setParent $parent;
    }

    proc string buildMatrix( string $which, int $size, int $editable )
    // Called to initialize the size and contents of the specified Matrix ("A", "B" or "C")
    // Matrix C is typically not editable.
    {
    int $tab = ( 100 / $size );

    string $frame = ( "frameMatrix" + $which );
    string $checkBoxMenu = ( "matrixUseWorldSpace" + $which );

    int $precision = 8;

    setParent $frame;

    string $form = `formLayout`;

    popupMenu -button 3 matrixPopupMenu;

    if ( $editable )
    {
    menuItem -label "Make Identity" -c ( "matrixMakeIdentity " + $which ) matrixMakeIdentityMenu;
    menuItem -label "Acquire from Selected" -c ( "matrixAcquire " + $which + " `menuItem -q -checkBox " + $checkBoxMenu + "`" ) matrixAcquireMenu;
    }

    menuItem -label "Apply to Selected" -c ( "matrixApply " + $which + " `menuItem -q -checkBox " + $checkBoxMenu + "`" ) matrixApplyMenu;

    menuItem -checkBox true -label "Use WorldSpace" $checkBoxMenu;

    if ( $which != "A" )
    {
    menuItem -label "Copy to Matrix A" -c ( "matrixCopy " + $which + " A" ) matrixCopyAMenu;
    }
    if ( $which != "B" )
    {
    menuItem -label "Copy to Matrix B" -c ( "matrixCopy " + $which + " B" ) matrixCopyBMenu;
    }
    if ( $which != "C" )
    {
    menuItem -label "Swap Matrix A & B" -c ( "matrixSwap A B" ) matrixSwapMenu;
    }

    floatField -editable $editable -precision $precision matrixA11;
    floatField -editable $editable -precision $precision matrixA12;
    floatField -editable $editable -precision $precision matrixA13;

    if ( $size == 4 )
    {
    floatField -editable $editable -precision $precision matrixA14;
    }

    floatField -editable $editable -precision $precision matrixA21;
    floatField -editable $editable -precision $precision matrixA22;
    floatField -editable $editable -precision $precision matrixA23;

    if ( $size == 4 )
    {
    floatField -editable $editable -precision $precision matrixA24;
    }

    floatField -editable $editable -precision $precision matrixA31;
    floatField -editable $editable -precision $precision matrixA32;
    floatField -editable $editable -precision $precision matrixA33;

    if ( $size == 4 )
    {
    floatField -editable $editable -precision $precision matrixA34;

    floatField -editable $editable -precision $precision matrixA41;
    floatField -editable $editable -precision $precision matrixA42;
    floatField -editable $editable -precision $precision matrixA43;
    floatField -editable $editable -precision $precision matrixA44;
    }

    setParent ..;

    formLayout -e
    -af matrixA11 top 4
    -af matrixA11 left 4
    -ap matrixA11 right 2 ( $tab * 1 )
    -af matrixA12 top 4
    -ap matrixA12 left 2 ( $tab * 1 )
    -ap matrixA12 right 2 ( $tab * 2 )
    -af matrixA13 top 4
    -ap matrixA13 left 2 ( $tab * 2 )
    -ap matrixA13 right 2 ( $tab * 3 )

    -ac matrixA21 top 4 matrixA11
    -af matrixA21 left 4
    -ap matrixA21 right 2 ( $tab * 1 )
    -ac matrixA22 top 4 matrixA12
    -ap matrixA22 left 2 ( $tab * 1 )
    -ap matrixA22 right 2 ( $tab * 2 )
    -ac matrixA23 top 4 matrixA13
    -ap matrixA23 left 2 ( $tab * 2 )
    -ap matrixA23 right 2 ( $tab * 3 )

    -ac matrixA31 top 4 matrixA21
    -af matrixA31 left 4
    -ap matrixA31 right 2 ( $tab * 1 )
    -ac matrixA32 top 4 matrixA22
    -ap matrixA32 left 2 ( $tab * 1 )
    -ap matrixA32 right 2 ( $tab * 2 )
    -ac matrixA33 top 4 matrixA23
    -ap matrixA33 left 2 ( $tab * 2 )
    -ap matrixA33 right 2 ( $tab * 3 )
    $form;

    if ( $size == 4 )
    {
    formLayout -e
    -af matrixA14 top 4
    -ap matrixA14 left 2 ( $tab * 3 )
    -ap matrixA14 right 2 ( $tab * 4 )

    -ac matrixA24 top 4 matrixA14
    -ap matrixA24 left 2 ( $tab * 3 )
    -ap matrixA24 right 2 ( $tab * 4 )

    -ac matrixA34 top 4 matrixA24
    -ap matrixA34 left 2 ( $tab * 3 )
    -ap matrixA34 right 2 ( $tab * 4 )

    -ac matrixA41 top 4 matrixA31
    -af matrixA41 left 4
    -ap matrixA41 right 2 ( $tab * 1 )

    -ac matrixA42 top 4 matrixA32
    -ap matrixA42 left 2 ( $tab * 1 )
    -ap matrixA42 right 2 ( $tab * 2 )

    -ac matrixA43 top 4 matrixA33
    -ap matrixA43 left 2 ( $tab * 2 )
    -ap matrixA43 right 2 ( $tab * 3 )

    -ac matrixA44 top 4 matrixA34
    -ap matrixA44 left 2 ( $tab * 3 )
    -ap matrixA44 right 2 ( $tab * 4 )
    $form;
    }

    matrixMakeIdentity( $which );

    setParent ..;

    return $form;
    }

    proc deleteFrameChildArray( string $frame )
    // Deletes all children from the specifed frameLayout.
    // This is used to clear the layout before rebuilding the Matrix
    // with buildMatrix() (above).
    {
    if ( `frameLayout -q -exists $frame` )
    {
    string $childArray[] = `frameLayout -q -childArray $frame`;

    for ( $child in $childArray )
    deleteUI $child;
    }
    }

    global proc string[] buildMatrixFrames( int $size )
    // A macro function to clear and rebuild all three Matrix layouts at once.
    {
    string $formMatrix[3];

    if ( `window -exists matrixPlayground` )
    {
    setParent matrixPlayground; // parent to window

    string $childArray[];

    deleteFrameChildArray( "frameMatrixA" );
    $formMatrix[0] = buildMatrix( "A", $size, true );

    deleteFrameChildArray( "frameMatrixB" );
    $formMatrix[1] = buildMatrix( "B", $size, true );

    deleteFrameChildArray( "frameMatrixC" );
    $formMatrix[2] = buildMatrix( "C", $size, false );
    }

    floatField -e -value 0.0 -enable false matrixTrace;
    floatField -e -value 0.0 -enable false matrixDet;

    frameLayout -e -label "Matrix C " frameMatrixC;

    return $formMatrix;
    }

    global proc matrixPlayground_performPrecision()
    {
    int $matrixPrecision = 4;

    if ( `optionVar -exists matrixPrecision` )
    $matrixPrecision = `optionVar -q matrixPrecision`;
    else
    optionVar -intValue matrixPrecision $matrixPrecision;

    string $mm[3] = { "A", "B", "C" };

    for ( $m in $mm )
    {
    string $form = getMatrixForm( $m );

    setParent $form;
    string $ca[] = `formLayout -q -ca $form`;

    for ( $c in $ca )
    {
    floatField -e -precision $matrixPrecision $c;
    }
    }
    }

    global proc matrixPlayground_precision()
    {
    int $precision = `optionVar -q matrixPrecision`;

    if ( `window -exists "matrixPrecision"` )
    deleteUI -window "matrixPrecision";

    window -title "Precision" -iconName "Precision" matrixPrecision;

    columnLayout -adjustableColumn true;

    intSliderGrp -label "" -min 1 -max 10 -fmn 1 -fmx 10 -field true
    -cw 1 1 -adj 3 -cat 3 "right" 2
    -value $precision
    -cc "optionVar -intValue matrixPrecision (#1); matrixPlayground_performPrecision()";

    showWindow matrixPrecision;
    }

    global proc matrixPlayground()
    // Main entry function for matrixPlayground.mel.
    // Creates the base UI layout with frameLayouts for each Matrix and associated tool controls.
    {
    if ( `window -exists "matrixPlayground"` )
    deleteUI -window "matrixPlayground";

    string $frameMatrixA, $frameMatrixB, $frameMatrixC, $frameOperA, $frameOperAB, $form;
    string $formMatrix[3];

    int $size = 4;
    int $precision = 4;

    if ( `optionVar -exists matrixSize` )
    $size = `optionVar -q matrixSize`;
    else
    optionVar -intValue matrixSize $size;

    window -title "Matrix Playground" -iconName "Matrix" -menuBar true matrixPlayground;

    menu -label "Options";
    menuItem -label "Precision" -c "matrixPlayground_precision()" matrixPlayground_menuPrecision;

    columnLayout -adjustableColumn true;

    radioButtonGrp
    -numberOfRadioButtons 2
    -columnWidth3 128 64 64
    -label "Matrix Dimensions"
    -labelArray2 "3Ч3" "4Ч4"
    -select ( $size - 2 )
    -on1 "optionVar -intValue matrixSize 3; buildMatrixFrames 3"
    -on2 "optionVar -intValue matrixSize 4; buildMatrixFrames 4"
    matrix_dimensions;

    $frameMatrixA = `frameLayout -label "Matrix A" frameMatrixA`;

    setParent ..;

    $frameOperA = `frameLayout -label "A operators" -borderVisible true -marginWidth 4 -marginHeight 4 -labelVisible false`;

    setParent ..;

    $frameMatrixB = `frameLayout -label "Matrix B" frameMatrixB`;

    setParent ..;

    $frameOperAB = `frameLayout -label "AB operators" -borderVisible true -marginWidth 4 -marginHeight 4 -labelVisible false`;

    setParent ..;

    $frameMatrixC = `frameLayout -label "Matrix C " frameMatrixC`;

    setParent ..;

    // /////////////////////////////////////////
    // Add options controls for Matrix C

    $frameOperC = `frameLayout -borderVisible true -marginWidth 4 -marginHeight 4 -labelVisible false`;

    checkBoxGrp
    -label "Echo Results to"
    -numberOfCheckBoxes 2
    -label1 "Matrix A"
    -label2 "Matrix B"
    -value1 off
    -value2 off
    -columnWidth3 128 64 64
    matrixAutoEcho;

    setParent ..;

    // /////////////////////////////////////////
    // Add tools that operate Matrix A

    setParent $frameOperA;

    columnLayout -adjustableColumn true;

    frameLayout -label "Matrix Functions";

    $form = `formLayout`;

    button -label "Trace" -c ( "matrixTrace A" ) matrixTraceButton;
    floatField matrixTrace;

    button -label "Determinant" -c ( "matrixDeterminant A" ) matrixDetButton;
    floatField matrixDet;

    rowColumnLayout -numberOfColumns 3 matrixAFunctions;
    button -label "Transpose" -c ( "matrixTranspose A" ) matrixTransposeButton;
    button -label "Adjoint" -c ( "matrixAdjoint A" ) matrixAdjointButton;
    button -label "Inverse" -c ( "matrixInverse A" ) matrixInverseButton;
    setParent ..;

    setParent ..;

    formLayout -e
    -af matrixTraceButton top 4
    -af matrixTraceButton left 4
    -ap matrixTraceButton right 2 33
    -af matrixTrace top 4
    -ap matrixTrace left 2 33

    -ac matrixDetButton top 4 matrixTraceButton
    -af matrixDetButton left 4
    -ap matrixDetButton right 2 33
    -ac matrixDet top 4 matrixTraceButton
    -ap matrixDet left 2 33

    -ac matrixAFunctions top 4 matrixDetButton
    -af matrixAFunctions left 4
    -af matrixAFunctions right 4

    // -ac matrixTransposeButton top 4 matrixDetButton
    // -af matrixTransposeButton left 4
    //
    // -ac matrixAdjointButton top 4 matrixDetButton
    // -ac matrixAdjointButton left 4 matrixTransposeButton
    //
    // -ac matrixInverseButton top 4 matrixDetButton
    // -ac matrixInverseButton left 4 matrixAdjointButton

    $form;

    setParent ..;

    frameLayout -label "Trigonometry";

    $form = `formLayout`;

    radioButtonGrp
    -numberOfRadioButtons 3
    -columnWidth4 128 64 64 64
    -label "Rotate Axis"
    -labelArray3 "X" "Y" "Z"
    -select 1
    matrixRotateAxis;

    button -label "Rotate" -c ( "matrixRotate A" ) matrixRotateButton;
    floatField matrixRotate;
    radioButtonGrp
    -numberOfRadioButtons 2
    -columnWidth3 1 48 48
    -label ""
    -labelArray2 "deg" "rad"
    -select 1
    matrixRotateUnit;

    setParent ..;

    formLayout -e
    -af matrixRotateAxis top 4
    -af matrixRotateAxis left 4

    -ac matrixRotateButton top 4 matrixRotateAxis
    -af matrixRotateButton left 4
    -ap matrixRotateButton right 2 33

    -ac matrixRotate top 4 matrixRotateAxis
    -ap matrixRotate left 2 33

    -ac matrixRotateUnit top 6 matrixRotateAxis
    -ac matrixRotateUnit left 4 matrixRotate

    $form;

    setParent ..;


    // /////////////////////////////////////////
    // Add tools that operate with Matrix A & Matrix B

    setParent $frameOperAB;

    rowColumnLayout -numberOfColumns 3 matrixBFunctions;
    button -label "Sum ( A + B )" -c ( "matrixAdd" ) matrixAddButton;
    button -label "Multiply ( A * B )" -c ( "matrixMultiply" ) matrixMultiplyButton;
    button -label "( A ? B )" -c ( "matrixWhatever" ) -enable false matrixWhateverButton;
    setParent ..;

    // /////////////////////////////////////////
    // Call macro to rebuild all Matrix layouts

    $formMatrix = buildMatrixFrames( $size );

    showWindow matrixPlayground;
    }
     
  2. Guest

    Матьмояженщина!
    Где ты ЭТО взял????
     
  3. Guest

    Мужик. Хочешь я _свой_ скрипт запостю? Он ВОСЕМЬДЕСЯТ КИЛ весит. Поглавнее будет. Покруче.

    Ты что, действительно думаешь, что в _этом_ кто-то будет разбираться?
    Тебе помогли с MEL'ом - это не повод засирать борду всяким отстоем - это повод нажать в майке кнопку F1 и начать грызть гранит науки.
     
Модераторы: Dark™, Skif

Поделиться этой страницей