JSONファイルのインポートとエクスポート

公開 更新

HDAのUI要素をJSON形式で保存する。

エクスポート

PythonのCallback Scriptで記述したコード。

#
# エクスポート
#
import json
import os

def onExportJson(kwargs):
    # HDAを指定する
    node = kwargs['node']

    # 出力パス
    filePath = node.parm("exportPath").eval()

    all_data = []
    for i in range(node.parm("prim").evalAsInt()):
        index = i
        entry = {} # 辞書型
        
        # id
        id = node.parm('id_%d' % index).eval()
        entry["id"] = id
        
        # name
        name = node.parm('name_%d' % index).eval()
        entry["name"] = name
        
        # name
        type = node.parm('roadType_%d' % index).eval()
        entry["roadType"] = type
        
        laneLeft = node.parm('laneLeft_%d' % index).eval()
        entry["laneLeft"] = laneLeft
        
        laneRight = node.parm('laneRight_%d' % index).eval()
        entry["laneRight"] = laneRight
        
        laneWidth = node.parm('laneWidth_%d' % index).eval()
        entry["laneWidth"] = laneWidth
        
        # point pos
        point = []
        multiparm_point = node.parm('points_%d' % index)
        for j in range(multiparm_point.evalAsInt()):
            parm_pos = node.parmTuple("pt_%d_%d" % (index, j)).eval()
            #useRadius = node.parm("useCurvatureRadius_%d_%d" % (index, j)).eval()
            #radius = node.parm("curvatureRadius_%d_%d" % (index, j)).eval()
            point.append({
                "pos": parm_pos,
                #"useRadius": useRadius,
                #"radius": radius
            })
        entry["point"] = point
        
        all_data.append(entry)
            
    # JSONファイルに保存
    with open(filePath, "w", encoding="utf-8") as json_file:
        json.dump(all_data, json_file, indent=4, ensure_ascii=False)
        
    # フォルダのパスを取得して、フォルダを開く(Windows)
    folder_path = os.path.dirname(filePath)
    os.startfile(folder_path)

entryという辞書型のオブジェクトをつくり、そこにキーと値を入力していく。各entryをall_dataというリストにまとめる。

# entryの内容
{'id': 'f0306547', 'name': '', 'roadType': 2, 'laneLeft': 1, 'laneRight': 1, 'laneWidth': 1.0, 'point': [{'pos': (1078.0423583984375, 112.5302734375, 526.1370849609375)}, {'pos': (1096.39111328125, 113.0234375, 521.8391723632812)}, {'pos': (1126.2235107421875, 113.3115234375, 529.3990478515625)}, {'pos': (1157.305419921875, 113.2314453125, 525.4832153320312)}, {'pos': (1190.1944580078125, 110.1865234375, 526.432373046875)}, {'pos': (1209.8333740234375, 109.7578125, 526.432373046875)}]}

json.dump() でJSON形式に変換している。

JSONデータ

出力されたデータはこのようになる。ほぼ辞書型と同じ構造。

[
    {
        "id": "f0306547",
        "name": "",
        "roadType": 2,
        "laneLeft": 1,
        "laneRight": 1,
        "laneWidth": 1.0,
        "point": [
            {
                "pos": [
                    1078.0423583984375,
                    112.5302734375,
                    526.1370849609375
                ]
            },
            {
                "pos": [
                    1096.39111328125,
                    113.0234375,
                    521.8391723632812
                ]
            },
            {
                "pos": [
                    1126.2235107421875,
                    113.3115234375,
                    529.3990478515625
                ]
            },
            {
                "pos": [
                    1157.305419921875,
                    113.2314453125,
                    525.4832153320312
                ]
            },
            {
                "pos": [
                    1190.1944580078125,
                    110.1865234375,
                    526.432373046875
                ]
            },
            {
                "pos": [
                    1209.8333740234375,
                    109.7578125,
                    526.432373046875
                ]
            },
            {
                "pos": [
                    1225.163818359375,
                    108.57421875,
                    528.5253295898438
                ]
            }
        ]
    },
    {
        "id": "77028fac",
        "name": "",
        "roadType": 0,
        "laneLeft": 1,
        "laneRight": 1,
        "laneWidth": 2.0,
        "point": [
            {
                "pos": [
                    1498.3621826171875,
                    194.3349609375,
                    346.3238830566406
                ]
            },
            {
                "pos": [
                    1448.8910810613365,
                    191.22689315673927,
                    358.7813692784315
                ]
            },
            {
                "pos": [
                    1399.60546875,
                    184.59375,
                    357.7013854980469
                ]
            },
            {
                "pos": [
                    1353.6488037109375,
                    182.1650390625,
                    346.82098388671875
                ]
            },
            {
                "pos": [
                    1307.922119140625,
                    178.9736328125,
                    334.58795166015625
                ]
            },
            {
                "pos": [
                    1278.413330078125,
                    174.693359375,
                    318.661865234375
                ]
            },
            {
                "pos": [
                    1247.0721435546875,
                    171.193359375,
                    318.4028625488281
                ]
            },
            {
                "pos": [
                    1218.2071533203125,
                    170.0888671875,
                    310.4221496582031
                ]
            }
        ]
    }
]

インポート

PythonのCallback Scriptで記述したコード。json.load()で読み込まれたものはエクポート時のall_dataと同じ値(辞書型のリスト)になる。

#
# インポート
#
import json

def onImportJson(kwargs):
    node = kwargs['node']

    # JSONファイルの読み込み
    json_path = node.parm("importPath").eval()

    with open(json_path, "r", encoding="utf-8") as json_file:
        data = json.load(json_file)
    
    if node.parm("importType") == 0:
        # 存在するプリミティブをすべて削除
        node.parm("prims").set(0)

    # JSONファイル内
    index = 0
    for i, item in enumerate(data):
        # ID
        id = item["id"]
        node.parm("id_%d" % (index)).set(id)
        
        name = item["name"]
        node.parm("name_%d" % (index)).set(name)
        
        road_type = item["roadType"]
        node.parm("roadType_%d" % (index)).set(road_type)
        
        lane_left = item["laneLeft"]
        node.parm("laneLeft_%d" % (index)).set(lane_left)
        
        lane_right = item["laneRight"]
        node.parm("laneRight_%d" % (index)).set(lane_right)
        
        lane_width = item["laneWidth"]
        node.parm("laneWidth_%d" % (index)).set(lane_width)
        
        # Point Pos
        node.parm("points_%d" % (index)).set(0)
        for j, data in enumerate(item["point"]):
            node.parm("points_%d" % (index)).insertMultiParmInstance(j)
            
            pos = data["pos"]
            node.parmTuple("pt_%d_%d" % (index, j)).set(pos)
            
            if "useRadius" in data:
                use_radius = data["useRadius"]
                node.parm("useCurvatureRadius_%d_%d" % (index, j)).set(use_radius)
            
            if "radius" in data:
                radius = data["radius"]
                node.parm("curvatureRadius_%d_%d" % (index, j)).set(radius)

        index += 1

← 記事一覧へ