From fe686cd904c036aa5aaa7aac01f42b74dcbb4f21 Mon Sep 17 00:00:00 2001 From: Kirpal Demian Date: Thu, 22 Oct 2020 23:18:25 -0600 Subject: [PATCH] Added support for blender 2.9 --- helpers.py | 10 +++++++++- import_keyboard.py | 6 +++--- labels.py | 11 ++++++----- parse_json.py | 9 ++++++--- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/helpers.py b/helpers.py index 630de8a..6628211 100644 --- a/helpers.py +++ b/helpers.py @@ -1,4 +1,4 @@ -from bpy import context, ops, data +from bpy import context, ops, data, app from typing import List, Tuple @@ -43,6 +43,14 @@ def add_object(object, collection=None): context.scene.objects.link(object) +def apply_modifier(modifier_name: str): + """Apply the given modifier on the current object (2.8/2.9)""" + if app.version >= (2, 90, 0): + ops.object.modifier_apply(modifier=modifier_name) + else: + ops.object.modifier_apply(apply_as='DATA', modifier=modifier_name) + + def hex2rgb(hex: str): """Convert HEX color to Blender RGBA""" hex = hex.lstrip("#") diff --git a/import_keyboard.py b/import_keyboard.py index 3866112..63d1ad1 100644 --- a/import_keyboard.py +++ b/import_keyboard.py @@ -17,7 +17,7 @@ from math import pi from . import parse_json from . import labels -from .helpers import hex2rgb, add_object, select_object, unselect_all, set_active_object +from .helpers import * from .materials import make_key_material, make_led_material from .key import Label, Key, KeyBase, KeySegment, Profile @@ -39,7 +39,7 @@ def append_object(obj_name: str): object = bpy.data.objects[obj_name] set_active_object(object) for mod in object.modifiers: - bpy.ops.object.modifier_apply(modifier=mod.name) + apply_modifier(mod.name) appended_objects.append(obj_name) @@ -345,7 +345,7 @@ def read(filepath: str): set_active_object(case) # bevel the corners bpy.ops.object.modifier_add(type="BEVEL") - bpy.ops.object.modifier_apply(modifier="Bevel") + apply_modifier("Bevel") bpy.data.materials["Stem"].node_tree.nodes[ "Diffuse BSDF"].inputs["Color"].default_value = keyboard.stem_color diff --git a/labels.py b/labels.py index d0f3c9b..69a88d2 100644 --- a/labels.py +++ b/labels.py @@ -2,7 +2,7 @@ import re import os from math import pi -from .helpers import select_all, unselect_all, add_object, select_object, set_active_object, in_charset +from .helpers import * from .key import Profile, Label, Key from .char_ranges import CJK_RANGES, DEJAVU_RANGES from typing import Tuple, List @@ -77,7 +77,7 @@ def add_curve(key: Key, curve, text_length: int, label_material_name: str, label curve.modifiers["Remesh"].mode = 'SMOOTH' curve.modifiers["Remesh"].octree_depth = (4 if text_length == 1 else 7) curve.modifiers["Remesh"].use_remove_disconnected = False - ops.object.modifier_apply(apply_as='DATA', modifier="Remesh") + apply_modifier("Remesh") unselect_all() set_active_object(curve) @@ -90,7 +90,7 @@ def add_curve(key: Key, curve, text_length: int, label_material_name: str, label context.object.modifiers["Shrinkwrap"].use_positive_direction = True context.object.modifiers["Shrinkwrap"].use_negative_direction = True context.object.modifiers["Shrinkwrap"].target = key_object - ops.object.modifier_apply(apply_as='DATA', modifier="Shrinkwrap") + apply_modifier("Shrinkwrap") # create clipping cube ops.mesh.primitive_cube_add(location=(box[0] - box[2] * 0.5, box[1] + box[3] * 0.5, key_object.location[2] + key_object.dimensions[2] / 2)) @@ -107,7 +107,7 @@ def add_curve(key: Key, curve, text_length: int, label_material_name: str, label ops.object.modifier_add(type='BOOLEAN') context.object.modifiers["Boolean"].operation = 'INTERSECT' context.object.modifiers["Boolean"].object = cube - ops.object.modifier_apply(apply_as='DATA', modifier="Boolean") + apply_modifier("Boolean") data.objects.remove(cube) for edge in context.object.data.edges: @@ -194,7 +194,8 @@ def add(key: Key, fonts: List, label_position: int, material_name: str, key_obj) label_length = len(key_label.text) curve = None - if (match := re.fullmatch(r"<\/i>", key_label.text)) is not None: + match = re.fullmatch(r"<\/i>", key_label.text) + if match is not None: label_size *= 0.5 curve = add_icon(match[1], match[3], label_size, label_position, box) label_length = 1 diff --git a/parse_json.py b/parse_json.py index 655319f..3e6efb5 100644 --- a/parse_json.py +++ b/parse_json.py @@ -86,9 +86,12 @@ def load(filePath: str) -> Keyboard: keyboard.name = row["name"] if "switchType" in row: keyboard.switch_type = row["switchType"] - if "notes" in row and (color_match := re.search(r'led_color:\s*#([a-fA-F0-9]{3,6})', row["notes"])) is not None: - keyboard.led_color = '#' + color_match[1] - if (brightness_match := re.search(r'led_brightness:\s*(1|0\.?[0-9]*)', row["notes"])) is not None: + if "notes" in row: + color_match = re.search(r'led_color:\s*#([a-fA-F0-9]{3,6})', row["notes"]) + brightness_match = re.search(r'led_brightness:\s*(1|0\.?[0-9]*)', row["notes"]) + if color_match is not None: + keyboard.led_color = '#' + color_match[1] + if brightness_match is not None: keyboard.led_brightness = float(brightness_match[1]) if "css" in row: keyboard.css = row["css"]