Now with static types!

This commit is contained in:
Skye Terran 2021-08-23 16:16:23 -07:00
parent 59c3d7561d
commit c52fd22b9b
2 changed files with 134 additions and 103 deletions

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 239, "execution_count": 86,
"source": [ "source": [
"import json\r\n", "import json\r\n",
"\r\n", "\r\n",
@ -10,16 +10,16 @@
"def new_scene(name):\r\n", "def new_scene(name):\r\n",
" # create empty neutrino data\r\n", " # create empty neutrino data\r\n",
" data = {\r\n", " data = {\r\n",
" \"meta\": {\r\n",
" \"name\": (\"name\", name),\r\n",
" \"scale\": (\"float\", 1.0),\r\n",
" \"asset_path\": (\"path\", \"./\"),\r\n",
" },\r\n",
" \"graph\": {\r\n", " \"graph\": {\r\n",
" \"scene\": {\r\n", " \"scene\": {},\r\n",
" \"meta\": {\r\n",
" \"name\": name\r\n",
" },\r\n",
" \"objects\": {}\r\n",
" },\r\n",
" \"assets\": {}\r\n", " \"assets\": {}\r\n",
" },\r\n", " },\r\n",
" \"meta\": {\r\n", " \"internal\": {\r\n",
" \"max_key\": 0\r\n", " \"max_key\": 0\r\n",
" }\r\n", " }\r\n",
" }\r\n", " }\r\n",
@ -29,59 +29,57 @@
"\r\n", "\r\n",
"# write the data to a JSON file\r\n", "# write the data to a JSON file\r\n",
"def save_scene(data):\r\n", "def save_scene(data):\r\n",
" clean_data = {\r\n", " # create working copy of the scene data\r\n",
" \"graph\": data[\"graph\"]\r\n", " clean_data = data.copy()\r\n",
" }\r\n",
"\r\n", "\r\n",
" # include cache if relevant\r\n", " # get rid of internal data (not to be exported)\r\n",
" if \"cache\" in data:\r\n", " del clean_data[\"internal\"]\r\n",
" clean_data[\"cache\"] = data[\"cache\"]\r\n", " \r\n",
"\r\n", " filename = data[\"meta\"][\"name\"][1].replace(\" \", \"\") + \".json\"\r\n",
" filename = data[\"graph\"][\"scene\"][\"meta\"][\"name\"].replace(\" \", \"\") + \".json\"\r\n",
" with open(filename, \"w\") as outfile:\r\n", " with open(filename, \"w\") as outfile:\r\n",
" json.dump(clean_data, outfile, indent = 4)\r\n", " json.dump(clean_data, outfile, indent = 4)\r\n",
"\r\n", "\r\n",
"# get a new indexed object key and increment the scene's max key\r\n", "# get a new indexed object key and increment the scene's max key\r\n",
"def add_key(data):\r\n", "def add_key(data):\r\n",
" # get the indexed key\r\n", " # get the indexed key\r\n",
" key = hex(data[\"meta\"][\"max_key\"] + 1)\r\n", " key = hex(data[\"internal\"][\"max_key\"] + 1)\r\n",
"\r\n", "\r\n",
" # index the max key\r\n", " # index the max key\r\n",
" data[\"meta\"][\"max_key\"] += 1\r\n", " data[\"internal\"][\"max_key\"] += 1\r\n",
"\r\n", "\r\n",
" return key\r\n", " return key\r\n",
"\r\n", "\r\n",
"# add an asset to the graph\r\n", "# add an asset to the graph\r\n",
"def add_asset(data, name):\r\n", "def add_asset(data, name):\r\n",
" asset_data = {\r\n", " asset_data = {\r\n",
" \"name\": {\"t\": \"name\", \"v\": name}\r\n", " \"name\": (\"name\", name)\r\n",
" }\r\n", " }\r\n",
" \r\n", " \r\n",
" # add the asset to the graph\r\n", " # add the asset to the graph\r\n",
" data[\"graph\"][\"assets\"][add_key(data)] = asset_data\r\n", " data[\"graph\"][\"assets\"][add_key(data)] = (\"asset\", asset_data)\r\n",
"\r\n", "\r\n",
"# add an object to the scene\r\n", "# add an object to the scene\r\n",
"def spawn_object(data, name, asset):\r\n", "def spawn_object(data, name, asset):\r\n",
" object_data = {\r\n", " object_data = {\r\n",
" \"name\": {\"t\": \"name\", \"v\": name},\r\n", " \"name\": (\"name\", name),\r\n",
" \"asset\": {\"t\": \"asset\", \"v\": asset},\r\n", " \"asset\": (\"asset\", asset),\r\n",
" \"trans\": {\"t\": \"trans\", \"v\": [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]}\r\n", " \"trans\": (\"trans\", [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])\r\n",
" }\r\n", " }\r\n",
"\r\n", "\r\n",
" # get an asset key by the provided name\r\n", " # get an asset key by the provided name\r\n",
" for key, value in data[\"graph\"][\"assets\"].items():\r\n", " for key, value in data[\"graph\"][\"assets\"].items():\r\n",
" if value[\"name\"][\"v\"] == asset:\r\n", " if value[1][\"name\"] == asset:\r\n",
" object_data[\"asset\"][\"v\"] = f\"*{key}\"\r\n", " object_data[\"asset\"] = (\"asset\", f\"*{key}\")\r\n",
"\r\n", "\r\n",
" # add the object to the scene\r\n", " # add the object to the scene\r\n",
" data[\"graph\"][\"scene\"][\"objects\"][add_key(data)] = object_data" " data[\"graph\"][\"scene\"][add_key(data)] = (\"object\", object_data)"
], ],
"outputs": [], "outputs": [],
"metadata": {} "metadata": {}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 240, "execution_count": 87,
"source": [ "source": [
"# cache the scene\r\n", "# cache the scene\r\n",
"def cache_scene(data):\r\n", "def cache_scene(data):\r\n",
@ -89,7 +87,7 @@
" data[\"cache\"] = {}\r\n", " data[\"cache\"] = {}\r\n",
"\r\n", "\r\n",
" containers = [\r\n", " containers = [\r\n",
" data[\"graph\"][\"scene\"][\"objects\"],\r\n", " data[\"graph\"][\"scene\"],\r\n",
" data[\"graph\"][\"assets\"]\r\n", " data[\"graph\"][\"assets\"]\r\n",
" ]\r\n", " ]\r\n",
"\r\n", "\r\n",
@ -99,7 +97,7 @@
"\r\n", "\r\n",
" # hash all values\r\n", " # hash all values\r\n",
" for key, value in objects.items():\r\n", " for key, value in objects.items():\r\n",
" for key, value in value.items():\r\n", " for key, value in value[1].items():\r\n",
" # ignore pointers\r\n", " # ignore pointers\r\n",
" if type(value) == str:\r\n", " if type(value) == str:\r\n",
" is_pointer = value[0] == \"*\"\r\n", " is_pointer = value[0] == \"*\"\r\n",
@ -131,7 +129,7 @@
"\r\n", "\r\n",
" # replace all instances of cached values in the graph with corresponding cache pointers\r\n", " # replace all instances of cached values in the graph with corresponding cache pointers\r\n",
" for object_key, object_value in objects.items():\r\n", " for object_key, object_value in objects.items():\r\n",
" for value_key, value_value in object_value.items():\r\n", " for value_key, value_value in object_value[1].items():\r\n",
" # ignore pointers\r\n", " # ignore pointers\r\n",
" if type(value_value) == str:\r\n", " if type(value_value) == str:\r\n",
" is_pointer = value_value[0] == \"*\"\r\n", " is_pointer = value_value[0] == \"*\"\r\n",
@ -143,14 +141,14 @@
"\r\n", "\r\n",
" # if this value is cached, replace it with its cache pointer\r\n", " # if this value is cached, replace it with its cache pointer\r\n",
" if value_hash in hash_cache:\r\n", " if value_hash in hash_cache:\r\n",
" objects[object_key][value_key] = hash_cache[value_hash][\"pointer\"]" " objects[object_key][1][value_key] = hash_cache[value_hash][\"pointer\"]"
], ],
"outputs": [], "outputs": [],
"metadata": {} "metadata": {}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 241, "execution_count": 88,
"source": [ "source": [
"# just returns a random string\r\n", "# just returns a random string\r\n",
"import random\r\n", "import random\r\n",

View File

@ -1,87 +1,120 @@
{ {
"meta": {
"name": [
"name",
"Neutrino Test Scene"
],
"scale": [
"float",
1.0
],
"asset_path": [
"path",
"./"
]
},
"graph": { "graph": {
"scene": { "scene": {
"meta": { "0x4": [
"name": "Neutrino Test Scene" "object",
}, {
"objects": { "name": [
"0x4": { "name",
"name": { "F0O8L98N"
"t": "name", ],
"v": "EY59QAA0"
},
"asset": "#0x9",
"trans": "#0xa"
},
"0x5": {
"name": {
"t": "name",
"v": "DK7AGBDR"
},
"asset": {
"t": "asset",
"v": "*0x1"
},
"trans": "#0xa"
},
"0x6": {
"name": {
"t": "name",
"v": "TJFARWI7"
},
"asset": {
"t": "asset",
"v": "*0x2"
},
"trans": "#0xa"
},
"0x7": {
"name": {
"t": "name",
"v": "NPW1NN32"
},
"asset": "#0x9",
"trans": "#0xa"
},
"0x8": {
"name": {
"t": "name",
"v": "ZUYHYVNZ"
},
"asset": "#0x9", "asset": "#0x9",
"trans": "#0xa" "trans": "#0xa"
} }
} ],
"0x5": [
"object",
{
"name": [
"name",
"PMG7JNPR"
],
"asset": [
"asset",
"2YGVO7W4"
],
"trans": "#0xa"
}
],
"0x6": [
"object",
{
"name": [
"name",
"RW53IYCM"
],
"asset": "#0x9",
"trans": "#0xa"
}
],
"0x7": [
"object",
{
"name": [
"name",
"6SS003XM"
],
"asset": [
"asset",
"7YRERZGM"
],
"trans": "#0xa"
}
],
"0x8": [
"object",
{
"name": [
"name",
"8L4JU4I1"
],
"asset": "#0x9",
"trans": "#0xa"
}
]
}, },
"assets": { "assets": {
"0x1": { "0x1": [
"name": { "asset",
"t": "name", {
"v": "ZDENIALL" "name": [
"name",
"QCR0823N"
]
} }
}, ],
"0x2": { "0x2": [
"name": { "asset",
"t": "name", {
"v": "CLO2S21B" "name": [
"name",
"7YRERZGM"
]
} }
}, ],
"0x3": { "0x3": [
"name": { "asset",
"t": "name", {
"v": "XEG2P4VV" "name": [
"name",
"2YGVO7W4"
]
} }
} ]
} }
}, },
"cache": { "cache": {
"#0x9": { "#0x9": [
"t": "asset", "asset",
"v": "*0x3" "QCR0823N"
}, ],
"#0xa": { "#0xa": [
"t": "trans", "trans",
"v": [ [
[ [
0.0, 0.0,
0.0, 0.0,
@ -98,6 +131,6 @@
1.0 1.0
] ]
] ]
} ]
} }
} }