Hi,
I am trying to build an extension where I have multiple images and each of them are draggable and point to usd files. I want to be able to drag the image into the viewport and then based on the usd I want to execute some action in the scene, this could be attaching a material to a prim or adding a usd onto the scene with the right scale.
I have been unable to do this for quite some time and would love your help. I have created a test demo, so you can also test it on your end and help me fix it.
I tried:
-
The ViewportWindow() does have a function called external_drag_drop_support() but it does not seem to work as intended. The code gets executed but it never enters the viewport drop function.
-
Looking at how the omni kit browser material does the same task, by looking at its source code. They use something called create_drop_helper which was not at all clear since the docs for this are hard to find and complicated.
Can someone please help me make more progress on this. Here is the test code for the extension that I created:
import omni.ext
import omni.ui as ui
from omni.kit.viewport.utility import get_active_viewport_window
import omni.kit.commands
from pxr import Sdf, Usd
from pathlib import Path
from omni.ui import color as cl
import os
from pxr import UsdShade
from os.path import dirname
import sys
class TestViewportDrop(omni.ext.IExt):
def drag_data(self):
print(f"This function should pass some data to the viewport_drop function")
print(f"Lets return a test string")
url = "USD_DIR/wallpaper1.usd"
return url
# return 42
# return "Hello World"
def viewport_drop(self, event_type, drag_data):
"""
This function should be called when the user drops a file into the viewport.
It should have the mouse event type and the dragged data (url of the file) as arguments.
"""
print(f"Viewport drop event: {event_type}")
print(f"Drag data: {drag_data}")
print(f"Viewport drop event: Succesful")
return None
def on_startup(self, ext_id):
print("[kaari.extensions.tutorial] TestViewportDrop startup")
self._viewport_window = get_active_viewport_window()
self._viewport_window.add_external_drag_drop_support(self.viewport_drop)
# Create a window with a draggable label
self._window = ui.Window("TestViewportDrop", width=300, height=200)
self._frame = ui.Frame(width=20, height=50, style={"border": "1px solid blue"})
with self._window.frame:
# with self._frame:
# self._label = ui.Button("TestViewportDrop Text", style={"color": "blue"})
# self._frame.set_drag_fn(lambda: self.drag_data("/Environment/Looks/Grid"))
with ui.VStack():
ui.Spacer(height=10)
self._frame = ui.Frame(width=120, height=50, style={"border": "5px solid blue"})
with self._frame:
ui.Label("TestViewportDrop", style={"color": "blue", \
"background_color": cl("#DDDD00"), \
"debug_color": cl("#FF000022")})
self._frame.set_drag_fn(lambda: self.drag_data())
def on_shutdown(self):
print("[kaari.extensions.tutorial] TestViewportDrop shutdown")
if self._window:
self._window.destroy()
self._window = None
testextension.zip (1.0 KB)