Difference between revisions of "IC Python API:Bounding Info"

From Reallusion Wiki!
Jump to: navigation, search
m
m (Required Modules and Global Variables)
Line 17: Line 17:
  
 
bi_events = {}  # Global dict for events and callbacks
 
bi_events = {}  # Global dict for events and callbacks
 +
</syntaxhighlight>
 +
 +
== Event Callback ==
 +
 +
In order to sync the custom user controls with the data model, we'll need to receive event triggers on object data and selection change and tie it to an update function.  We do this by inheriting and configuring the RLPy.REventCallback class.
 +
 +
<syntaxhighlight lang="python">
 +
class BoundingInfoEventCallback(RLPy.REventCallback):
 +
    def __init__(self):
 +
        RLPy.REventCallback.__init__(self)
 +
 +
    def OnObjectDataChanged(self):
 +
        get_bounding_box()
 +
 +
    def OnObjectSelectionChanged(self):
 +
        get_bounding_box()
 +
</syntaxhighlight>
 +
 +
== Dialog Event Callback ==
 +
 +
Event callbacks are global values, therefore they tend to persist even when the user is not longer using the script. This can quickly pollute the environment with wasteful scripted calculations that can chew up memory and CPU cycles.  Therefore, we'll need a dialog event callback to discard current event callbacks created by this script and clean up the global variables.
 +
 +
<syntaxhighlight lang="python">
 +
class DialogEventCallback(RLPy.RDialogCallback):
 +
    def __init__(self):
 +
        RLPy.RDialogCallback.__init__(self)
 +
 +
    def OnDialogClose(self):
 +
        global bi_events
 +
 +
        RLPy.REventHandler.UnregisterCallback(bi_events["callback_id"])
 +
        bi_events.clear
 +
        return True
 +
</syntaxhighlight>
 +
 +
== Creating the UI ==
 +
 +
<syntaxhighlight lang="python">
 +
# Create the dialog window in iClone
 +
window = RLPy.RUi.CreateRDialog()
 +
window.SetWindowTitle("Bounding Box")
 +
 +
# Empower the window with Python QT functionalities
 +
dialog = wrapInstance(int(window.GetWindow()), QtWidgets.QDialog)
 +
dialog.setFixedWidth(350)
 +
 +
# Read the QT UI file from location and deploy as widget
 +
ui = QtCore.QFile(os.path.dirname(__file__) + "/Bounding_Info.ui")
 +
ui.open(QtCore.QFile.ReadOnly)
 +
widget = QtUiTools.QUiLoader().load(ui)
 +
ui.close()
 +
dialog.layout().addWidget(widget)
 +
 +
window.Show()
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 20:21, 20 August 2019

Main article: RL Python Samples.
Ic python api bounding info 01.png

This article will go over the handling of bounding area data in iClone and how to derive useful bounding box size information from it. Bounds of an object are the farthest extent of its vertices in world-space coordinates, therefore, they are subject to transformational changes. This can be counter-intuitive when it comes to changing rotations where one would expect the bounding box to stay the same. However, this is not the case and it more useful to think of the bounding box as a fixed orientation where the points represented do not rotate in the object's transform space.

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 house our UI and callback events that we'll need to link the custom user controls with those of iClone.

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

bi_events = {}  # Global dict for events and callbacks

Event Callback

In order to sync the custom user controls with the data model, we'll need to receive event triggers on object data and selection change and tie it to an update function. We do this by inheriting and configuring the RLPy.REventCallback class.

class BoundingInfoEventCallback(RLPy.REventCallback):
    def __init__(self):
        RLPy.REventCallback.__init__(self)

    def OnObjectDataChanged(self):
        get_bounding_box()

    def OnObjectSelectionChanged(self):
        get_bounding_box()

Dialog Event Callback

Event callbacks are global values, therefore they tend to persist even when the user is not longer using the script. This can quickly pollute the environment with wasteful scripted calculations that can chew up memory and CPU cycles. Therefore, we'll need a dialog event callback to discard current event callbacks created by this script and clean up the global variables.

class DialogEventCallback(RLPy.RDialogCallback):
    def __init__(self):
        RLPy.RDialogCallback.__init__(self)

    def OnDialogClose(self):
        global bi_events

        RLPy.REventHandler.UnregisterCallback(bi_events["callback_id"])
        bi_events.clear
        return True

Creating the UI

# Create the dialog window in iClone
window = RLPy.RUi.CreateRDialog()
window.SetWindowTitle("Bounding Box")

# Empower the window with Python QT functionalities
dialog = wrapInstance(int(window.GetWindow()), QtWidgets.QDialog)
dialog.setFixedWidth(350)

# Read the QT UI file from location and deploy as widget
ui = QtCore.QFile(os.path.dirname(__file__) + "/Bounding_Info.ui")
ui.open(QtCore.QFile.ReadOnly)
widget = QtUiTools.QUiLoader().load(ui)
ui.close()
dialog.layout().addWidget(widget)

window.Show()