Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Added support for blender 2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirpal committed Oct 23, 2020
1 parent 41361e0 commit fe686cd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
10 changes: 9 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bpy import context, ops, data
from bpy import context, ops, data, app
from typing import List, Tuple


Expand Down Expand Up @@ -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("#")
Expand Down
6 changes: 3 additions & 3 deletions import_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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:
Expand Down Expand Up @@ -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 class=['\"](fa|kb) (fa|kb)-([a-zA-Z0-9\-]+)['\"]><\/i>", key_label.text)) is not None:
match = re.fullmatch(r"<i class=['\"](fa|kb) (fa|kb)-([a-zA-Z0-9\-]+)['\"]><\/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
Expand Down
9 changes: 6 additions & 3 deletions parse_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit fe686cd

Please sign in to comment.