IC Python API:Item Lister
- Main article: RL Python Samples.
There are two main lessons for this article: Capturing a state of the scene by recording user specified objects and pruning the list when the said objects become obsolete. We demonstrate these two priorities with a script that can record user specified objects in a list window.
Required Modules and Global Variables
Besides the fundamental Reallusion Python module, we'll also need Pyside2 and os to read the QT UI file and build the user interface. We'll also need a global variable to store the various designated objects in the scene.
import RLPy
import os
from PySide2 import *
from PySide2.shiboken2 import wrapInstance
recorded_objects = {}
Event Callbacks
We'll need a way to prune the recorded list by verifying it again when an object in the scene is deleted, performed by an REventCallback. However, we'll also need to clean up the said callback event when the window is closed, so we'll also need a RDialogCallback to rescind the aforementioned event callback.
class EventCallback(RLPy.REventCallback):
def __init__(self):
RLPy.REventCallback.__init__(self)
def OnObjectDeleted(self):
prune_list()
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
Notice that we call the prune_list method in the event that an object is deleted from the scene.
Pruning the List
Let's create a function to prune the list represented by the recorded_objects dictionary. We test for the existence of an object with the RLPy.RIBase.IsValid() member function.
def prune_list_list():
widget.listWidget.clear()
for name, item in list(recorded_objects.items()):
if item.IsValid():
widget.listWidget.insertItem(widget.listWidget.count(), name)
else:
del recorded_objects[name]
Notice that when we iterate over the list, we actually iterate over a copy of it by casting it to another list() type. This is so we can safely delete from the list while changing its size.