Difference between revisions of "IC Python API:Saving JSON"

From Reallusion Wiki!
Jump to: navigation, search
(Created page with "{{TOC}} {{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}")
 
m
Line 1: Line 1:
 
{{TOC}}
 
{{TOC}}
 
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
 
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
 +
{{Sibling|IC_Python_API:Loading_JSON|Loading JSON}}
 +
 +
This article will go over the handling of JSON formatted data for iClone.  This is done by saving transformational data for all the props in the current iClone scene to an external '''.json''' file.
 +
 +
== Necessary Modules ==
 +
 +
<syntaxhighlight lang="Python">
 +
import RLPy
 +
import os
 +
import json
 +
</syntaxhighlight>
 +
 +
== Global Variables ==
 +
 +
<syntaxhighlight lang="Python">
 +
data = {"props": []}  # Empty array to store object data
 +
time = RLPy.RTime(0)
 +
decimal_places = 3  # How accurately do we want to store the transform data?
 +
exclusions = ["Shadow Catcher "]  # Create a list objects to ignore when saving
 +
all_props = RLPy.RScene.FindObjects(RLPy.EObjectType_Prop)
 +
</syntaxhighlight>
 +
 +
== Recording Transform Data ==
 +
 +
<syntaxhighlight lang="Python">
 +
for prop in all_props:
 +
    if prop.GetName() in exclusions:
 +
        continue
 +
    control = prop.GetControl("Transform")
 +
    data_block = control.GetDataBlock()
 +
    data["props"].append(
 +
        {"Name": prop.GetName(),
 +
        "Position": {
 +
            "x": round(data_block.GetData("Position/PositionX", time).ToFloat(), decimal_places),
 +
            "y": round(data_block.GetData("Position/PositionY", time).ToFloat(), decimal_places),
 +
            "z": round(data_block.GetData("Position/PositionZ", time).ToFloat(), decimal_places)
 +
        },
 +
            "Rotation": {
 +
            "x": round(data_block.GetData("Rotation/RotationX", time).ToFloat(), decimal_places),
 +
            "y": round(data_block.GetData("Rotation/RotationY", time).ToFloat(), decimal_places),
 +
            "z": round(data_block.GetData("Rotation/RotationZ", time).ToFloat(), decimal_places)
 +
        },
 +
            "Scale": {
 +
            "x": round(data_block.GetData("Scale/ScaleX", time).ToFloat(), decimal_places),
 +
            "y": round(data_block.GetData("Scale/ScaleY", time).ToFloat(), decimal_places),
 +
            "z": round(data_block.GetData("Scale/ScaleZ", time).ToFloat(), decimal_places)
 +
        }
 +
        }
 +
    )
 +
</syntaxhighlight>
 +
 +
== Saving the JSON File ==
 +
 +
<syntaxhighlight lang="Python">
 +
# Prepare the filepath and save the json file
 +
file_name = "all_prop_transformations.json"
 +
 +
local_path = os.path.dirname(os.path.realpath(__file__))
 +
json_file_path = os.path.join(local_path, file_name)
 +
 +
with open(json_file_path, "w") as json_file:
 +
    json.dump(data, json_file)
 +
</syntaxhighlight>
 +
 +
== Reading Back the JSON File ==
 +
 +
<syntaxhighlight lang="Python">
 +
# Read back the data and pretty print it in the console
 +
with open(json_file_path, "r") as json_file:
 +
    data_in = json.load(json_file)
 +
    print(json.dumps(data_in, indent=4, sort_keys=True))
 +
</syntaxhighlight>

Revision as of 21:42, 8 March 2020

Main article: RL Python Samples.
Related article: Loading JSON.

This article will go over the handling of JSON formatted data for iClone. This is done by saving transformational data for all the props in the current iClone scene to an external .json file.

Necessary Modules

import RLPy
import os
import json

Global Variables

data = {"props": []}  # Empty array to store object data
time = RLPy.RTime(0)
decimal_places = 3  # How accurately do we want to store the transform data?
exclusions = ["Shadow Catcher "]  # Create a list objects to ignore when saving
all_props = RLPy.RScene.FindObjects(RLPy.EObjectType_Prop)

Recording Transform Data

for prop in all_props:
    if prop.GetName() in exclusions:
        continue
    control = prop.GetControl("Transform")
    data_block = control.GetDataBlock()
    data["props"].append(
        {"Name": prop.GetName(),
         "Position": {
            "x": round(data_block.GetData("Position/PositionX", time).ToFloat(), decimal_places),
            "y": round(data_block.GetData("Position/PositionY", time).ToFloat(), decimal_places),
            "z": round(data_block.GetData("Position/PositionZ", time).ToFloat(), decimal_places)
        },
            "Rotation": {
            "x": round(data_block.GetData("Rotation/RotationX", time).ToFloat(), decimal_places),
            "y": round(data_block.GetData("Rotation/RotationY", time).ToFloat(), decimal_places),
            "z": round(data_block.GetData("Rotation/RotationZ", time).ToFloat(), decimal_places)
        },
            "Scale": {
            "x": round(data_block.GetData("Scale/ScaleX", time).ToFloat(), decimal_places),
            "y": round(data_block.GetData("Scale/ScaleY", time).ToFloat(), decimal_places),
            "z": round(data_block.GetData("Scale/ScaleZ", time).ToFloat(), decimal_places)
        }
        }
    )

Saving the JSON File

# Prepare the filepath and save the json file
file_name = "all_prop_transformations.json"

local_path = os.path.dirname(os.path.realpath(__file__))
json_file_path = os.path.join(local_path, file_name)

with open(json_file_path, "w") as json_file:
    json.dump(data, json_file)

Reading Back the JSON File

# Read back the data and pretty print it in the console
with open(json_file_path, "r") as json_file:
    data_in = json.load(json_file)
    print(json.dumps(data_in, indent=4, sort_keys=True))