Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions packages/tree_clipper/src/tree_clipper/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import bpy

from types import NoneType
from typing import Any, Callable, TYPE_CHECKING
from pathlib import Path
import tempfile
from collections.abc import Callable
from pathlib import Path
from types import NoneType
from typing import TYPE_CHECKING, Any

import bpy

if TYPE_CHECKING:
from .export_nodes import Exporter
Expand Down Expand Up @@ -47,15 +47,13 @@
PROP_TYPE_ENUM = "ENUM"
PROP_TYPE_POINTER = "POINTER"
PROP_TYPE_COLLECTION = "COLLECTION"
SIMPLE_PROPERTY_TYPES_AS_STRS = set(
[
PROP_TYPE_BOOLEAN,
PROP_TYPE_INT,
PROP_TYPE_FLOAT,
PROP_TYPE_STRING,
PROP_TYPE_ENUM,
]
)
SIMPLE_PROPERTY_TYPES_AS_STRS = {
PROP_TYPE_BOOLEAN,
PROP_TYPE_INT,
PROP_TYPE_FLOAT,
PROP_TYPE_STRING,
PROP_TYPE_ENUM,
}
NODE_TREE = "node_tree"
DIMENSIONS = "dimensions"

Expand Down Expand Up @@ -110,15 +108,15 @@ def most_specific_type_handled(
return next(
(
ty
for ty in specific_handlers.keys()
for ty in specific_handlers
if ty != NoneType and ty.bl_rna.identifier == obj.bl_rna.identifier # type: ignore
),
NoneType,
)

ty = type(obj)
while True:
if ty in specific_handlers.keys():
if ty in specific_handlers:
return ty
if len(ty.__bases__) == 0:
return NoneType
Expand Down
11 changes: 7 additions & 4 deletions packages/tree_clipper/src/tree_clipper/dynamic_pointer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import bpy

from typing import Type

from .common import no_clobber


# TODO: we might need to return a list that fits the Blender version
KNOWN_POINTABLES = {
bpy.types.SunLight,
Expand Down Expand Up @@ -75,7 +72,7 @@

def add_all_known_pointer_properties(
*,
cls: Type[bpy.types.PropertyGroup],
cls: type[bpy.types.PropertyGroup],
prefix: str,
):
def get_pointer_property_name(ty: type):
Expand Down Expand Up @@ -106,6 +103,12 @@ def set_active_pointer_type(self, type_name: str):
for ty in KNOWN_POINTABLES:
setattr(self, get_pointer_property_name(ty), None)

# Otherwise, there might be no font to select
# https://github.com/Algebraic-UG/tree_clipper/issues/219
if type_name == "VectorFont" and not bpy.data.fonts:
text_curve = bpy.data.curves.new("Temp Text", type="FONT")
bpy.data.curves.remove(text_curve)

# this is needed to display the property
def get_active_pointer_identifier(self) -> str:
return f"{prefix}{self.active_ptr_type_name}"
Expand Down
54 changes: 26 additions & 28 deletions packages/tree_clipper/src/tree_clipper/export_nodes.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
import bpy

import base64
import gzip
import json
from collections.abc import Iterator
from pathlib import Path
from types import NoneType
from typing import Any, cast, Type, Iterator, Tuple
from typing import Any, cast

import bpy

from .common import (
BL_RNA,
BLENDER_VERSION,
CURRENT_TREE_CLIPPER_VERSION,
DATA,
DEFAULT_VALUE,
DIMENSIONS,
DISPLAY_SHAPE,
EXTERNAL,
EXTERNAL_DESCRIPTION,
EXTERNAL_FIXED_TYPE_NAME,
EXTERNAL_SCENE_ID,
EXTERNAL_SERIALIZATION,
FORBIDDEN_PROPERTIES,
FROM_ROOT,
ID,
ITEMS,
MAGIC_STRING,
MATERIAL_NAME,
SIMPLE_PROPERTY_TYPES_AS_STRS,
NAME,
NODE_TREE,
PROP_TYPE_BOOLEAN,
PROP_TYPE_COLLECTION,
PROP_TYPE_ENUM,
PROP_TYPE_FLOAT,
PROP_TYPE_INT,
PROP_TYPE_POINTER,
RNA_TYPE,
SCENES,
SERIALIZER,
SIMPLE_DATA_TYPE,
SIMPLE_PROP_TYPE,
SIMPLE_PROPERTY_TYPES_AS_STRS,
TREE_CLIPPER_VERSION,
TREES,
FromRoot,
most_specific_type_handled,
no_clobber,
EXTERNAL_SERIALIZATION,
PROP_TYPE_BOOLEAN,
PROP_TYPE_INT,
PROP_TYPE_FLOAT,
PROP_TYPE_ENUM,
PROP_TYPE_POINTER,
PROP_TYPE_COLLECTION,
NAME,
ITEMS,
BL_RNA,
FROM_ROOT,
RNA_TYPE,
EXTERNAL,
EXTERNAL_FIXED_TYPE_NAME,
NODE_TREE,
SCENES,
EXTERNAL_SCENE_ID,
)

from .id_data_getter import canonical_reference
from .scene_info import export_scene_info

Expand Down Expand Up @@ -112,7 +112,7 @@ def export_all_simple_writable_properties(
self,
*,
obj: bpy.types.bpy_struct,
assumed_type: Type[bpy.types.bpy_struct],
assumed_type: type[bpy.types.bpy_struct],
from_root: FromRoot,
) -> dict[str, SIMPLE_DATA_TYPE]:
data = {}
Expand Down Expand Up @@ -222,7 +222,7 @@ def _export_property_simple(
)

# https://github.com/Algebraic-UG/tree_clipper/issues/96
def clamp_and_report(value: int | float) -> int | float:
def clamp_and_report(value: float) -> int | float:
# https://github.com/Algebraic-UG/tree_clipper/issues/162
if isinstance(obj, bpy.types.NodeTreeInterfaceSocket):
return value
Expand Down Expand Up @@ -310,9 +310,7 @@ def _export_property_collection(
items = [
self._export_obj(
obj=element,
from_root=from_root.add(
f"[{i}] ({getattr(attribute[i], NAME, 'unnamed')})"
),
from_root=from_root.add(f"[{i}] ({getattr(element, NAME, 'unnamed')})"),
)
for i, element in enumerate(attribute)
]
Expand Down Expand Up @@ -712,7 +710,7 @@ def get_external(self) -> dict[int, External]:

def set_external(
self,
ids_and_descriptions: Iterator[Tuple[int, str]],
ids_and_descriptions: Iterator[tuple[int, str]],
) -> None:
assert not self.unexported_trees
for external_id, description in ids_and_descriptions:
Expand Down
4 changes: 2 additions & 2 deletions packages/tree_clipper/src/tree_clipper/id_data_getter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bpy
from collections.abc import Callable

from typing import Callable
import bpy

_ID_TYPE_TO_DATA_BLOCK: dict[str, Callable[[], bpy.types.bpy_prop_collection]] = {
"ACTION": lambda: bpy.data.actions,
Expand Down
62 changes: 29 additions & 33 deletions packages/tree_clipper/src/tree_clipper/import_nodes.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
import bpy

from operator import xor

import base64
import gzip
import json
from collections.abc import Iterator
from operator import xor
from pathlib import Path
from types import NoneType
from typing import Any

from typing import Any, Type, Tuple, Iterator

from pathlib import Path
import bpy

from .common import (
BL_IDNAME,
BL_RNA,
BLENDER_VERSION,
CURRENT_TREE_CLIPPER_VERSION,
DATA,
DEFAULT_VALUE,
DESERIALIZER,
EXTERNAL,
EXTERNAL_DESCRIPTION,
EXTERNAL_SCENE_ID,
EXTERNAL_SERIALIZATION,
FORBIDDEN_PROPERTIES,
GETTER,
ID,
ITEMS,
MAGIC_STRING,
MATERIAL_NAME,
SIMPLE_PROP_TYPE,
SIMPLE_PROPERTY_TYPES_AS_STRS,
BLENDER_VERSION,
NAME,
PROP_TYPE_COLLECTION,
PROP_TYPE_ENUM,
PROP_TYPE_POINTER,
RNA_TYPE,
SCENES,
SIMPLE_DATA_TYPE,
SIMPLE_PROP_TYPE,
SIMPLE_PROP_TYPE_TUPLE,
SIMPLE_PROPERTY_TYPES_AS_STRS,
TREE_CLIPPER_VERSION,
TREES,
FromRoot,
most_specific_type_handled,
MAGIC_STRING,
EXTERNAL_SERIALIZATION,
PROP_TYPE_ENUM,
DEFAULT_VALUE,
PROP_TYPE_POINTER,
PROP_TYPE_COLLECTION,
ITEMS,
NAME,
BL_RNA,
RNA_TYPE,
BL_IDNAME,
EXTERNAL_DESCRIPTION,
EXTERNAL,
SCENES,
no_clobber,
EXTERNAL_SCENE_ID,
)

from .id_data_getter import make_id_data_getter
from .scene_info import verify_scene, SceneValidationError
from .scene_info import SceneValidationError, verify_scene


class ImportReport:
Expand Down Expand Up @@ -113,7 +110,7 @@ def import_all_simple_writable_properties(
*,
getter: GETTER,
serialization: dict[str, Any],
assumed_type: Type[bpy.types.bpy_struct],
assumed_type: type[bpy.types.bpy_struct],
forbidden: list[str],
from_root: FromRoot,
) -> None:
Expand Down Expand Up @@ -186,9 +183,8 @@ def _import_property_simple(
return

if (
(
isinstance(getter(), bpy.types.NodeSocket)
or isinstance(getter(), bpy.types.NodeTreeInterfaceSocket)
isinstance(
getter(), (bpy.types.NodeSocket, bpy.types.NodeTreeInterfaceSocket)
)
and prop.type == PROP_TYPE_ENUM
and identifier == DEFAULT_VALUE
Expand Down Expand Up @@ -282,7 +278,7 @@ def make_getter(i: int) -> GETTER:
name = item[DATA].get(NAME, "unnamed")
self._import_obj(
getter=make_getter(i),
serialization=serialized_items[i],
serialization=item,
from_root=from_root.add(f"[{i}] ({name})"),
)

Expand Down Expand Up @@ -619,7 +615,7 @@ def get_external(self) -> dict[str, EXTERNAL_SERIALIZATION]:

def set_external(
self,
ids_and_references: Iterator[Tuple[int, bpy.types.ID | None]],
ids_and_references: Iterator[tuple[int, bpy.types.ID | None]],
) -> None:
for external_id, external_item in ids_and_references:
scene_id = self.get_external()[str(external_id)][EXTERNAL_SCENE_ID]
Expand Down
6 changes: 3 additions & 3 deletions packages/tree_clipper/src/tree_clipper/scene_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# parameters to reconstruct the tree around the nodes that reference it and it's view layers.
# All of this is very ad-hoc and might well break in the future.

import bpy

from typing import Any

from .common import no_clobber, PROP_TYPE_BOOLEAN, NAME
import bpy

from .common import NAME, PROP_TYPE_BOOLEAN, no_clobber

# to help prevent typos
VIEW_LAYERS = "view_layers"
Expand Down
Loading
Loading