Swap serialize/sub-serialize, always solve which Serializer class to use

This commit is contained in:
Jonas Holzman 2025-05-27 12:10:07 +02:00
parent cf5095202b
commit 510de4253d

View File

@ -23,6 +23,7 @@ def serialize_selected_nodes_from_node_tree(node_tree: bpy.types.NodeTree):
links_data = [Serializer.serialize(link, bl_pointers) for link in selected_links] links_data = [Serializer.serialize(link, bl_pointers) for link in selected_links]
# Only serialize selected nodes and their links # Only serialize selected nodes and their links
# Data format corresponds to the bpy.types.NodeTree properties that we want to (de)serialize
ntree_data = { ntree_data = {
"nodes": nodes_data, "nodes": nodes_data,
"links": links_data, "links": links_data,
@ -62,10 +63,21 @@ class Serializer(ABC):
return None return None
# --- Serialization --- # --- Serialization ---
@classmethod
def serialize(cls, obj: bpy.types.bpy_struct | Any, bl_pointers_ref: dict) -> Any:
if not isinstance(obj, bpy.types.bpy_struct):
# Primitive type, return directly
return obj
"""Resolve which Serializer class to use"""
serializer = cls.get_serializer(obj)
return serializer.serialize_obj(obj, bl_pointers_ref)
@classmethod @classmethod
@abstractmethod @abstractmethod
def serialize(cls, obj: bpy.types.bpy_struct | Any, bl_pointers_ref: dict) -> dict: def serialize_obj(cls, obj: bpy.types.bpy_struct | Any, bl_pointers_ref: dict) -> dict:
"""Base serialization method, overridden by subclasses""" """Base serialization method, overridden by subclasses"""
# Early recursive return case # Early recursive return case
@ -96,7 +108,7 @@ class Serializer(ABC):
@classmethod @classmethod
def serialize_property(cls, bl_prop: BlenderProperty, bl_pointers_ref: dict) -> Any: def serialize_property(cls, bl_prop: BlenderProperty, bl_pointers_ref: dict) -> Any:
"""Serialize node property, special cases for arrays/collections/pointers""" """Serialize Blender property, special cases for arrays/collections/pointers"""
# Property array case # Property array case
if getattr(bl_prop.rep, "is_array", False): if getattr(bl_prop.rep, "is_array", False):
# Contained in BoolProperty, IntProperty and FloatProperty # Contained in BoolProperty, IntProperty and FloatProperty
@ -113,7 +125,7 @@ class Serializer(ABC):
if not collection: if not collection:
return [] return []
values = [cls.sub_serialize(sub_prop) for sub_prop in collection] values = [cls.serialize(sub_prop) for sub_prop in collection]
# TODO: Check why the original code has a None check # TODO: Check why the original code has a None check
return [v for v in values if v is not None] return [v for v in values if v is not None]
@ -127,19 +139,8 @@ class Serializer(ABC):
return ptr return ptr
bl_pointers_ref[ptr] = bl_prop.attr bl_pointers_ref[ptr] = bl_prop.attr
return cls.sub_serialize(bl_prop.attr, bl_pointers_ref) return cls.serialize(bl_prop.rep, bl_pointers_ref)
@classmethod
def sub_serialize(cls, obj: bpy.types.bpy_struct | Any, bl_pointers_ref: dict) -> Any:
if not isinstance(obj, bpy.types.bpy_struct):
# Primitive type, return directly
return obj
"""Resolve which Serializer class to use"""
serializer = cls.get_serializer(obj)
return serializer.serialize(obj, bl_pointers_ref)
# --- Deserialization --- # --- Deserialization ---