Difference between revisions of "IC Python API:Transform Math"

From Reallusion Wiki!
Jump to: navigation, search
(Created page with "{{TOC}} {{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}} This article will go some recipes for common 3D transformational math. You'll find this helpful if you n...")
 
m
Line 25: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Transform Direction ==
+
In order to specify the rotation in world-space, use the following instead:
 
+
Transforms direction of a vector from local-space to world-space.  The returned vector has the same length as the direction.
+
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
def transform_point(world_transform, local_position):
+
def transform_point(world_transform, specific_rotation, local_position):
 
     # Get the transform matrix4
 
     # Get the transform matrix4
 
     world_matrix = world_transform.Matrix()
 
     world_matrix = world_transform.Matrix()
 +
    world_matrix.SetSR(specific_rotation)
  
 
     # New matrix4 for the local position
 
     # New matrix4 for the local position
Line 46: Line 45:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In order to specify the rotation in world-space, use the following instead:
+
== Transform Direction ==
 +
 
 +
Transforms direction of a vector from local-space to world-space.  The returned vector has the same length as the direction.
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
def transform_point(world_transform, specific_rotation, local_position):
+
def transform_direction(world_transform, local_position):
     # Get the transform matrix4
+
     # Get the transform rotation 3x3 matrix
     world_matrix = world_transform.Matrix()
+
     world_rot_matrix = world_transform.Rotate()
    world_matrix.SetSR(specific_rotation)
+
  
     # New matrix4 for the local position
+
     # New matrix4 for world direction
 +
    world_dir = RLPy.RMatrix4()
 +
    world_dir.MakeIdentity()
 +
    world_dir.SetSR(world_rot_matrix)
 +
 
 +
    # New matrix for the local position
 
     point_world_matrix = RLPy.RMatrix4()
 
     point_world_matrix = RLPy.RMatrix4()
 
     point_world_matrix.MakeIdentity()
 
     point_world_matrix.MakeIdentity()
Line 60: Line 65:
  
 
     # Combine the 2 matrix4
 
     # Combine the 2 matrix4
     point_world_matrix = point_world_matrix * world_matrix
+
     point_world_matrix = point_world_matrix * world_dir
  
 
     # Return the translation element of the combined matrix4
 
     # Return the translation element of the combined matrix4
 
     return RLPy.RVector3(point_world_matrix.GetTranslate())
 
     return RLPy.RVector3(point_world_matrix.GetTranslate())
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:31, 17 February 2020

Main article: RL Python Samples.

This article will go some recipes for common 3D transformational math. You'll find this helpful if you need to perform spacial calculations.

Transform Point

Transforms position of a point from local-space to world-space. The returned position is affected by scale.

def transform_point(world_transform, local_position):
    # Get the transform matrix4
    world_matrix = world_transform.Matrix()

    # New matrix4 for the local position
    point_world_matrix = RLPy.RMatrix4()
    point_world_matrix.MakeIdentity()
    point_world_matrix.SetTranslate(local_position)

    # Combine the 2 matrix4s
    point_world_matrix = point_world_matrix * world_matrix

    # Return the translation element of the combined matrix4
    return RLPy.RVector3(point_world_matrix.GetTranslate())

In order to specify the rotation in world-space, use the following instead:

def transform_point(world_transform, specific_rotation, local_position):
    # Get the transform matrix4
    world_matrix = world_transform.Matrix()
    world_matrix.SetSR(specific_rotation)

    # New matrix4 for the local position
    point_world_matrix = RLPy.RMatrix4()
    point_world_matrix.MakeIdentity()
    point_world_matrix.SetTranslate(local_position)

    # Combine the 2 matrix4
    point_world_matrix = point_world_matrix * world_matrix

    # Return the translation element of the combined matrix4
    return RLPy.RVector3(point_world_matrix.GetTranslate())

Transform Direction

Transforms direction of a vector from local-space to world-space. The returned vector has the same length as the direction.

def transform_direction(world_transform, local_position):
    # Get the transform rotation 3x3 matrix
    world_rot_matrix = world_transform.Rotate()

    # New matrix4 for world direction
    world_dir = RLPy.RMatrix4()
    world_dir.MakeIdentity()
    world_dir.SetSR(world_rot_matrix)

    # New matrix for the local position
    point_world_matrix = RLPy.RMatrix4()
    point_world_matrix.MakeIdentity()
    point_world_matrix.SetTranslate(local_position)

    # Combine the 2 matrix4
    point_world_matrix = point_world_matrix * world_dir

    # Return the translation element of the combined matrix4
    return RLPy.RVector3(point_world_matrix.GetTranslate())