IC Python API:Loading All Props

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

This article will go over the use of Python os and winreq to load all props within the iClone Template directory. Learn how to deal with and access the Window's file system with Python.

Necessary Modules

Besides the RLPy standard module, we'll need two system based modules to load a prop from a directory with the likes of os and winreg.

import RLPy
import os
import winreg

os

The os module provides a portable way of using operating system dependent functionality. For more information, see os.html

winreg

winreg offers functions that exposes the Windows registry API to Python. For more information, see winreg.html

Template Folder Path

Let's get the directory path for the iClone Template folder.

# Get the iClone 7 default template path
registry = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
rawKey = winreg.OpenKey(registry, r"SOFTWARE\Reallusion\iClone\7.0")
ic_template_path = os.path.abspath(
    winreg.QueryValueEx(rawKey, "Template Data")[0])

# Drill down to the beveled shapes directory
path = ic_template_path + r"\iClone Template\Props\3D Blocks\Beveled"

Loading All Template Props

With the template folder path, we can search the directory for all iProps, load them into iClone, assemble them in a single line, and scale them down to stand side-by-side.

index = 0

for file_name in os.listdir(path):
    if file_name.endswith(".iProp"):  # If the file is an iProp

        # Load the prop into the scene
        RLPy.RFileIO.LoadFile(path+"\\"+file_name)

        # Grab the loaded prop by name
        prop = RLPy.RScene.FindObject(RLPy.EObjectType_Prop, os.path.splitext(file_name)[0])

        # Retrieve the transform datablock to move the prop where we desire
        prop_control = prop.GetControl("Transform")
        data_block = prop_control.GetDataBlock()

        # Align the props side by side
        data_block.SetData("Position/PositionX", RLPy.RTime(0),
                           RLPy.RVariant(index * 50 - 280))

        # Scale the props down
        data_block.SetData("Scale/ScaleX", RLPy.RTime(0), RLPy.RVariant(0.4))
        data_block.SetData("Scale/ScaleY", RLPy.RTime(0), RLPy.RVariant(0.4))
        data_block.SetData("Scale/ScaleZ", RLPy.RTime(0), RLPy.RVariant(0.4))

        # Increment the index for placing the next prop
        index += 1

Iclone python api loading all props 01.png

Everything Put Together

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

import RLPy
import os
import winreg

# Get the iClone 7 default template path
registry = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
rawKey = winreg.OpenKey(registry, r"SOFTWARE\Reallusion\iClone\7.0")
ic_template_path = os.path.abspath(
    winreg.QueryValueEx(rawKey, "Template Data")[0])

# Drill down to the beveled shapes directory
path = ic_template_path + r"\iClone Template\Props\3D Blocks\Beveled"

index = 0

for file_name in os.listdir(path):
    if file_name.endswith(".iProp"):  # If the file is an iProp

        # Load the prop into the scene
        RLPy.RFileIO.LoadFile(path+"\\"+file_name)

        # Grab the loaded prop by name
        prop = RLPy.RScene.FindObject(RLPy.EObjectType_Prop, os.path.splitext(file_name)[0])

        # Retrieve the transform datablock to move the prop where we desire
        prop_control = prop.GetControl("Transform")
        data_block = prop_control.GetDataBlock()

        # Align the props side by side
        data_block.SetData("Position/PositionX", RLPy.RTime(0),
                           RLPy.RVariant(index * 50 - 280))

        # Scale the props down
        data_block.SetData("Scale/ScaleX", RLPy.RTime(0), RLPy.RVariant(0.4))
        data_block.SetData("Scale/ScaleY", RLPy.RTime(0), RLPy.RVariant(0.4))
        data_block.SetData("Scale/ScaleZ", RLPy.RTime(0), RLPy.RVariant(0.4))

        # Increment the index for placing the next prop
        index += 1

APIs Used

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