Skip to content

Commit

Permalink
fix: fix Blender version polling
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Jul 12, 2024
1 parent c0eca5d commit 2768161
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 71 deletions.
4 changes: 2 additions & 2 deletions bevels/bevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, set_stream, has_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, add_smooth_by_angle, rectify_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_obj_mode, obj_exists, obj_is_mesh
from .. lib.polling import ctx_obj_mode, obj_exists, obj_is_mesh, app_minor_version


mod_bevel = "Bevel — ND B"
Expand Down Expand Up @@ -277,7 +277,7 @@ def add_smooth_shading(self, context):
if not get_preferences().enable_auto_smooth:
return

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
add_smooth_by_angle(self.target_object)
return

Expand Down
14 changes: 7 additions & 7 deletions bevels/edge_bevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, has_stream, set_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, rectify_smooth_by_angle, add_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_edit_mode, obj_edges_selected, obj_is_mesh
from .. lib.polling import ctx_edit_mode, obj_edges_selected, obj_is_mesh, app_minor_version


mod_bevel = "Bevel — ND EB"
Expand Down Expand Up @@ -181,7 +181,7 @@ def do_invoke(self, context, event):

bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)
Expand Down Expand Up @@ -212,7 +212,7 @@ def do_invoke(self, context, event):

self.target_object = context.active_object

if bpy.app.version < (3, 4, 0):
if app_minor_version() < (3, 4):
if not self.target_object.data.use_customdata_edge_bevel:
self.target_object.data.use_customdata_edge_bevel = True

Expand Down Expand Up @@ -278,7 +278,7 @@ def add_smooth_shading(self, context):
if not get_preferences().enable_auto_smooth:
return

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
bpy.ops.object.mode_set(mode='OBJECT')
add_smooth_by_angle(self.target_object)
bpy.ops.object.mode_set(mode='EDIT')
Expand Down Expand Up @@ -331,7 +331,7 @@ def take_edges_snapshot(self, context):

bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)
Expand All @@ -358,7 +358,7 @@ def operate(self, context):

bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)
Expand Down Expand Up @@ -403,7 +403,7 @@ def revert(self, context):

bevel_weight_layer = None

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bevel_weight_layer = bm.edges.layers.bevel_weight.verify()
else:
bevel_weight_layer = bm.edges.layers.float.get("bevel_weight_edge", None)
Expand Down
4 changes: 2 additions & 2 deletions bevels/vertex_bevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, has_stream, set_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, rectify_smooth_by_angle, add_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_edit_mode, obj_is_mesh, obj_verts_selected
from .. lib.polling import ctx_edit_mode, obj_is_mesh, obj_verts_selected, app_minor_version


mod_bevel = "Bevel — ND VB"
Expand Down Expand Up @@ -261,7 +261,7 @@ def add_smooth_shading(self, context):
if not get_preferences().enable_auto_smooth:
return

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
bpy.ops.object.mode_set(mode='OBJECT')
add_smooth_by_angle(self.target_object)
bpy.ops.object.mode_set(mode='EDIT')
Expand Down
4 changes: 2 additions & 2 deletions booleans/hydrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property, draw_hint
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences
from .. lib.polling import ctx_obj_mode, list_ok
from .. lib.polling import ctx_obj_mode, list_ok, app_minor_version


class ND_OT_hydrate(BaseOperator):
Expand Down Expand Up @@ -130,7 +130,7 @@ def finish(self, context):
new_obj.select_set(True)
bpy.context.view_layer.objects.active = new_obj

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bpy.ops.object.parent_clear({'object': new_obj}, type='CLEAR_KEEP_TRANSFORM')
else:
with bpy.context.temp_override(object=new_obj):
Expand Down
4 changes: 2 additions & 2 deletions deform/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, has_stream, set_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, rectify_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_obj_mode, obj_is_mesh, ctx_objects_selected
from .. lib.polling import ctx_obj_mode, obj_is_mesh, ctx_objects_selected, app_minor_version


mod_lattice = "Lattice — ND L"
Expand Down Expand Up @@ -293,7 +293,7 @@ def add_lattice_object(self, context):
context.active_object.data.name = "ND — Lattice"
context.active_object.data.use_outside = True

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
context.active_object.scale = eval_obj.dimensions * 1.001
else:
context.active_object.dimensions = eval_obj.dimensions * 1.001
Expand Down
4 changes: 2 additions & 2 deletions extrusion/profile_extrude.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, has_stream, set_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, rectify_smooth_by_angle, add_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_multi_mode, obj_is_mesh, ctx_objects_selected
from .. lib.polling import ctx_multi_mode, obj_is_mesh, ctx_objects_selected, app_minor_version


mod_screw = "Extrusion — ND PE"
Expand Down Expand Up @@ -230,7 +230,7 @@ def add_smooth_shading(self, context):
bpy.ops.object.mode_set(mode='OBJECT')
return_to_edit = True

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
add_smooth_by_angle(self.target_object)
if return_to_edit:
bpy.ops.object.mode_set(mode='EDIT')
Expand Down
4 changes: 2 additions & 2 deletions extrusion/screw.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, has_stream, set_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, add_smooth_by_angle, rectify_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_multi_mode, obj_moddable, ctx_objects_selected
from .. lib.polling import ctx_multi_mode, obj_moddable, ctx_objects_selected, app_minor_version


mod_displace = "Offset — ND SCR"
Expand Down Expand Up @@ -246,7 +246,7 @@ def add_smooth_shading(self, context):
bpy.ops.object.mode_set(mode='OBJECT')
return_to_edit = True

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
add_smooth_by_angle(self.target_object)
if return_to_edit:
bpy.ops.object.mode_set(mode='EDIT')
Expand Down
4 changes: 2 additions & 2 deletions extrusion/solidify.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .. lib.numeric_input import update_stream, no_stream, get_stream_value, new_stream, has_stream, set_stream
from .. lib.modifiers import new_modifier, remove_modifiers_ending_with, rectify_smooth_by_angle, add_smooth_by_angle
from .. lib.objects import get_real_active_object
from .. lib.polling import ctx_obj_mode, obj_is_mesh, ctx_objects_selected
from .. lib.polling import ctx_obj_mode, obj_is_mesh, ctx_objects_selected, app_minor_version


mod_displace = "Offset — ND SOL"
Expand Down Expand Up @@ -192,7 +192,7 @@ def add_smooth_shading(self, context):
if not get_preferences().enable_auto_smooth:
return

if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
add_smooth_by_angle(self.target_object)
return

Expand Down
3 changes: 2 additions & 1 deletion lib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from mathutils import Vector, Matrix
from gpu_extras.batch import batch_for_shader
from . preferences import get_preferences
from . polling import app_minor_version


def register_axis_handler(cls):
Expand Down Expand Up @@ -98,7 +99,7 @@ def update_axis(cls):

shader = None

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
else:
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
Expand Down
23 changes: 12 additions & 11 deletions lib/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import bpy
import re
from math import radians
from .. lib.preferences import get_preferences
from . preferences import get_preferences
from . polling import app_minor_version


def new_modifier(object, mod_name, mod_type, rectify=True):
Expand Down Expand Up @@ -80,7 +81,7 @@ def rectify_mod_order(object, mod_name):
if matching_mod_index is None:
return

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 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):
Expand All @@ -100,7 +101,7 @@ def has_sba_mod(object):


def add_smooth_by_angle(object):
if bpy.app.version < (4, 1, 0):
if app_minor_version() < (4, 1):
return

if has_sba_mod(object):
Expand All @@ -109,22 +110,22 @@ def add_smooth_by_angle(object):
with bpy.context.temp_override(object=object):
sba_node_group = bpy.data.node_groups.get("Smooth by Angle")

if sba_node_group == None and bpy.app.version == (4, 1, 0):
if sba_node_group == None and app_minor_version() == (4, 1):
bpy.ops.object.shade_smooth()
bpy.ops.object.modifier_add_node_group(asset_library_type='ESSENTIALS', asset_library_identifier="",
relative_asset_identifier="geometry_nodes\\smooth_by_angle.blend\\NodeTree\\Smooth by Angle")

if sba_node_group == None and bpy.app.version > (4, 1, 0):
if sba_node_group == None and app_minor_version() > (4, 1):
bpy.ops.object.shade_auto_smooth()

sba_mod = None

if sba_node_group != None:
sba_mod = object.modifiers.new("Smooth — ND SBA", 'NODES')
sba_mod.node_group = sba_node_group
if bpy.app.version > (4, 1, 0):
if app_minor_version() > (4, 1):
sba_mod.use_pin_to_last = True
if bpy.app.version >= (4, 1, 0):
if app_minor_version() >= (4, 1):
sba_mod.show_group_selector = False

# It isn't pretty, but it's the only way to get the modifier as modifier_add_node_group/shade_auto_smooth
Expand Down Expand Up @@ -168,7 +169,7 @@ def set_smoothing_angle(object, angle, ignore_sharpness=False):
def rectify_smooth_by_angle(object, force=False):
# For Blender 4.1.0 and above, the smoothing and weighted normal
# modifiers are pinned at the end of the stack by ND.
if force == False and bpy.app.version > (4, 1, 0):
if force == False and app_minor_version() > (4, 1):
return

mod_order = ['Smooth by Angle', 'Smooth — ND SBA', 'Weighted Normal — ND WN']
Expand All @@ -178,7 +179,7 @@ def rectify_smooth_by_angle(object, force=False):
if not(mod_name in object_mods):
continue

if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bpy.ops.object.modifier_move_to_index({'object': object}, modifier=mod_name, index=len(object_mods) - 1)
else:
with bpy.context.temp_override(object=object):
Expand Down Expand Up @@ -210,7 +211,7 @@ def remove_modifiers_ending_with(objects, suffix, strict=False):
for mod_name in mod_names:
base_name = re.sub(r"(.+?)(\.[0-9]{3})$", r"\1", mod_name) if not strict else mod_name
if base_name.endswith(suffix):
if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bpy.ops.object.modifier_remove({'object': object}, modifier=mod_name)
else:
with bpy.context.temp_override(object=object):
Expand All @@ -224,7 +225,7 @@ 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):
if bpy.app.version < (4, 0, 0):
if app_minor_version() < (4, 0):
bpy.ops.object.modifier_remove({'object': object}, modifier=mod_name)
else:
with bpy.context.temp_override(object=object):
Expand Down
5 changes: 3 additions & 2 deletions lib/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from math import radians
from mathutils.geometry import distance_point_to_plane, normal
from . preferences import get_preferences
from . polling import app_minor_version


def add_single_vertex_object(cls, context, name):
Expand All @@ -47,7 +48,7 @@ def add_single_vertex_object(cls, context, name):

context.view_layer.objects.active = obj

if bpy.app.version < (4, 1, 0):
if app_minor_version() < (4, 1):
bpy.ops.object.shade_smooth()
obj.data.use_auto_smooth = True
obj.data.auto_smooth_angle = radians(float(get_preferences().default_smoothing_angle))
Expand Down Expand Up @@ -111,7 +112,7 @@ def create_duplicate_liftable_geometry(context, mode, object_name, ignore_comple

bevel_weight_layer = None

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

0 comments on commit 2768161

Please sign in to comment.