Session APIs - Managing Your Scene
Overview
Vuer provides a set of intuitive APIs for managing your 3D scene dynamically. Understanding when to use each API is crucial for building efficient, interactive applications.
This guide covers the six core session APIs:
session.set- Initialize the scenesession.upsert- Update or insert elementssession.update- Update existing elements onlysession.add- Add new elementssession.remove- Remove elementssession.till- Wait for client events
Quick Reference
| API | Purpose | Behavior if element exists | Behavior if element missing |
|---|---|---|---|
session.set | Initialize root scene | N/A (replaces entire scene) | N/A (creates new scene) |
session.upsert | Ensure element exists | Updates it | Creates it |
session.update | Modify existing only | Updates it | Does nothing (NOOP) |
session.add | Add new elements | Error/duplicate | Creates it |
session.remove | Delete elements | Removes it | Does nothing |
session.till | Wait for event | N/A | Waits until received |
1. session.set - Initialize the Scene
Purpose: Set up the root scene structure. This should be called once at the beginning of your session.
When to use:
- At the start of your session to initialize the scene
- When you need to completely replace the entire scene structure
Accepts: Only Scene or DefaultScene objects
2. session.upsert - Update or Insert (Most Common)
Purpose: Ensure an element with a specific key exists with the given properties. If it exists, update it; if not, create it.
When to use:
- When you want to guarantee an element exists (most common use case)
- For dynamic, real-time updates where you don't know if the element was created yet
- When building interactive applications with frequent updates
Behavior:
- If element with the key exists → updates it
- If element doesn't exist → inserts it as new
Advanced: Targeting specific parents
3. session.update - Update Existing Only
Purpose: Modify existing elements without creating new ones. Safe for conditional updates.
When to use:
- When you want to update elements but not create them if they don't exist
- For defensive programming where you want to avoid accidental creation
- When you're unsure if an element exists
Behavior:
- If element exists → updates it
- If element doesn't exist → does nothing (NOOP)
Real-world example: Safe property updates
4. session.add - Add New Elements
Purpose: Explicitly add new elements to the scene. Assumes the element doesn't already exist.
When to use:
- When you're certain the element doesn't exist yet
- For one-time additions where you control the lifecycle
- When you want explicit "add" semantics in your code
Behavior:
- If element doesn't exist → adds it
- If element exists → behavior depends on implementation (may create duplicate or error)
Adding multiple elements:
5. session.remove - Delete Elements
Purpose: Remove elements from the scene by their keys.
When to use:
- When you need to delete specific elements
- For cleanup operations
- To remove temporary visualizations
Behavior:
- Removes elements matching the provided key(s)
- Safe to call even if elements don't exist
Practical example: Toggle visualization
Common Patterns
Pattern 1: Initialize then Update
The most common pattern: use set once, then upsert for updates.
Pattern 2: Dynamic Element Management
Add and remove elements dynamically based on application state.
Pattern 3: Efficient Batch Updates
Update multiple elements efficiently.
Pattern 4: Safe Updates with Fallback
Use update for conditional changes, upsert when you need guarantees.
Performance Considerations
Efficient Updates
❌ Inefficient: Rebuilding entire scene
✅ Efficient: Updating only what changed
Batch Operations
When updating multiple elements, batch them into a single operation:
Summary
Decision Tree:
- Setting up initial scene? → Use
session.set - Need element to exist with specific properties? → Use
session.upsert⭐ (most common) - Only update if exists, otherwise skip? → Use
session.update - Adding definitely new elements? → Use
session.add - Removing elements? → Use
session.remove - Need to wait for a client event? → Use
session.till
Most Common Usage:
6. session.till - Wait for Client Events
Purpose: Wait for and receive a specific event type from the client. This is useful for awaiting events like INIT to identify the client type.
When to use:
- When you need to wait for a specific event before proceeding
- To identify client type (browser vs Python client) on connection
- For handshake or initialization sequences
Behavior:
- Registers a one-time handler for the specified event type
- Returns the event when received
- Optionally times out if event doesn't arrive
With timeout:
Client Info (INIT Event Value):
Common fields (both clients):
client—"browser"or"python"clientVersion— library versiontimezone— IANA format, e.g.,"America/Los_Angeles"timezoneOffset— minutes from UTC
Python clients send:
Browser clients send:
See Also
- Constructing a Scene - Learn about scene structure
- Event Handling - Responding to user interactions
- Client Connection - Connecting Python clients to Vuer