Difference between revisions of "IC Python API:Object Type"

From Reallusion Wiki!
Jump to: navigation, search
m (Update UI Function)
m (Required Modules)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
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.
+
{{TOC}}
 +
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
  
== Required Modules ==
+
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:
 +
{| class = "wikitable" style="text-align:center;"
 +
|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.
 
Besides the fundamental Reallusion Python module, we'll also need Pyside2 to build the user interface.
Line 108: Line 125:
 
== Everything Put Together ==
 
== Everything Put Together ==
  
You can copy and paste the following code into a PY file and load it into iClone via Script > Load Python.  
+
[[File:Ic_python_api_object_type_01.gif|right]]
 +
 
 +
You can copy and paste the following code into a PY file and load it into iClone via '''Script > Load Python'''.  
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Line 181: Line 200:
  
 
     text.setText("Nothing Selected!")
 
     text.setText("Nothing Selected!")
<syntaxhighlight>
+
</syntaxhighlight>
  
 
== APIs Used ==
 
== APIs Used ==

Latest revision as of 20:24, 30 September 2019

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.