Usage Examples#

Create Multiple Image Views and Clean Up Resources#

import omni.ui as ui
from omni.kit.widget.imageview import ImageView

# Create a window with a layout to display multiple images
window = ui.Window("Multiple Images", width=800, height=400)

# List to keep track of all image views for later cleanup
image_views = []

with window.frame:
    # Create a horizontal layout for the images
    with ui.HStack():
        # Create multiple image views
        image1 = ImageView("path/to/first_image.png")
        image_views.append(image1)
        
        image2 = ImageView("path/to/second_image.png")
        image_views.append(image2)

# Function to properly release all image view resources
def cleanup_resources():
    for image_view in image_views:
        image_view.destroy()
    print(f"Released resources for {len(image_views)} image views")

Track Image Loading Progress with Callback#

import omni.ui as ui
import asyncio
from omni.kit.widget.imageview import ImageView
from functools import partial

# Create a window for displaying the image
window = ui.Window("Image Loading Progress", width=600, height=600)

# Create a future to track when the image is fully loaded
image_loaded_future = asyncio.Future()

# Callback function that reports the loading progress
def on_image_progress(future, progress):
    print(f"Image loading progress: {progress * 100:.1f}%")
    
    # When the image is fully loaded (progress = 1.0), resolve the future
    if progress >= 1.0 and not future.done():
        future.set_result(None)
        print("Image fully loaded!")

with window.frame:
    # Create the image view - replace with a valid image path
    image_view = ImageView("path/to/your/image.png")
    
    # Register the progress callback function
    image_view.set_progress_changed_fn(
        partial(on_image_progress, image_loaded_future)
    )

# Non-blocking way to handle when the image is loaded
def handle_future_completion(future):
    try:
        # This will either get the result or raise any exception from the future
        future.result()
        print("Image is now fully loaded - continue with your operations")
        
        # Clean up resources when appropriate
        # image_view.destroy()
    except Exception as e:
        print(f"Error during image loading: {e}")

# Add the callback to the future
image_loaded_future.add_done_callback(handle_future_completion)