Skip to content

Instantly share code, notes, and snippets.

@mafshin
Created November 11, 2023 10:59
Show Gist options
  • Save mafshin/d0174d08629b65bed09ff820156943b2 to your computer and use it in GitHub Desktop.
Save mafshin/d0174d08629b65bed09ff820156943b2 to your computer and use it in GitHub Desktop.
3D Scene Move Camera and Objects in NiceGUI
from nicegui import app, ui
refresh_rate = 60
def play():
timer.active = not timer.active
def update_scene():
movement = 1
delta = movement / refresh_rate
teapot.move(x = teapot.x + delta)
light_yellow.move(x = light_yellow.x, y = light_yellow.y + delta, z = light_yellow.z)
light_red.move(x = light_red.x, y = light_red.y - delta, z =light_red.z)
sphere.move(x = sphere.x, y = sphere.y, z = sphere.z + delta)
box.move(x = box.x - delta, y = box.y, z = box.z)
def move_camera():
scene.move_camera(x = 0, y = 0, duration=10)
def move_sphere():
sphere.move(x = sphere.x, y = sphere.y + 1, z = sphere.z)
with ui.row():
ui.button('Play/Pause', on_click=play)
ui.button('Move Camera', on_click=move_camera)
ui.button('Move Sphere', on_click=move_sphere)
timer = ui.timer(1/refresh_rate, update_scene, active=False)
app.add_static_files('/assets', 'assets')
with ui.scene().classes('w-full h-64') as scene:
light_yellow = scene.spot_light(color='#ffff00', distance=50, intensity=1).move(-10, 0, 10)
light_red = scene.spot_light(color='red', distance=50, intensity=1).move(-10, 20, 10)
sphere = scene.sphere(radius=3).material('red').move(20, 0, 3)
box = scene.box(width=3, height=2, depth=4).material('green').move(-20, 15, 3)
teapot = scene.stl("/assets/teapot.stl").material('gray').scale(0.5)
scene.camera.x = 0
scene.camera.y = -20
scene.camera.z = 8
scene.camera.look_at_x = 0
scene.camera.look_at_y = 0
scene.camera.look_at_z = 0
ui.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment