Example UI for accepting and decoding MIME data drops

This example provides a script plugin into which the user can drag and drop various items from VRED.

vrDragAndDropExample.py
 1from PySide6 import QtCore, QtGui, QtWidgets
 2
 3import uiTools
 4from vrKernelServices import vrUserMimeTypes
 5
 6vrDragAndDropExampleGUI_form, vrDragAndDropExampleGUI_base = uiTools.loadUiType('vrDragAndDropExampleGUI.ui')
 7
 8class vrDragAndDropExample(vrDragAndDropExampleGUI_form, vrDragAndDropExampleGUI_base):
 9    def __init__(self, parent=None):
10        super(vrDragAndDropExample, self).__init__(parent)
11        parent.layout().addWidget(self)
12        self.parent = parent
13        self.setupUi(self)
14        self.setAcceptDrops(True)
15
16    def dragEnterEvent(self, event):
17        event.accept()
18
19    def dropEvent(self, event):
20        mimeData = event.mimeData()
21
22        droppedTypes = mimeData.formats()
23        print(" \ndropped types: " + str(droppedTypes))
24
25        # If you just want to decode a specific MIME type, you can just call:
26        # - vrUserMimeTypes.decodeStrings (for string based types)
27        # - vrUserMimeTypes.decodeObjects (for vrd object based types)
28        # - vrUserMimeTypes.decode (for anything: strings, objects and custom types)
29        # For example, use the following snippet to decode dropped nodes from the camera tree.
30        # You can also omit the check if the mime data contains the specific MIME type. If the
31        # data does not contain that type, the function will simply return an empty list.
32        if vrUserMimeTypes.cameraTreeNode in droppedTypes:
33            cameraTreeNodes = vrUserMimeTypes.decodeObjects(mimeData, vrUserMimeTypes.cameraTreeNode)
34
35        # If you want to extract a specific data type and do not care about the MIME type,
36        # you can search for a suitable MIME type using the findType function. For instance,
37        # the following snippet finds the first suitable MIME type which contains vrd objects
38        # and decodes them.
39        objectType = vrUserMimeTypes.findType(droppedTypes, vrUserMimeTypes.DataType.VrdObject)
40        objects = vrUserMimeTypes.decodeObjects(mimeData, objectType)
41
42        # If you want all suitable MIME types instead of only one, use this snippet:
43        objectTypes = vrUserMimeTypes.findTypes(droppedTypes, vrUserMimeTypes.DataType.VrdObject)
44        for objectType in objectTypes:
45            objects = vrUserMimeTypes.decodeObjects(mimeData, objectType)
46
47        # If you want decode all supported types inside a drop, use this snippet:
48        supportedTypes = vrUserMimeTypes.findTypes(droppedTypes, vrUserMimeTypes.DataType.Any)
49        print("supported types in drop: " + str(supportedTypes))
50        for supportedType in supportedTypes:
51            decodedElements = vrUserMimeTypes.decode(mimeData, supportedType)
52            print(supportedType + ": " + str(decodedElements))
53
54instance = vrDragAndDropExample(VREDPluginWidget)