Skip to content

Maya Addon Doesn't properly compute distance along the camera's forward vector #4

Open
@AnsonSavage

Description

@AnsonSavage

This one updates dynamically but doesn't properly compute the focal length (will be wrong if the target object isn't near the center of the camera's focal plane):

float $distance = pow(pow((camera1.translateX - pCube1.translateX), 2.0) + 
                         pow((camera1.translateY - pCube1.translateY), 2.0) + 
                         pow((camera1.translateZ - pCube1.translateZ), 2.0), 0.5);

    // Store initial focal length and distance when the expression is created
    // Calculate updated focal length based on the initial ratio
    cameraShape1.focalLength = $distance * 2.245459997252437;

This one (according to ChatGPT) should do it more or less right, however it doesn't seem to update dynamically.

float $cameraPosition[] = `xform -q -ws -t |camera1`;
    float $objectPosition[] = `xform -q -ws -t pCube1`;
    float $cameraRotation[] = `xform -q -ws -ro |camera1`;

    float $forwardVector[] = {
        -sin(deg_to_rad($cameraRotation[1])) * cos(deg_to_rad($cameraRotation[0])),
        sin(deg_to_rad($cameraRotation[0])),
        -cos(deg_to_rad($cameraRotation[1])) * cos(deg_to_rad($cameraRotation[0]))
    };

    float $cameraToObjectVector[] = {
        $objectPosition[0] - $cameraPosition[0],
        $objectPosition[1] - $cameraPosition[1],
        $objectPosition[2] - $cameraPosition[2]
    };

    // Calculate the distance along the camera's forward vector
    float $dotProduct = ($cameraToObjectVector[0] * $forwardVector[0]) + ($cameraToObjectVector[1] * $forwardVector[1]) + ($cameraToObjectVector[2] * $forwardVector[2]);
    float $distance = $dotProduct;

    // Store initial focal length and distance when the expression is created
    // Calculate updated focal length based on the initial ratio
    cameraShape1.focalLength = $distance * 3.297392606418606;

Any help I could get with this would be awesome!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions