Skip to content

Commit

Permalink
chore: move Json def to convert module
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed Jun 16, 2024
1 parent bee63be commit 9bd4c9c
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 38 deletions.
32 changes: 19 additions & 13 deletions src/io_scene_vrm/common/convert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from collections.abc import Iterator, Mapping
from sys import float_info
from typing import Optional
from typing import Optional, Union

Json = Union[
None,
bool,
int,
float,
str,
list["Json"],
dict[str, "Json"],
]


def iterator_or_none(v: object) -> Optional[Iterator[object]]:
Expand All @@ -16,7 +26,7 @@ def iterator_or_none(v: object) -> Optional[Iterator[object]]:


def vrm_json_vector3_to_tuple(
value: object,
value: Json,
) -> Optional[tuple[float, float, float]]:
if not isinstance(value, Mapping):
return None
Expand All @@ -32,27 +42,23 @@ def vrm_json_vector3_to_tuple(
return (float(x), float(y), float(z))


def vrm_json_curve_to_list(curve: object) -> Optional[list[float]]:
iterator = iterator_or_none(curve)
if iterator is None:
def vrm_json_curve_to_list(curve: Json) -> Optional[list[float]]:
if not isinstance(curve, list):
return None
values = [float(v) if isinstance(v, (int, float)) else 0 for v in iterator]
values = [float(v) if isinstance(v, (int, float)) else 0 for v in curve]
while len(values) < 8:
values.append(0)
while len(values) > 8:
values.pop()
return values


def vrm_json_array_to_float_vector(json: object, defaults: list[float]) -> list[float]:
if isinstance(json, str):
return defaults

iterator = iterator_or_none(json)
if iterator is None:
def vrm_json_array_to_float_vector(
input_values: Json, defaults: list[float]
) -> list[float]:
if not isinstance(input_values, list):
return defaults

input_values = list(iterator)
output_values: list[float] = []
for index, default in enumerate(defaults):
if index >= len(input_values):
Expand Down
11 changes: 1 addition & 10 deletions src/io_scene_vrm/common/deep.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@
from typing import Union

from . import convert
from .convert import Json
from .logging import get_logger

logger = get_logger(__name__)

Json = Union[
None,
bool,
int,
float,
str,
list["Json"],
dict[str, "Json"],
]


def make_json(v: object) -> Json:
if v is None:
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/common/gltf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from typing import Final, Optional, Union

from .binary_reader import BinaryReader
from .deep import Json, make_json
from .convert import Json
from .deep import make_json

# https://www.khronos.org/opengl/wiki/Small_Float_Formats#Numeric_limits_and_precision
FLOAT_POSITIVE_MAX: Final = 3.4028237e38
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/editor/vrm0/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from bpy.types import ID, Armature, Context, Mesh, Object, Text

from ...common import convert
from ...common.deep import Json, make_json
from ...common.convert import Json
from ...common.deep import make_json
from ...common.vrm0.human_bone import HumanBoneSpecifications
from ..property_group import BonePropertyGroup
from .property_group import (
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/exporter/abstract_base_vrm_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from mathutils import Quaternion

from ..common import shader
from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json
from ..common.workspace import save_workspace
from ..editor.vrm0.property_group import Vrm0HumanoidPropertyGroup
from ..editor.vrm1.property_group import Vrm1HumanoidPropertyGroup
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/exporter/glb_bin_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from typing import Optional, Union

from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json


class GlbBinCollection:
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/exporter/vrm0_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from mathutils import Matrix, Vector

from ..common import convert, deep, shader
from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json
from ..common.gl import (
GL_FLOAT,
GL_LINEAR,
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/exporter/vrm1_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

from ..common import convert, deep, shader
from ..common.char import INTERNAL_NAME_PREFIX
from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json
from ..common.gltf import pack_glb, parse_glb
from ..common.logging import get_logger
from ..common.version import addon_version
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/exporter/vrm_animation_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from mathutils import Euler, Matrix, Quaternion, Vector

from ..common import version
from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json
from ..common.gl import GL_FLOAT
from ..common.gltf import pack_glb
from ..common.logging import get_logger
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/importer/abstract_base_vrm_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
from mathutils import Matrix

from ..common import deep, shader
from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json
from ..common.fs import (
create_unique_indexed_directory_path,
create_unique_indexed_file_path,
Expand Down
2 changes: 1 addition & 1 deletion src/io_scene_vrm/importer/license_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bpy.app.translations import pgettext

from ..common import deep
from ..common.deep import Json
from ..common.convert import Json


class LicenseConfirmationRequiredProp:
Expand Down
2 changes: 1 addition & 1 deletion src/io_scene_vrm/importer/vrm0_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from mathutils import Matrix, Vector

from ..common import convert, deep, shader
from ..common.deep import Json
from ..common.convert import Json
from ..common.logging import get_logger
from ..common.version import addon_version
from ..common.vrm0.human_bone import HumanBoneName, HumanBoneSpecifications
Expand Down
2 changes: 1 addition & 1 deletion src/io_scene_vrm/importer/vrm1_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mathutils import Matrix, Vector

from ..common import convert, deep, shader
from ..common.deep import Json
from ..common.convert import Json
from ..common.logging import get_logger
from ..common.version import addon_version
from ..common.vrm1 import human_bone as vrm1_human_bone
Expand Down
2 changes: 1 addition & 1 deletion src/io_scene_vrm/importer/vrm_animation_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from mathutils import Matrix, Quaternion, Vector

from ..common import convert
from ..common.convert import Json
from ..common.debug import dump
from ..common.deep import Json
from ..common.gl import (
GL_BYTE,
GL_FLOAT,
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/importer/vrm_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from sys import float_info

from ..common import deep, gltf
from ..common.deep import Json, make_json
from ..common.convert import Json
from ..common.deep import make_json
from .vrm_parser import decode_bin


Expand Down
2 changes: 1 addition & 1 deletion src/io_scene_vrm/importer/vrm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ..common import deep
from ..common.binary_reader import BinaryReader
from ..common.deep import Json
from ..common.convert import Json
from ..common.gltf import parse_glb
from ..common.logging import get_logger
from .license_validation import validate_license
Expand Down
2 changes: 1 addition & 1 deletion tools/vrm_clean_extensions_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

from io_scene_vrm.common import deep, gltf
from io_scene_vrm.common.deep import Json
from io_scene_vrm.common.convert import Json


def clean(path: Path) -> None:
Expand Down

0 comments on commit 9bd4c9c

Please sign in to comment.