IC Python API:Animated Material

From Reallusion Wiki!
Revision as of 21:15, 16 June 2019 by Chuck (RL) (Talk | contribs) (Flashing Light Effect)

Jump to: navigation, search
Main article: RL Python Samples.

Required Modules

Besides the rudimentary Reallusion python API, we'll need the random module to create random colors.

import RLPy
import random

Retrieving the Material

Let's get the first selected object in the scene and grab its material components for animations.

# Grab the selected prop in the scene
prop = RLPy.RScene.GetSelectedObjects()[0]

# Get Material from Prop
material_component = prop.GetMaterialComponent()
mesh_name = prop.GetMeshNames()[0]
material_name = material_component.GetMaterialNames(mesh_name)[0]

Animation Key

In order to animate the material component, we must first create a key. This same key can be re-used by simply changing its parameters.

# Create a key for reuse
key = RLPy.RKey()
key.SetTime(RLPy.RTime(0))
key.SetTransitionType(RLPy.ETransitionType_Step)

Eliminate Diffuse Contribution

Reduce the diffuse texture contribution to zero for a complete diffuse color override.

# Reduce Diffuse texture contribution to 0
material_component.AddTextureWeightKey(
    key, mesh_name, material_name, RLPy.EMaterialTextureChannel_Diffuse, 0)

Flashing Light Effect

Ic python api flashing lights 01.gif

Assign random colors with 50x power to create a flashing light effect and play the timeline.

for i in range(100):
    # Power up the rgb channels to create a flashing effect
    r = random.random() ** 10
    g = random.random() ** 10
    b = random.random() ** 10

    random_rgb = RLPy.RRgb(r, g, b).Normalize().Saturate()

    key.SetTime(RLPy.RTime(i * 50))  # Set key on every 50 frame interval

    material_component.AddDiffuseKey(
        key, mesh_name, material_name, random_rgb * 10)  # Intensity the rgb channels to simulate lighting

RLPy.RGlobal.Play(RLPy.RTime(0), RLPy.RTime(100 * 50))


Everything Put Together

import RLPy
import random

# Grab the selected prop in the scene
prop = RLPy.RScene.GetSelectedObjects()[0]

# Get Material from Prop
material_component = prop.GetMaterialComponent()
mesh_name = prop.GetMeshNames()[0]
material_name = material_component.GetMaterialNames(mesh_name)[0]

# Create a key for reuse
key = RLPy.RKey()
key.SetTime(RLPy.RTime(0))
key.SetTransitionType(RLPy.ETransitionType_Step)

# Reduce Diffuse texture contribution to 0
material_component.AddTextureWeightKey(
    key, mesh_name, material_name, RLPy.EMaterialTextureChannel_Diffuse, 0)

for i in range(100):
    # Power up the rgb channels to create a flashing effect
    r = random.random() ** 10
    g = random.random() ** 10
    b = random.random() ** 10

    random_rgb = RLPy.RRgb(r, g, b).Normalize().Saturate()

    key.SetTime(RLPy.RTime(i * 50))  # Set key on every 50 frame interval

    material_component.AddDiffuseKey(
        key, mesh_name, material_name, random_rgb * 10)  # Intensity the rgb channels to simulate lighting

RLPy.RGlobal.Play(RLPy.RTime(0), RLPy.RTime(100 * 50))

APIs Used

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