IC Python API:Object Type

From Reallusion Wiki!
Jump to: navigation, search
Main article: RL Python Samples.

Reading the object type is important for operations that can only be applied to certain types of assets. In this way, the object type acts like a very important filter to exclude incompatible assets. This article will demonstrate the retrieval of the object type for the first item selected.

Asset types that can be detected include the following:

Objects Avatars Props Cameras Particles
Lights Spot Lights Point Lights Directional Lights

Necessary 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 *

Global Variables

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

event_callback = None
event_callback_id = None
dialog_callback = None

Event Callback

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

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()

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.

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!")

Everything Put Together

Ic python api object type 01.gif

You can copy and paste the following code into a PY file and load it into iClone via Script > Load Python.

import RLPy
from PySide2.shiboken2 import wrapInstance
from PySide2 import *

event_callback = None
event_callback_id = None
dialog_callback = None


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()


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


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()


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!")

APIs Used

You can research the following references for the APIs deployed in this code.