Initial Dumper static functions cleanup
parent
b251a3b122
commit
18f75eed25
|
@ -1,61 +1,61 @@
|
||||||
import bpy
|
import bpy
|
||||||
import mathutils
|
|
||||||
from pprint import pprint
|
|
||||||
import json
|
|
||||||
import itertools
|
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from os.path import abspath
|
from os.path import abspath
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
||||||
def get_default(prop):
|
def get_default(prop: bpy.types.Property):
|
||||||
"""Get the default value of a bl property"""
|
"""Get the default value of a Blender property"""
|
||||||
|
|
||||||
if getattr(prop, "is_array", False):
|
if getattr(prop, "is_array", False):
|
||||||
return list(prop.default_array)
|
return list(prop.default_array)
|
||||||
elif hasattr(prop, "default"):
|
elif hasattr(prop, "default"):
|
||||||
return prop.default
|
return prop.default
|
||||||
|
|
||||||
|
|
||||||
def get_dumper(bl_object, fallback=None):
|
def get_dumper(bl_object: bpy.types.bpy_struct):
|
||||||
"""Find the right dumper type by checking inheritance"""
|
"""Get the corresponding dumper for a given Blender object, or its closest base type"""
|
||||||
for dp in dumpers:
|
for dp in dumpers:
|
||||||
if isinstance(bl_object, dp.bl_type):
|
if isinstance(bl_object, dp.bl_type): # TODO: Cascade system
|
||||||
return dp
|
return dp
|
||||||
|
|
||||||
return fallback or Dumper
|
return Dumper or None
|
||||||
|
|
||||||
|
|
||||||
def get_bl_object(data):
|
def get_current_node_tree(data):
|
||||||
"""Find the bl object for loading data into it depending on the type and the context"""
|
|
||||||
if data.get("_new", {}).get("type") == "GeometryNodeTree":
|
if data.get("_new", {}).get("type") == "GeometryNodeTree":
|
||||||
return bpy.context.object.modifiers.active.node_group
|
return bpy.context.object.modifiers.active.node_group
|
||||||
|
|
||||||
|
|
||||||
def dump_nodes(ob):
|
def dump_nodes(nodes: list[bpy.types.Node]):
|
||||||
"""Generic Recursive Dump, convert any object into a dict"""
|
"""Generic Recursive Dump, convert any object into a dict"""
|
||||||
Dumper.pointers.clear()
|
Dumper.pointers.clear() # TODO: Bad global
|
||||||
|
|
||||||
if isinstance(ob, (list, tuple)):
|
data = [dump_node(node) for node in nodes]
|
||||||
data = [get_dumper(o).dump(o) for o in ob]
|
|
||||||
else:
|
|
||||||
data = get_dumper(ob).dump(ob)
|
|
||||||
|
|
||||||
Dumper.pointers.clear()
|
Dumper.pointers.clear()
|
||||||
|
|
||||||
|
print("---")
|
||||||
|
pprint(data)
|
||||||
|
print("---")
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def dump_node(node: bpy.types.Node):
|
||||||
|
dumper = get_dumper(node)
|
||||||
|
return dumper.dump(node) # TODO: Break the recursivity, clear things up
|
||||||
|
|
||||||
def load_nodes(data, bl_object=None):
|
|
||||||
|
def load_nodes(data, node_tree=None):
|
||||||
"""Generic Load to create an object from a dict"""
|
"""Generic Load to create an object from a dict"""
|
||||||
|
|
||||||
Dumper.pointers.clear()
|
Dumper.pointers.clear()
|
||||||
# print(Dumper.pointers)
|
# print(Dumper.pointers)
|
||||||
|
|
||||||
if bl_object is None:
|
if node_tree is None:
|
||||||
bl_object = get_bl_object(data)
|
node_tree = get_current_node_tree(data)
|
||||||
|
|
||||||
dumper = get_dumper(bl_object)
|
dumper = get_dumper(node_tree)
|
||||||
dumper.load(data, bl_object)
|
dumper.load(data, node_tree)
|
||||||
|
|
||||||
Dumper.pointers.clear()
|
Dumper.pointers.clear()
|
||||||
|
|
||||||
|
@ -96,7 +96,6 @@ class Dumper:
|
||||||
if bl_object is None:
|
if bl_object is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
# pprint(data)
|
|
||||||
if bl_pointer := data.get("bl_pointer"):
|
if bl_pointer := data.get("bl_pointer"):
|
||||||
cls.pointers[bl_pointer] = bl_object
|
cls.pointers[bl_pointer] = bl_object
|
||||||
|
|
||||||
|
@ -114,7 +113,7 @@ class Dumper:
|
||||||
dumper = PropCollection
|
dumper = PropCollection
|
||||||
if hasattr(attr, "bl_rna"):
|
if hasattr(attr, "bl_rna"):
|
||||||
bl_type = attr.bl_rna.type_recast()
|
bl_type = attr.bl_rna.type_recast()
|
||||||
dumper = get_dumper(bl_type, fallback=PropCollection)
|
dumper = PropCollection or get_dumper(bl_type)
|
||||||
|
|
||||||
dumper.load(value, attr)
|
dumper.load(value, attr)
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue