Skip to main content

An improved Model3D component with environment map support

Project description


tags: [gradio-custom-component, Model3D, model 3d, 3d, model, illumination, light, environment map, env map] title: gradio_litmodel3d short_description: An improved Model3D component with environment map support colorFrom: blue colorTo: yellow sdk: gradio pinned: false app_file: space.py

gradio_litmodel3d

Static Badge

An improved Model3D component with environment map support

Installation

pip install gradio_litmodel3d

Usage

import gradio as gr
from gradio_litmodel3d import LitModel3D

# print gradio version
print(gr.__version__)

def update_hdr(hdr_upload):
    return gr.update(env_map=hdr_upload.name if hdr_upload else None)

with gr.Blocks() as demo:
    env_map = gr.File(label="HDR Environment Map", file_types=[".hdr"], file_count="single")
    modelupload = gr.File(label="3D Model", file_types=[".obj", ".gltf", ".glb"])
    model3d = LitModel3D(interactive=False)

    tonemapping = gr.Radio(
        value="standard",
        label="Tonemapping",
        choices=["standard", "aces"],
    )
    exposure = gr.Slider(
        value=1.0,
        label="Exposure",
        minimum=0.1,
        maximum=5.0, 
        step=0.1,
    )
    contrast = gr.Slider(
        value=1.0,
        label="Contrast",
        minimum=0.1,
        maximum=2.0, 
        step=0.1,
    )
    tonemapping.change(
        lambda tonemapping: gr.update(tonemapping=tonemapping),
        inputs=[tonemapping],
        outputs=[model3d],
    )
    exposure.change(
        lambda exposure: gr.update(exposure=exposure),
        inputs=[exposure],
        outputs=[model3d],
    )
    contrast.change(
        lambda contrast: gr.update(contrast=contrast),
        inputs=[contrast],
        outputs=[model3d],
    )

    modelupload.change(
        lambda model_upload: gr.update(value=model_upload),
        inputs=[modelupload],
        outputs=[model3d],
    )
    env_map.change(
        update_hdr,
        inputs=[env_map],
        outputs=[model3d],
    )


if __name__ == "__main__":
    demo.launch()

LitModel3D

Initialization

name type default description
value
str | Callable | None
None path to (.obj, .glb, .stl, .gltf, .splat, or .ply) file to show in model3D viewer. If callable, the function will be called whenever the app loads to set the initial value of the component.
env_map
str | None
None path to environment map file to show in model3D viewer. If callable, the function will be called whenever the app loads to set the initial value of the environment map.
tonemapping
Literal["standard", "aces"] | None
None tonemapping algorithm to use for rendering the scene. Should be one of "standard" or "aces". If not provided, defaults to "standard".
exposure
float
1.0 exposure value to use for rendering the scene. Should be a float, increase this value to make the scene brighter, decrease to make it darker. Affects the exposure property of the camera.
contrast
float
1.0 contrast value to use for rendering the scene. Should be a float, increase this value to make the scene more contrasted, decrease to make it less contrasted. Affects the contrast property of the camera.
clear_color
tuple[float, float, float, float] | None
None background color of scene, should be a tuple of 4 floats between 0 and 1 representing RGBA values.
camera_position
tuple[
    int | float | None,
    int | float | None,
    int | float | None,
]
None, None, None initial camera position of scene, provided as a tuple of `(alpha, beta, radius)`. Each value is optional. If provided, `alpha` and `beta` should be in degrees reflecting the angular position along the longitudinal and latitudinal axes, respectively. Radius corresponds to the distance from the center of the object to the camera.
zoom_speed
float
1 the speed of zooming in and out of the scene when the cursor wheel is rotated or when screen is pinched on a mobile device. Should be a positive float, increase this value to make zooming faster, decrease to make it slower. Affects the wheelPrecision property of the camera.
pan_speed
float
1 the speed of panning the scene when the cursor is dragged or when the screen is dragged on a mobile device. Should be a positive float, increase this value to make panning faster, decrease to make it slower. Affects the panSensibility property of the camera.
height
int | str | None
None The height of the model3D component, specified in pixels if a number is passed, or in CSS units if a string is passed.
label
str | None
None The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
show_label
bool | None
None if True, will display label.
every
float | None
None If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
container
bool
True If True, will place the component in a container - providing some extra padding around the border.
scale
int | None
None relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
min_width
int
160 minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
interactive
bool | None
None if True, will allow users to upload a file; if False, can only be used to display files. If not provided, this is inferred based on whether the component is used as an input or output.
visible
bool
True If False, component will be hidden.
elem_id
str | None
None An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes
list[str] | str | None
None An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render
bool
True If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
key
int | str | None
None if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.

Events

name description
change Triggered when the value of the LitModel3D changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See .input() for a listener that is only triggered by user input.
upload This listener is triggered when the user uploads a file into the LitModel3D.
edit This listener is triggered when the user edits the LitModel3D (e.g. image) using the built-in editor.
clear This listener is triggered when the user clears the LitModel3D using the X button for the component.

User function

The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).

  • When used as an Input, the component only impacts the input signature of the user function.
  • When used as an output, the component only impacts the return signature of the user function.

The code snippet below is accurate in cases where the component is used as both an input and an output.

  • As output: Is passed, passes the uploaded file as a {str} filepath to the function.
  • As input: Should return, expects function to return a {str} or {pathlib.Path} filepath of type (.obj, .glb, .stl, or .gltf).
def predict(
    value: str | None
) -> str | Path | None:
    return value

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradio_litmodel3d-0.0.1.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gradio_litmodel3d-0.0.1-py3-none-any.whl (989.8 kB view details)

Uploaded Python 3

File details

Details for the file gradio_litmodel3d-0.0.1.tar.gz.

File metadata

  • Download URL: gradio_litmodel3d-0.0.1.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.6

File hashes

Hashes for gradio_litmodel3d-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3052b4484631a1240c26709de06899fa4bd3db00f2c4b1573c491df5f0351bc4
MD5 e73a9c31880a6a849e0996e0355f12bb
BLAKE2b-256 7750103047eab2fcd5d86d62142083035c2d585afdc3bbd7edf36ae1151c7b65

See more details on using hashes here.

File details

Details for the file gradio_litmodel3d-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gradio_litmodel3d-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f59302f3406af963d6613d364a9d85740ff709843d94e90cd3736871e6982e5
MD5 a4013d49d08694807658668373252cb0
BLAKE2b-256 b531282ae85e449a6cd55953590f5f344b3374def2cb8aee1cfcffb1f660efda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page