Skip to content

Commit ae109bb

Browse files
committed
Changed if/elif to match blocks where appropriate
1 parent 6b1de5e commit ae109bb

File tree

8 files changed

+316
-292
lines changed

8 files changed

+316
-292
lines changed

bdk_addon/bdk/repository/operators.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,14 +832,15 @@ def execute(self, context):
832832
addon_prefs = get_addon_preferences(context)
833833
repository = addon_prefs.repositories[addon_prefs.repositories_index]
834834

835-
if self.direction == 'UP':
836-
if repository.rules_index > 0:
837-
repository.rules.move(repository.rules_index, repository.rules_index - 1)
838-
repository.rules_index -= 1
839-
elif self.direction == 'DOWN':
840-
if repository.rules_index < len(repository.rules) - 1:
841-
repository.rules.move(repository.rules_index, repository.rules_index + 1)
842-
repository.rules_index += 1
835+
match self.direction:
836+
case 'UP':
837+
if repository.rules_index > 0:
838+
repository.rules.move(repository.rules_index, repository.rules_index - 1)
839+
repository.rules_index -= 1
840+
case 'DOWN':
841+
if repository.rules_index < len(repository.rules) - 1:
842+
repository.rules.move(repository.rules_index, repository.rules_index + 1)
843+
repository.rules_index += 1
843844

844845
repository_metadata_write(repository)
845846
repository_runtime_packages_update_rule_exclusions(repository)

bdk_addon/bsp/operators.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,13 @@ def execute(self, context):
487487
self.report({'ERROR'}, f'Found {len(object_errors)} brush(es) with errors')
488488
for obj, errors in object_errors.items():
489489
for (error, index) in errors:
490-
print(error, index)
491-
if error == BspBrushError.NOT_MANIFOLD:
492-
self.report({'ERROR'}, f'{obj.name}: Edge {index} is not manifold')
493-
elif error == BspBrushError.NOT_CONVEX:
494-
self.report({'ERROR'}, f'{obj.name}: Edge {index} is not convex')
495-
elif error == BspBrushError.TWISTED_FACE:
496-
self.report({'ERROR'}, f'{obj.name}: Face {index} is twisted')
490+
match error:
491+
case BspBrushError.NOT_MANIFOLD:
492+
self.report({'ERROR'}, f'{obj.name}: Edge {index} is not manifold')
493+
case BspBrushError.NOT_CONVEX:
494+
self.report({'ERROR'}, f'{obj.name}: Edge {index} is not convex')
495+
case BspBrushError.TWISTED_FACE:
496+
self.report({'ERROR'}, f'{obj.name}: Face {index} is twisted')
497497
if self.deselect_ok:
498498
# Go through all selected objects and deselect those that don't have errors.
499499
for obj in context.selected_objects:

bdk_addon/material/importer.py

Lines changed: 192 additions & 182 deletions
Large diffs are not rendered by default.

bdk_addon/node_helpers.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,30 @@ def add_noise_type_switch_nodes(
151151
index_switch_node.index_switch_items.new()
152152

153153
for index, noise_type in enumerate(noise_types):
154-
if noise_type == 'PERLIN':
155-
noise_node = node_tree.nodes.new(type='ShaderNodeTexNoise')
156-
noise_node.noise_dimensions = '2D'
157-
# NOTE: There is a bug with using normal fDM noise where values end up as NaN. Use multi-fractal instead.
158-
noise_node.noise_type = 'MULTIFRACTAL'
159-
noise_node.inputs['Scale'].default_value = 0.5
160-
noise_node.inputs['Detail'].default_value = 16
161-
noise_node.inputs['Distortion'].default_value = 0.5
162-
163-
if noise_distortion_socket:
164-
node_tree.links.new(noise_distortion_socket, noise_node.inputs['Distortion'])
165-
166-
if noise_roughness_socket:
167-
node_tree.links.new(noise_roughness_socket, noise_node.inputs['Roughness'])
168-
169-
node_tree.links.new(vector_socket, noise_node.inputs['Vector'])
170-
noise_value_socket = noise_node.outputs['Fac']
171-
elif noise_type == 'WHITE':
172-
noise_node = node_tree.nodes.new(type='ShaderNodeTexWhiteNoise')
173-
noise_node.noise_dimensions = '2D'
174-
noise_value_socket = noise_node.outputs['Value']
175-
else:
176-
raise ValueError(f'Unknown noise type {noise_type}')
154+
match noise_type:
155+
case 'PERLIN':
156+
noise_node = node_tree.nodes.new(type='ShaderNodeTexNoise')
157+
noise_node.noise_dimensions = '2D'
158+
# NOTE: There is a bug with using normal fDM noise where values end up as NaN. Use multi-fractal instead.
159+
noise_node.noise_type = 'MULTIFRACTAL'
160+
noise_node.inputs['Scale'].default_value = 0.5
161+
noise_node.inputs['Detail'].default_value = 16
162+
noise_node.inputs['Distortion'].default_value = 0.5
163+
164+
if noise_distortion_socket:
165+
node_tree.links.new(noise_distortion_socket, noise_node.inputs['Distortion'])
166+
167+
if noise_roughness_socket:
168+
node_tree.links.new(noise_roughness_socket, noise_node.inputs['Roughness'])
169+
170+
node_tree.links.new(vector_socket, noise_node.inputs['Vector'])
171+
noise_value_socket = noise_node.outputs['Fac']
172+
case 'WHITE':
173+
noise_node = node_tree.nodes.new(type='ShaderNodeTexWhiteNoise')
174+
noise_node.noise_dimensions = '2D'
175+
noise_value_socket = noise_node.outputs['Value']
176+
case _:
177+
raise ValueError(f'Unknown noise type {noise_type}')
177178

178179
node_tree.links.new(noise_value_socket, index_switch_node.inputs[f'{index}'])
179180

@@ -298,10 +299,11 @@ def ensure_input_and_output_nodes(node_tree: NodeTree) -> Tuple[Node, Node]:
298299
input_node = None
299300
output_node = None
300301
for node in node_tree.nodes:
301-
if node.bl_idname == 'NodeGroupInput':
302-
input_node = node
303-
elif node.bl_idname == 'NodeGroupOutput':
304-
output_node = node
302+
match node.bl_idname:
303+
case 'NodeGroupInput':
304+
input_node = node
305+
case 'NodeGroupOutput':
306+
output_node = node
305307

306308
input_node = node_tree.nodes.new(type='NodeGroupInput') if input_node is None else input_node
307309
output_node = node_tree.nodes.new(type='NodeGroupOutput') if output_node is None else output_node

bdk_addon/terrain/doodad/builder.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,14 @@ def create_terrain_doodad_object(context: Context, terrain_info_object: Object,
414414

415415
bpy_object = bpy.data.objects.new(name='Doodad', object_data=object_data)
416416

417-
if object_type == 'EMPTY':
418-
bpy_object.empty_display_type = 'SPHERE'
419-
bpy_object.empty_display_size = meters_to_unreal(1.0)
420-
# Set the delta transform to the terrain info object's rotation.
421-
bpy_object.delta_rotation_euler = (0, 0, 0)
422-
elif object_type == 'MESH':
423-
bpy_object.display_type = 'WIRE'
417+
match object_type:
418+
case 'EMPTY':
419+
bpy_object.empty_display_type = 'SPHERE'
420+
bpy_object.empty_display_size = meters_to_unreal(1.0)
421+
# Set the delta transform to the terrain info object's rotation.
422+
bpy_object.delta_rotation_euler = (0, 0, 0)
423+
case 'MESH':
424+
bpy_object.display_type = 'WIRE'
424425

425426
# Set the location of the curve object to the 3D cursor.
426427
bpy_object.location = context.scene.cursor.location
@@ -692,14 +693,15 @@ def add_doodad_layer_driver(
692693
var.type = 'SINGLE_PROP'
693694
var.targets[0].id = layer.terrain_doodad_object
694695

695-
if layer_type == 'SCULPT':
696-
data_path = f"bdk.terrain_doodad.sculpt_layers[{layer.index}].{data_path}"
697-
elif layer_type == 'PAINT':
698-
data_path = f"bdk.terrain_doodad.paint_layers[{layer.index}].{data_path}"
699-
elif layer_type == 'DECO':
700-
data_path = f"bdk.terrain_doodad.deco_layers[{layer.index}].{data_path}"
701-
else:
702-
raise Exception(f"Unknown layer type: {layer_type}")
696+
match layer_type:
697+
case 'SCULPT':
698+
data_path = f"bdk.terrain_doodad.sculpt_layers[{layer.index}].{data_path}"
699+
case 'PAINT':
700+
data_path = f"bdk.terrain_doodad.paint_layers[{layer.index}].{data_path}"
701+
case 'DECO':
702+
data_path = f"bdk.terrain_doodad.deco_layers[{layer.index}].{data_path}"
703+
case _:
704+
raise Exception(f"Unknown layer type: {layer_type}")
703705

704706
var.targets[0].data_path = data_path
705707

bdk_addon/terrain/doodad/operators.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ def execute(self, context: Context):
131131

132132
def get_terrain_doodad_paint_layer_nodes(doodad_paint_layer: 'BDK_PG_terrain_doodad_paint_layer'):
133133
terrain_info = doodad_paint_layer.terrain_doodad_object.bdk.terrain_doodad.terrain_info_object.bdk.terrain_info
134-
if doodad_paint_layer.layer_type == 'PAINT':
135-
# Get the terrain layer from the paint layer ID.
136-
terrain_info_paint_layer = get_terrain_info_paint_layer_by_id(terrain_info, doodad_paint_layer.paint_layer_id)
137-
return terrain_info_paint_layer.nodes if terrain_info_paint_layer is not None else None
138-
elif doodad_paint_layer.layer_type == 'DECO':
139-
# Get the terrain layer from the deco layer ID.
140-
terrain_info_deco_layer = get_terrain_info_deco_layer_by_id(terrain_info, doodad_paint_layer.deco_layer_id)
141-
return terrain_info_deco_layer.nodes if terrain_info_deco_layer is not None else None
134+
match doodad_paint_layer.layer_type:
135+
case 'PAINT':
136+
# Get the terrain layer from the paint layer ID.
137+
terrain_info_paint_layer = get_terrain_info_paint_layer_by_id(terrain_info, doodad_paint_layer.paint_layer_id)
138+
return terrain_info_paint_layer.nodes if terrain_info_paint_layer is not None else None
139+
case 'DECO':
140+
# Get the terrain layer from the deco layer ID.
141+
terrain_info_deco_layer = get_terrain_info_deco_layer_by_id(terrain_info, doodad_paint_layer.deco_layer_id)
142+
return terrain_info_deco_layer.nodes if terrain_info_deco_layer is not None else None
142143
return None
143144

144145

bdk_addon/terrain/doodad/ui.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ def draw_item(self, context: Context, layout, data, item, icon, active_data, act
4141
class BDK_UL_terrain_doodad_paint_layers(UIList):
4242

4343
def draw_item(self, context: Context, layout, data, item, icon, active_data, active_propname, index):
44-
if item.layer_type == 'PAINT':
45-
layout.label(text=item.paint_layer_name if item.paint_layer_name else '<no layer selected>', icon='BRUSH_DATA')
46-
elif item.layer_type == 'DECO':
47-
layout.label(text=item.deco_layer_name if item.deco_layer_name else '<no layer selected>', icon='MONKEY')
48-
elif item.layer_type == 'ATTRIBUTE':
49-
layout.label(text=item.attribute_layer_id if item.attribute_layer_id else '<no attribute>', icon='MODIFIER_DATA')
44+
match item.layer_type:
45+
case 'PAINT':
46+
layout.label(text=item.paint_layer_name if item.paint_layer_name else '<no layer selected>', icon='BRUSH_DATA')
47+
case 'DECO':
48+
layout.label(text=item.deco_layer_name if item.deco_layer_name else '<no layer selected>', icon='MONKEY')
49+
case 'ATTRIBUTE':
50+
layout.label(text=item.attribute_layer_id if item.attribute_layer_id else '<no attribute>', icon='MODIFIER_DATA')
5051
layout.prop(item, 'operation', emboss=False, text='')
5152
layout.prop(item, 'mute', text='', icon='HIDE_ON' if item.mute else 'HIDE_OFF', emboss=False)
5253

@@ -733,19 +734,21 @@ def draw(self, context: Context):
733734

734735
flow.separator()
735736

736-
if scatter_layer_object.scale_mode == 'UNIFORM':
737-
flow.prop(scatter_layer_object, 'scale_uniform', text='Scale')
738-
elif scatter_layer_object.scale_mode == 'NON_UNIFORM':
739-
flow.prop(scatter_layer_object, 'scale', text='Scale')
737+
match scatter_layer_object.scale_mode:
738+
case 'UNIFORM':
739+
flow.prop(scatter_layer_object, 'scale_uniform', text='Scale')
740+
case 'NON_UNIFORM':
741+
flow.prop(scatter_layer_object, 'scale', text='Scale')
740742

741743
flow.separator()
742744

743-
if scatter_layer_object.scale_mode == 'UNIFORM':
744-
flow.prop(scatter_layer_object, 'scale_random_uniform_min', text='Random Scale Min')
745-
flow.prop(scatter_layer_object, 'scale_random_uniform_max', text='Max')
746-
elif scatter_layer_object.scale_mode == 'NON_UNIFORM':
747-
flow.prop(scatter_layer_object, 'scale_random_min', text='Random Scale Min')
748-
flow.prop(scatter_layer_object, 'scale_random_max', text='Max')
745+
match scatter_layer_object.scale_mode:
746+
case 'UNIFORM':
747+
flow.prop(scatter_layer_object, 'scale_random_uniform_min', text='Random Scale Min')
748+
flow.prop(scatter_layer_object, 'scale_random_uniform_max', text='Max')
749+
case 'NON_UNIFORM':
750+
flow.prop(scatter_layer_object, 'scale_random_min', text='Random Scale Min')
751+
flow.prop(scatter_layer_object, 'scale_random_max', text='Max')
749752

750753
flow.prop(scatter_layer_object, 'scale_seed', text='Seed')
751754

bdk_addon/terrain/operators.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ def execute(self, context: Context):
9999
paint_layers = terrain_info.paint_layers
100100
paint_layers_index = terrain_info.paint_layers_index
101101

102-
if self.direction == 'UP' and paint_layers_index > 0:
103-
paint_layers.move(paint_layers_index, paint_layers_index - 1)
104-
terrain_info.paint_layers_index -= 1
105-
build_terrain_material(active_object)
106-
elif self.direction == 'DOWN' and paint_layers_index < len(paint_layers) - 1:
107-
paint_layers.move(paint_layers_index, paint_layers_index + 1)
108-
terrain_info.paint_layers_index += 1
109-
build_terrain_material(active_object)
102+
match self.direction:
103+
case 'UP':
104+
if paint_layers_index > 0:
105+
paint_layers.move(paint_layers_index, paint_layers_index - 1)
106+
terrain_info.paint_layers_index -= 1
107+
build_terrain_material(active_object)
108+
case 'DOWN':
109+
if paint_layers_index < len(paint_layers) - 1:
110+
paint_layers.move(paint_layers_index, paint_layers_index + 1)
111+
terrain_info.paint_layers_index += 1
112+
build_terrain_material(active_object)
110113

111114
# The order of the paint layers changed. Therefore, we need to:
112115
# 1. Rebuild the paint layer node groups.
@@ -465,19 +468,20 @@ def add_terrain_layer_node(terrain_info_object: Object, nodes, type: str):
465468
node.terrain_info_object = terrain_info_object
466469
node.type = type
467470

468-
if type == 'PAINT':
469-
# mesh_data = cast(Mesh, terrain_info_object.data)
470-
# TODO: when we can paint non-color data, rewrite this!
471-
# Add the density map attribute to the TerrainInfo mesh.
472-
terrain_info_object.vertex_groups.new(name=node.id)
473-
# attribute = mesh_data.attributes.get(node.id, None)
474-
# vertex_count = len(attribute.data)
475-
# color_data = numpy.ndarray(shape=(vertex_count, 4), dtype=float)
476-
# color_data[:] = (0.0, 0.0, 0.0, 0.0)
477-
# attribute.data.foreach_set('color', color_data.flatten())
478-
elif type == 'FIELD':
479-
mesh_data = cast(Mesh, terrain_info_object.data)
480-
mesh_data.attributes.new(node.id, 'FLOAT', domain='POINT')
471+
match type:
472+
case 'PAINT':
473+
# mesh_data = cast(Mesh, terrain_info_object.data)
474+
# TODO: when we can paint non-color data, rewrite this!
475+
# Add the density map attribute to the TerrainInfo mesh.
476+
terrain_info_object.vertex_groups.new(name=node.id)
477+
# attribute = mesh_data.attributes.get(node.id, None)
478+
# vertex_count = len(attribute.data)
479+
# color_data = numpy.ndarray(shape=(vertex_count, 4), dtype=float)
480+
# color_data[:] = (0.0, 0.0, 0.0, 0.0)
481+
# attribute.data.foreach_set('color', color_data.flatten())
482+
case 'FIELD':
483+
mesh_data = cast(Mesh, terrain_info_object.data)
484+
mesh_data.attributes.new(node.id, 'FLOAT', domain='POINT')
481485

482486
# Move the node to the top of the list.
483487
nodes.move(len(nodes) - 1, 0)
@@ -507,14 +511,15 @@ def remove_terrain_layer_node(terrain_info_object: Object, nodes, nodes_index: i
507511

508512

509513
def move_terrain_layer_node(direction: str, nodes, nodes_index: int) -> int:
510-
if direction == 'UP':
511-
if nodes_index > 0:
512-
nodes.move(nodes_index, nodes_index - 1)
513-
nodes_index -= 1
514-
elif direction == 'DOWN':
515-
if nodes_index < len(nodes) - 1:
516-
nodes.move(nodes_index, nodes_index + 1)
517-
nodes_index += 1
514+
match direction:
515+
case'UP':
516+
if nodes_index > 0:
517+
nodes.move(nodes_index, nodes_index - 1)
518+
nodes_index -= 1
519+
case 'DOWN':
520+
if nodes_index < len(nodes) - 1:
521+
nodes.move(nodes_index, nodes_index + 1)
522+
nodes_index += 1
518523
return nodes_index
519524

520525

0 commit comments

Comments
 (0)