IC Python API:Object Type

From Reallusion Wiki!
Revision as of 22:15, 16 September 2019 by Chuck (RL) (Talk | contribs) (Created page with "Reading the object type is important for operations that can only be applied to certain types of assets. This article will demonstrate the retrieval of the object type for th...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Reading the object type is important for operations that can only be applied to certain types of assets. This article will demonstrate the retrieval of the object type for the first item selected.

Required Modules

Besides the fundamental Reallusion Python module, we'll also need Pyside2 to build the user interface.

import RLPy
from PySide2.shiboken2 import wrapInstance
from PySide2 import *
</syntaxhighligh>

== Global Variables ==

We'll need to create some global variables to house the callback events for the script.

<syntaxhighlight lang="python">
event_callback = None
event_callback_id = None
dialog_callback = None
</syntaxhighligh>


== Event Callback ==

Let's create a simple event callback class to handle changes to the current selection.

<syntaxhighlight lang="python">
class EventCallback(RLPy.REventCallback):
    def __init__(self):
        RLPy.REventCallback.__init__(self)

    def OnObjectSelectionChanged(self):
        update_ui()

    def OnObjectAdded(self):
        update_ui()

    def OnObjectDeleted(self):
        update_ui()
</syntaxhighligh>

Notice that we are calling an '''update_ui''' function, expounded upon later in the article.

== Dialog Event Callback ==

A dialog event callback is also needed to cleanup the event callbacks tied to this script in the event that the window is closed.

<syntaxhighlight lang= "python">
class DialogEventCallback(RLPy.RDialogCallback):
    def __init__(self):
        RLPy.RDialogCallback.__init__(self)

    def OnDialogHide(self):
        global event_callback_id

        RLPy.REventHandler.UnregisterCallback(event_callback_id)
        return True

User Interface

Building the UI is rather straightforward...

window = RLPy.RUi.CreateRDialog()
window.SetWindowTitle("Selected Object Type")

dialog = wrapInstance(int(window.GetWindow()), QtWidgets.QDialog)
text = QtWidgets.QLabel("Nothing Selected!")

dialog.layout().addWidget(text)

event_callback = EventCallback()
event_callback_id = RLPy.REventHandler.RegisterCallback(event_callback)

dialog_callback = DialogEventCallback()
window.RegisterEventCallback(dialog_callback)

window.Show()

Update UI Function

Finally, we'll need a function inspect the object type for the first selected item and update the UI accordingly.

def update_ui():
    items = RLPy.RScene.GetSelectedObjects()

    if len(items) > 0:
        text.setText("{0} is {1}.".format(
            items[0].GetName(),
            {RLPy.EObjectType_Object: "an Object",
             RLPy.EObjectType_Avatar: "an Avatar",
             RLPy.EObjectType_Prop: "a Prop",
             RLPy.EObjectType_Camera: "a Camera",
             RLPy.EObjectType_Particle: "a Particle",
             RLPy.EObjectType_Light: "a Light",
             RLPy.EObjectType_SpotLight: "a Spot Light",
             RLPy.EObjectType_PointLight: "a Point Light",
             RLPy.EObjectType_DirectionalLight: "a Directional Light"
             }[items[0].GetType()]))
        return

    text.setText("Nothing Selected!")