Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure camera settings only update when necessary in Model3D #6871

Merged
merged 6 commits into from Jan 4, 2024

Conversation

hannahblair
Copy link
Collaborator

@hannahblair hannahblair commented Dec 22, 2023

Description

We were calling reset_camera_position whenever the component re-rendered. This isn't ideal if the value, nor camera settings of a scene have changed. However, we need to maintain this reactivity if any of those camera properties do change.

This PR checks if the old camera settings are different to the new camera settings before resetting the scene.

A test demo: (where we dynamically change the camera position, and also check that we maintain the camera settings when re-rendering)

import gradio as gr
import os
import trimesh
from PIL import Image
import numpy as np


def update_theta(theta, cam_position):
    cam = (theta, cam_position[1], None)
    model = gr.Model3D(label="Input 3D mesh", value=os.path.join(os.path.abspath(''), 
                            "./demo/model3D/files/Duck.glb") , camera_position=cam)
    return cam, model

def update_phi(phi, cam_position):
    cam = (cam_position[0], phi, None)
    model = gr.Model3D(label="Input 3D mesh", value=os.path.join(os.path.abspath(''), 
                            "./demo/model3D/files/Duck.glb") , camera_position=cam)
    return cam, model


with gr.Blocks() as demo:
    with gr.Row():
        model = gr.Model3D(label="Input 3D mesh", value=os.path.join(os.path.abspath(''), 
                                "./demo/model3D/files/Duck.glb") , camera_position=(0, 45, None))
        with gr.Column():     
            theta_slider = gr.Slider(0, 360, step=1, label="camera theta")
            phi_slider = gr.Slider(45, 90, step=1, label="camera phi")

        cam_position = gr.State(value = (0, 45, None))
        theta_slider.release(update_theta, inputs=[theta_slider, cam_position], outputs=[cam_position, model])
        phi_slider.release(update_phi, inputs=[phi_slider, cam_position], outputs=[cam_position, model])

    def update(name):
        return f"Welcome to Gradio, {name}!"

    file = os.path.join(os.path.abspath(''), "./demo/model3D/files/Duck.glb")

    with gr.Row():
        with gr.Column(scale=5):
            model_3d = gr.Model3D(
                value=file,
                clear_color=[0.0, 0.0, 0.0, 0.0],
                label="3D Model",
                )
    with gr.Row():
        inp = gr.Textbox(placeholder="What is your name?")
        out = gr.Textbox()
    btn = gr.Button("Run")
    btn.click(fn=update, inputs=[inp], outputs=[out], queue=False)

demo.launch()

Closes: #6859

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Dec 22, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/27b95ca8f4ac5d643907e910a5f5682379187208/gradio-4.12.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@27b95ca8f4ac5d643907e910a5f5682379187208#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Dec 22, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/model3d patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Ensure camera settings only update when necessary in Model3D

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@hannahblair hannahblair changed the title change reset_camera_position reactivity logic Ensure camera settings only update when necessary Dec 22, 2023
@hannahblair hannahblair changed the title Ensure camera settings only update when necessary Ensure camera settings only update when necessary in Model3D Dec 22, 2023
@hannahblair hannahblair marked this pull request as ready for review January 3, 2024 12:34
Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great. Nice demo too!

@abidlabs
Copy link
Member

abidlabs commented Jan 4, 2024

I'll go ahead and merge this in so we can get it out for the release. Thanks @hannahblair

@abidlabs abidlabs merged commit d361a0f into main Jan 4, 2024
23 checks passed
@abidlabs abidlabs deleted the tweak-model3d-reactivity branch January 4, 2024 18:39
@pngwn pngwn mentioned this pull request Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model3D Automatic Reloading
3 participants