Difference between revisions of "IC Python API:Object Type"
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...") |
Chuck (RL) (Talk | contribs) m (→Required Modules) |
||
Line 9: | Line 9: | ||
from PySide2.shiboken2 import wrapInstance | from PySide2.shiboken2 import wrapInstance | ||
from PySide2 import * | from PySide2 import * | ||
− | </ | + | </syntaxhighlight> |
== Global Variables == | == Global Variables == | ||
Line 19: | Line 19: | ||
event_callback_id = None | event_callback_id = None | ||
dialog_callback = None | dialog_callback = None | ||
− | </ | + | </syntaxhighlight> |
Line 39: | Line 39: | ||
def OnObjectDeleted(self): | def OnObjectDeleted(self): | ||
update_ui() | update_ui() | ||
− | </ | + | </syntaxhighlight> |
Notice that we are calling an '''update_ui''' function, expounded upon later in the article. | Notice that we are calling an '''update_ui''' function, expounded upon later in the article. |
Revision as of 23:15, 16 September 2019
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.
Contents
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 *
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!")