Usage Examples#

Create a Basic OpenGLWidget#

import omni.ui as ui
import omni.kit.widget.opengl as og

# Create a window
window = ui.Window("OpenGL Widget Example", width=300, height=300)

# Add OpenGLWidget to the window
with window.frame:
    # Create an OpenGL widget for custom rendering
    opengl_widget = og.OpenGLWidget()

Implement a Custom OpenGL Scene#

import omni.ui as ui
import omni.kit.widget.opengl as og

# Define a custom OpenGL scene
class CustomScene(og.OpenGLScene):
    def initialize_gl(self, viewport_width, viewport_height, first_init):
        # Initialize OpenGL resources
        if first_init:
            print(f"First initialization with viewport: {viewport_width}x{viewport_height}")
            # Set up OpenGL resources (shaders, textures, etc.) here
        else:
            print(f"Resized to viewport: {viewport_width}x{viewport_height}")
    
    def draw_gl(self, viewport_width, viewport_height):
        # Draw OpenGL content
        print(f"Drawing with viewport: {viewport_width}x{viewport_height}")
        # Implement OpenGL drawing commands here
        # This is where you would render your scene

# Create a window
window = ui.Window("Custom OpenGL Scene Example", width=300, height=300)

# Set up the OpenGL widget with custom scene
with window.frame:
    opengl_widget = og.OpenGLWidget()
    custom_scene = CustomScene(opengl_widget)

Use OpenGLImageProvider#

# Warning: This code snippet is not working. Please fix it.
import omni.ui as ui
import omni.kit.widget.opengl as og

# Create a window
window = ui.Window("OpenGL Image Provider Example", width=300, height=300)

# Set up OpenGLImageProvider
with window.frame:
    # Create an image provider using OpenGL
    image_provider = og.OpenGLImageProvider()
    
    # Use the image provider with an Image widget
    image = ui.Image(image_provider)

Create Interactive OpenGL Scene with UI Controls#

# Warning: This code snippet is not working. Please fix it.
import omni.ui as ui
import omni.kit.widget.opengl as og

# Define an interactive OpenGL scene
class InteractiveScene(og.OpenGLScene):
    def __init__(self, widget_or_provider):
        super().__init__(widget_or_provider)
        # Store state that affects rendering
        self.rotation = 0.0
    
    def initialize_gl(self, viewport_width, viewport_height, first_init):
        if first_init:
            print("Initializing OpenGL resources")
            # Initialize OpenGL resources here
    
    def draw_gl(self, viewport_width, viewport_height):
        print(f"Drawing with rotation: {self.rotation}")
        # Use self.rotation in your OpenGL rendering
        # OpenGL drawing code here

# Create a window
window = ui.Window("Interactive OpenGL Scene", width=400, height=400)

with window.frame:
    with ui.VStack():
        # Create UI controls
        with ui.HStack(height=30):
            ui.Label("Rotation:")
            slider = ui.FloatSlider(min=0, max=360)
        
        # Create OpenGL widget and scene
        opengl_widget = og.OpenGLWidget()
        scene = InteractiveScene(opengl_widget)
        
        # Update scene when slider changes
        def on_slider_changed(value):
            scene.rotation = value
        
        slider.set_value_changed_fn(on_slider_changed)