Skip to content

Commit 74de976

Browse files
committed
Simplifying and correcting some type hinting
1 parent 6b84562 commit 74de976

31 files changed

+235
-234
lines changed

bdk_addon/bdk/operators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import OrderedDict
22
from pathlib import Path
3-
from typing import Set, cast
3+
from typing import cast
44

55
import bpy
66
from bpy.types import Operator, Context, Node, Event, Armature, Mesh
@@ -377,7 +377,7 @@ def poll(cls, context: 'Context'):
377377
return False
378378
return True
379379

380-
def execute(self, context: Context) -> Set[str]:
380+
def execute(self, context: Context) -> set[str]:
381381
library_path: Path
382382
assets = context.selected_assets
383383

bdk_addon/bdk/repository/kernel.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@
1515

1616
from .properties import BDK_PG_repository, BDK_PG_repository_package
1717
from pathlib import Path
18-
from typing import Optional, List, Dict
1918

2019
from ...data import UReference
2120
from ...helpers import get_addon_preferences
2221
from ...io.config import ConfigParserMultiOpt
2322
import json
2423

2524

26-
def is_game_directory_and_mod_valid(game_directory: Path, mod: Optional[str]) -> bool:
25+
def is_game_directory_and_mod_valid(game_directory: Path, mod: str | None) -> bool:
2726
try:
2827
read_repository_package_patterns(game_directory, mod)
2928
return True
3029
except FileNotFoundError:
3130
return False
3231

3332

34-
def read_repository_package_patterns(game_directory: Path, mod: Optional[str]) -> List[Path]:
33+
def read_repository_package_patterns(game_directory: Path, mod: str | None) -> list[Path]:
3534
# Determine the asset paths.
3635
game_directory = Path(game_directory)
3736
game_system_directory = game_directory / 'System'
@@ -93,12 +92,12 @@ def parse_config_package_patterns(path: Path):
9392
class Manifest:
9493
class Package:
9594
def __init__(self):
96-
self.exported_time: Optional[datetime] = None
97-
self.build_time: Optional[datetime] = None
95+
self.exported_time: datetime | None = None
96+
self.build_time: datetime | None = None
9897

9998
def __init__(self, path: str):
10099
self.path = path
101-
self.packages: Dict[str, Manifest.Package] = dict()
100+
self.packages: dict[str, Manifest.Package] = dict()
102101

103102
def has_package(self, package_path: str) -> bool:
104103
return package_path in self.packages

bdk_addon/bdk/ui.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class BDK_MT_object_add_menu(Menu):
1414
bl_label = 'BDK'
1515

1616
def draw(self, context):
17+
if self.layout is None:
18+
return
1719
self.layout.operator(terrain_operators.BDK_OT_terrain_info_add.bl_idname, text='Terrain Info', icon='GRID')
1820
self.layout.operator_menu_enum(terrain_doodad_operators.BDK_OT_terrain_doodad_add.bl_idname, 'object_type',
1921
text='Terrain Doodad', icon='CURVE_DATA')
@@ -39,6 +41,8 @@ def poll(cls, context):
3941
def draw(self, context):
4042
node_tree = context.space_data.edit_tree
4143
layout = self.layout
44+
if layout is None:
45+
return
4246
layout.use_property_split = True
4347
layout.use_property_decorate = False
4448
layout.prop(node_tree.bdk, 'build_hash', text='Build Hash', icon='KEYINGSET', emboss=False)

bdk_addon/bin/blend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def build(args):
3838
# are in the .blend file before it evaluates any static meshes.
3939
material_files = []
4040
static_mesh_files = []
41-
new_ids: List[bpy.types.ID] = []
41+
new_ids: list[bpy.types.ID] = []
4242

4343
for file in glob.glob('**/*.props.txt', root_dir=args.input_directory):
4444
# The class type of the object is the directory name of the parent folder.

bdk_addon/bsp/builder.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from bmesh.types import BMFace
1717
from bpy.types import Object, Mesh, NodeTree, bpy_struct
1818
from math import isnan
19-
from typing import Optional, Dict, Tuple, List, cast
19+
from typing import cast
2020
import mathutils
2121
import numpy as np
2222

@@ -92,8 +92,8 @@ class BrushMappingErrorType(Enum):
9292

9393

9494
class BrushMappingError:
95-
def __init__(self, brush_index: int, error_type: BrushMappingErrorType, level_polygon_index: Optional[int] = None,
96-
brush_polygon_index: Optional[int] = None) -> None:
95+
def __init__(self, brush_index: int, error_type: BrushMappingErrorType, level_polygon_index: int | None = None,
96+
brush_polygon_index: int | None = None) -> None:
9797
self.brush_index = brush_index
9898
self.error_type = error_type
9999
self.level_polygon_index = level_polygon_index
@@ -111,7 +111,7 @@ def __init__(self):
111111
self.face_count = 0
112112
self.brush_count = 0
113113
self.missing_brush_count = 0
114-
self.errors: List[BrushMappingError] = []
114+
self.errors: list[BrushMappingError] = []
115115
self.duration = 0.0
116116

117117

@@ -223,7 +223,7 @@ def apply_level_to_brush_mapping(level_object: Object) -> BrushMappingResult:
223223
return result
224224

225225

226-
def build_level_polygon_to_brush_polygon_mapping(level_object: Object) -> Dict[int, Dict[int, int]]:
226+
def build_level_polygon_to_brush_polygon_mapping(level_object: Object) -> dict[int, dict[int, int]]:
227227
"""
228228
Build a mapping from the level object's polygons to the brush object's polygons.
229229
"""
@@ -238,7 +238,7 @@ def build_level_polygon_to_brush_polygon_mapping(level_object: Object) -> Dict[i
238238
if BRUSH_POLYGON_INDEX_ATTRIBUTE_NAME in mesh_data.attributes:
239239
mesh_data.attributes[BRUSH_POLYGON_INDEX_ATTRIBUTE_NAME].data.foreach_get('value', brush_polygon_indices)
240240

241-
mapping: Dict[int, Dict[int, int]] = {}
241+
mapping: dict[int, dict[int, int]] = {}
242242

243243
for polygon_index, (brush_index, brush_polygon_index) in enumerate(zip(brush_indices, brush_polygon_indices)):
244244
if brush_index not in mapping:
@@ -501,7 +501,7 @@ def create_bsp_brush_polygon(
501501
texture_height: int,
502502
uv_layer,
503503
face: BMFace,
504-
transform_matrix: Matrix) -> Tuple[Vector, Quaternion, Vector]:
504+
transform_matrix: Matrix) -> tuple[Vector, Quaternion, Vector]:
505505
texture_coordinates = [loop[uv_layer].uv for loop in face.loops[0:3]]
506506

507507
u_tiling = 1

bdk_addon/bsp/operators.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from bpy.types import Operator, Object, Context, Depsgraph, Mesh, Material, Event
1414
from collections import OrderedDict
1515
from enum import Enum
16-
from typing import cast, List, Optional, Tuple, Set
16+
from typing import cast
1717
from bdk_py import Poly, Brush, csg_rebuild, BspBuildOptions
1818
import bmesh
1919
import bpy
@@ -142,13 +142,13 @@ def draw(self, context):
142142
flow.prop(self, 'poly_flags')
143143

144144
def execute(self, context):
145-
def set_flags(bsp_brush: BDK_PG_bsp_brush, flags: Set[str]):
145+
def set_flags(bsp_brush: BDK_PG_bsp_brush, flags: set[str]):
146146
bsp_brush.poly_flags = flags
147147

148-
def add_flags(bsp_brush: BDK_PG_bsp_brush, flags: Set[str]):
148+
def add_flags(bsp_brush: BDK_PG_bsp_brush, flags: set[str]):
149149
bsp_brush.poly_flags |= flags
150150

151-
def remove_flags(bsp_brush: BDK_PG_bsp_brush, flags: Set[str]):
151+
def remove_flags(bsp_brush: BDK_PG_bsp_brush, flags: set[str]):
152152
bsp_brush.poly_flags -= flags
153153

154154
def get_operation_function(operation: str):
@@ -379,7 +379,7 @@ class BspBrushError(Enum):
379379
TWISTED_FACE = 3
380380

381381

382-
def get_bsp_brush_errors(obj: Object, depsgraph: Depsgraph) -> List[Tuple[BspBrushError, int]]:
382+
def get_bsp_brush_errors(obj: Object, depsgraph: Depsgraph) -> list[tuple[BspBrushError, int]]:
383383
"""
384384
Check the given object for errors and return a set of all the errors that were found.
385385
"""
@@ -746,7 +746,7 @@ def execute(self, context):
746746
for error in result.errors:
747747
self.report({'WARNING'}, str(error))
748748

749-
def brush_object_filter(obj: Object, instance_objects: List[Object]):
749+
def brush_object_filter(obj: Object, instance_objects: list[Object]):
750750
if not obj.bdk.type == 'BSP_BRUSH':
751751
return False
752752
if self.should_do_only_visible:
@@ -762,9 +762,9 @@ def brush_object_filter(obj: Object, instance_objects: List[Object]):
762762

763763
# This is a list of the materials used for the brushes. It is populated as we iterate over the brush objects.
764764
# We then use this at the end to create the materials for the level object.
765-
materials: List[Optional[Material]] = []
765+
materials: list[Material | None] = []
766766

767-
def _get_or_add_material(material: Optional[Material]) -> int:
767+
def _get_or_add_material(material: Material | None) -> int:
768768
try:
769769
return materials.index(material)
770770
except ValueError:
@@ -790,7 +790,7 @@ def _get_or_add_material(material: Optional[Material]) -> int:
790790
# level object and exclude them from BSP surface tool operations.
791791
instanced_brush_indices = []
792792

793-
brushes: List[Brush] = []
793+
brushes: list[Brush] = []
794794
for brush_index, (brush_object, asset_instances, matrix_world) in enumerate(brush_objects):
795795

796796
if asset_instances:

bdk_addon/bsp/properties.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
BoolProperty, StringProperty
44
from bpy.types import PropertyGroup, Object, Context
55
from enum import Enum
6-
from typing import Set
76

87

98
class BrushColors(Enum):
@@ -15,7 +14,7 @@ class BrushColors(Enum):
1514
SemiSolid = (0.875, 0.6, 0.6, 1.0)
1615

1716

18-
def get_brush_color(csg_operation: str, poly_flags: Set[str]) -> tuple[float, float, float, float]:
17+
def get_brush_color(csg_operation: str, poly_flags: set[str]) -> tuple[float, float, float, float]:
1918
match csg_operation:
2019
case 'ADD':
2120
if 'PORTAL' in poly_flags:
@@ -67,15 +66,15 @@ def get_brush_color(csg_operation: str, poly_flags: Set[str]) -> tuple[float, fl
6766
__poly_flag_values_to_keys__ = {value: key for key, _, _, _, value in poly_flags_items}
6867

6968

70-
def get_poly_flags_value_from_keys(keys: Set[str]) -> int:
69+
def get_poly_flags_value_from_keys(keys: set[str]) -> int:
7170
poly_flags: int = 0
7271
for key in keys:
7372
poly_flags |= __poly_flag_keys_to_values__[key]
7473
return poly_flags
7574

7675

77-
def get_poly_flags_keys_from_value(values: int) -> Set[str]:
78-
poly_flags: Set[str] = set()
76+
def get_poly_flags_keys_from_value(values: int) -> set[str]:
77+
poly_flags: set[str] = set()
7978
for value, key in __poly_flag_values_to_keys__.items():
8079
if values & value:
8180
poly_flags.add(key)

bdk_addon/bsp/tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable, List
1+
from typing import Callable
22

33
from bpy.types import NodeTree, NodeSocket
44

@@ -1481,7 +1481,7 @@ def build_function(node_tree: NodeTree):
14811481
return ensure_geometry_node_tree('BDK Screen Space Mouse Position', items, build_function)
14821482

14831483

1484-
ensure_functions: List[Callable[[], NodeTree]] = [
1484+
ensure_functions: list[Callable[[], NodeTree]] = [
14851485
ensure_bdk_axes_to_matrix_node_tree,
14861486
ensure_bdk_duplicate_active_face_node_tree,
14871487
ensure_bdk_move_edge_to_origin_node_tree,

bdk_addon/catalog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# https://github.com/strike-digital/asset_bridge/blob/main/asset_bridge/helpers/catalog.py
22

33
from pathlib import Path
4-
from typing import Dict, Optional
4+
from typing import Optional
55
from uuid import uuid4
66

77
"""A module for working with blender_assets.cats.txt files, and the asset catalogs that they contain"""
@@ -100,7 +100,7 @@ def update_catalog_from_file(self):
100100
"""Read and set the catalogs from the file"""
101101
self.catalogs = self.get_catalogs_from_file()
102102

103-
def get_catalogs_from_file(self) -> Dict[str, AssetCatalog]:
103+
def get_catalogs_from_file(self) -> dict[str, AssetCatalog]:
104104
"""Read the catalogs from the file"""
105105
catalogs = {}
106106
with open(self.catalog_file, "r") as f:

bdk_addon/data.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Self
22
from pathlib import Path
33
import re
44
from .units import unreal_to_radians
@@ -13,14 +13,14 @@ def __init__(self, r: int, g: int, b: int, a: int):
1313

1414

1515
class UReference:
16-
def __init__(self, package_name: str, object_name: str, type_name: Optional[str], group_name: Optional[str] = None):
16+
def __init__(self, package_name: str, object_name: str, type_name: str | None, group_name: str | None = None):
1717
self.type_name = type_name
1818
self.package_name = package_name
1919
self.object_name = object_name
2020
self.group_name = group_name
2121

22-
@staticmethod
23-
def from_string(string: str) -> Optional['UReference']:
22+
@classmethod
23+
def from_string(cls,string: str) -> Self | None:
2424
if string == 'None' or string == '':
2525
return None
2626

@@ -44,15 +44,15 @@ def from_string(string: str) -> Optional['UReference']:
4444
package_name = values[0]
4545
object_name = values[-1]
4646

47-
return UReference(package_name, object_name, type_name=type_name, group_name=group_name)
47+
return cls(package_name, object_name, type_name=type_name, group_name=group_name)
4848

49-
@staticmethod
50-
def from_path(path: Path):
49+
@classmethod
50+
def from_path(cls, path: Path) -> Self:
5151
parts = path.parts[-3:]
5252
package_name = parts[0]
5353
type_name = parts[1]
5454
object_name = parts[2][0:parts[2].index('.')]
55-
return UReference(package_name, object_name, type_name)
55+
return cls(package_name, object_name, type_name)
5656

5757
def __str__(self):
5858
string = f"{self.type_name}'{self.package_name}"

0 commit comments

Comments
 (0)