Difference between revisions of "IC Python API:Animated Material"

From Reallusion Wiki!
Jump to: navigation, search
m (Everything Put Together)
m
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{TOC}}
 
{{TOC}}
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
+
{{Parent|IC_Python_API#Python_of_the_Month|Python of the Month}}
  
 
Animating material attributes are also allowed in iClone via Python scripting.  In this article, we'll go over the steps required to create a flashing light material.
 
Animating material attributes are also allowed in iClone via Python scripting.  In this article, we'll go over the steps required to create a flashing light material.
  
=== Required Modules ===
+
== Necessary Modules ==
  
 
Besides the rudimentary Reallusion python API, we'll need the random module to create random colors.
 
Besides the rudimentary Reallusion python API, we'll need the random module to create random colors.
Line 13: Line 13:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Retrieving the Material ===
+
== Retrieving the Material ==
  
 
Let's get the first selected object in the scene and grab its material components for animations.
 
Let's get the first selected object in the scene and grab its material components for animations.
Line 27: Line 27:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Animation Key ===
+
== 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.
 
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.
Line 38: Line 38:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Eliminate Diffuse Contribution ===
+
== Eliminate Diffuse Contribution ==
  
 
Reduce the diffuse texture contribution to zero for a complete diffuse color override.
 
Reduce the diffuse texture contribution to zero for a complete diffuse color override.
Line 48: Line 48:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Flashing Light Effect ===
+
== Flashing Light Effect ==
  
 
[[File:Ic_python_api_flashing_lights_01.gif|frame]]
 
[[File:Ic_python_api_flashing_lights_01.gif|frame]]
Line 71: Line 71:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
== Everything Put Together ==
=== Everything Put Together ===
+
  
 
You can copy and paste the following code into a PY file and load it into iClone via '''Script > Load Python'''.
 
You can copy and paste the following code into a PY file and load it into iClone via '''Script > Load Python'''.
Line 113: Line 112:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== APIs Used ====
+
=== APIs Used ===
  
 
You can research the following references for the APIs deployed in this code.
 
You can research the following references for the APIs deployed in this code.

Latest revision as of 19:39, 18 October 2020

Main article: Python of the Month.

Animating material attributes are also allowed in iClone via Python scripting. In this article, we'll go over the steps required to create a flashing light material.

Necessary 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

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

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.