Skip to content

Commit

Permalink
clean up type casts slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwonko committed May 8, 2024
1 parent 92b3d0f commit da73769
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions JAG2GLM.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from . import JAMaterialmanager
from . import MrwProfiler
from . import JAG2Panels
from .casts import optional_cast, downcast, bpy_generic_cast, union_cast, unpack_cast, matrix_getter_cast, vector_getter_cast, vector_overload_cast
from .casts import optional_cast, downcast, bpy_generic_cast, unpack_cast, matrix_getter_cast, vector_getter_cast, vector_overload_cast
from .error_types import ErrorMessage, NoError, ensureListIsGapless

import bpy
Expand Down Expand Up @@ -577,7 +577,7 @@ def loadFromBlender(self, object: bpy.types.Object, surfaceData: MdxmSurfaceData
# This is not a tag, do normal things
else:

uv_layer = union_cast(bpy.types.UVLoopLayers, mesh.uv_layers).active
uv_layer = mesh.uv_layers.active
if not uv_layer or not (uv_layer_data := uv_layer.data):
return False, ErrorMessage("No UV coordinates found!")

Expand Down Expand Up @@ -1003,7 +1003,7 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
# retrieve skeleton
if not "skeleton_root" in bpy.data.objects:
return False, ErrorMessage("No skeleton_root Object found!")
obj = cast(bpy.types.Object, bpy.data.objects["skeleton_root"])
obj = bpy_generic_cast(bpy.types.Object, bpy.data.objects["skeleton_root"])
skeleton_object = obj
if obj.type != 'ARMATURE':
return False, ErrorMessage("skeleton_root is no Armature!")
Expand Down
10 changes: 5 additions & 5 deletions casts.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def optional_list_cast(t: Type[List[T]], v: List[Optional[T]]) -> List[T]:
return cast(t, v)


def union_cast(t: Type[T], v: Union[T, U]) -> T:
"""A cast from a union to one of its elements"""
return cast(t, v)

# A cast used to turn A | B into A or B, for properties that accept unions in the setter but return a fixed type in the setter.
# Blender uses this extensively to allow assigning sequences in place of vectors and matrices,
# and mypy doesn't currently support differing types in setters (https://github.com/python/mypy/issues/3004)
# and the bpy bindings don't currently define separate setter/getter (https://github.com/nutti/fake-bpy-module/issues/158)
getter_cast = cast
getter_cast = union_cast


def matrix_getter_cast(x: Any) -> mathutils.Matrix:
Expand All @@ -48,10 +52,6 @@ def vector_getter_cast(x: Any) -> mathutils.Vector:
# Should happen close to a check that ensures this is valid.
downcast = cast

def union_cast(t: Type[T], v: Union[T, U]) -> T:
"""A cast from a union to one of its elements"""
return cast(t, v)

# A cast to resolve polymorphic functions being annotated with union return types instead of proper overloads.
overload_cast = union_cast

Expand Down

0 comments on commit da73769

Please sign in to comment.