Skip to content

Commit

Permalink
fix: fix additional Blender 4 compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Dec 1, 2023
1 parent 6c25e01 commit 2ffe4af
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 30 deletions.
63 changes: 46 additions & 17 deletions bevels/edge_bevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,17 @@ def do_invoke(self, context, event):
remove_modifiers_ending_with(context.selected_objects, ' — ND EB')
for object in context.selected_objects:
bm = bmesh.from_edit_mesh(object.data)
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()

for edge in bm.edges:
edge[bevel_weight_layer] = 0

bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)

if bevel_weight_layer is not None:
for edge in bm.edges:
edge[bevel_weight_layer] = 0

bmesh.update_edit_mesh(object.data)

Expand Down Expand Up @@ -282,14 +289,21 @@ def take_edges_snapshot(self, context):

data = context.active_object.data
bm = bmesh.from_edit_mesh(data)
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()

selected_edges = [edge for edge in bm.edges if edge.select]
for edge in selected_edges:
self.edges_snapshot[edge.index] = edge[bevel_weight_layer]
self.edge_weight_average += edge[bevel_weight_layer]

self.edge_weight_average /= len(selected_edges)
bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)

if bevel_weight_layer is not None:
selected_edges = [edge for edge in bm.edges if edge.select]
for edge in selected_edges:
self.edges_snapshot[edge.index] = edge[bevel_weight_layer]
self.edge_weight_average += edge[bevel_weight_layer]

self.edge_weight_average /= len(selected_edges)


def operate(self, context):
Expand All @@ -302,8 +316,16 @@ def operate(self, context):

data = context.active_object.data
bm = bmesh.from_edit_mesh(data)
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()


bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)
if bevel_weight_layer is None:
bevel_weight_layer = bm.edges.layers.float.new("bevel_weight_edge")

selected_edges = [edge for edge in bm.edges if edge.select]
for edge in selected_edges:
edge[bevel_weight_layer] = self.weight
Expand Down Expand Up @@ -336,11 +358,18 @@ def revert(self, context):

data = context.active_object.data
bm = bmesh.from_edit_mesh(data)
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()

bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)

selected_edges = [edge for edge in bm.edges if edge.select]
for edge in selected_edges:
edge[bevel_weight_layer] = self.edges_snapshot[edge.index]
if bevel_weight_layer is not None:
selected_edges = [edge for edge in bm.edges if edge.select]
for edge in selected_edges:
edge[bevel_weight_layer] = self.edges_snapshot[edge.index]

bmesh.update_edit_mesh(data)

Expand Down
18 changes: 15 additions & 3 deletions lib/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def rectify_mod_order(object, mod_name):
if matching_mod_index is None:
return

bpy.ops.object.modifier_move_to_index({'object': object}, modifier=mod_name, index=matching_mod_index)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_move_to_index({'object': object}, modifier=mod_name, index=matching_mod_index)
else:
with bpy.context.temp_override(object=object):
bpy.ops.object.modifier_move_to_index(modifier=mod_name, index=matching_mod_index)


def remove_problematic_bevels(object):
Expand All @@ -98,7 +102,11 @@ def remove_modifiers_ending_with(objects, suffix):
for mod_name in mod_names:
base_name = re.sub(r"(.+?)(\.[0-9]{3})$", r"\1", mod_name)
if base_name.endswith(suffix):
bpy.ops.object.modifier_remove({'object': object}, modifier=mod_name)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_remove({'object': object}, modifier=mod_name)
else:
with bpy.context.temp_override(object=object):
bpy.ops.object.modifier_remove(modifier=mod_name)


def remove_modifiers_starting_with(objects, suffix):
Expand All @@ -108,4 +116,8 @@ def remove_modifiers_starting_with(objects, suffix):
for mod_name in mod_names:
base_name = re.sub(r"(.+?)(\.[0-9]{3})$", r"\1", mod_name)
if base_name.startswith(suffix):
bpy.ops.object.modifier_remove({'object': object}, modifier=mod_name)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_remove({'object': object}, modifier=mod_name)
else:
with bpy.context.temp_override(object=object):
bpy.ops.object.modifier_remove(modifier=mod_name)
8 changes: 6 additions & 2 deletions scene/clean_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def remove_utils(self):
if obj and obj.name not in active_util_object_names:
deleted_objects.append(obj)
removal_count += 1

bpy.ops.object.delete({'active_object': None, 'object': None, 'selected_objects': deleted_objects}, use_global=False)

if bpy.app.version < (4, 0, 0):
bpy.ops.object.delete({'active_object': None, 'object': None, 'selected_objects': deleted_objects}, use_global=False)
else:
with bpy.context.temp_override(active_object=None, object=None, selected_objects=deleted_objects):
bpy.ops.object.delete(use_global=False)

return removal_count

Expand Down
6 changes: 5 additions & 1 deletion standalone/cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ def finish(self, context):
for apply_obj in self.applied_utils:
for mod in self.target_obj.modifiers:
if mod.type == 'BOOLEAN' and mod.object == apply_obj:
bpy.ops.object.modifier_apply({'object': self.target_obj}, modifier=mod.name)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_apply({'object': self.target_obj}, modifier=mod.name)
else:
with bpy.context.temp_override(object=self.target_obj):
bpy.ops.object.modifier_apply(modifier=mod.name)

bpy.context.view_layer.objects.active = self.util_mods[self.util_current_index].object

Expand Down
32 changes: 25 additions & 7 deletions utils/apply_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,25 @@ def collapse_modifiers(self, obj):

for mod_name in mods_to_apply:
try:
bpy.ops.object.modifier_apply({'object': obj}, modifier=mod_name)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_apply({'object': obj}, modifier=mod_name)
else:
with bpy.context.temp_override(object=obj):
bpy.ops.object.modifier_apply(modifier=mod_name)
except:
# If the modifier is disabled, just remove it.
bpy.ops.object.modifier_remove({'object': obj}, modifier=mod_name)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_remove({'object': obj}, modifier=mod_name)
else:
with bpy.context.temp_override(object=obj):
bpy.ops.object.modifier_remove(modifier=mod_name)

for mod_name in mods_to_remove:
bpy.ops.object.modifier_remove({'object': obj}, modifier=mod_name)
if bpy.app.version < (4, 0, 0):
bpy.ops.object.modifier_remove({'object': obj}, modifier=mod_name)
else:
with bpy.context.temp_override(object=obj):
bpy.ops.object.modifier_remove(modifier=mod_name)


def remove_vertex_groups(self, obj):
Expand All @@ -109,11 +121,17 @@ def remove_edge_weights(self, obj):
bm = bmesh.new()
bm.from_mesh(obj.data)

bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)

edges = list(bm.edges)
for edge in edges:
edge[bevel_weight_layer] = 0
if bevel_weight_layer is not None:
edges = list(bm.edges)
for edge in edges:
edge[bevel_weight_layer] = 0

bm.to_mesh(obj.data)
bm.free()
Expand Down

0 comments on commit 2ffe4af

Please sign in to comment.