cleanup, little fixes and improvements

This commit is contained in:
jrkb 2024-08-04 12:52:37 +02:00
parent d7a73116fd
commit 9e910239ac
2 changed files with 301 additions and 188 deletions

114
butils.py
View file

@ -2,6 +2,7 @@ import bpy
import mathutils
import queue
import importlib
import os
# then import dependencies for our addon
if "Font" in locals():
@ -289,11 +290,7 @@ def find_font_face_object(font_obj, face_name):
return face
return None
def move_in_fontcollection(obj, fontcollection):
for c in obj.users_collection:
c.objects.unlink(obj)
if fontcollection.objects.find(obj.name) < 0:
fontcollection.objects.link(obj)
def move_in_fontcollection(obj, fontcollection, allow_duplicates=False):
# parent nesting structure
# the font object
@ -339,10 +336,105 @@ def move_in_fontcollection(obj, fontcollection):
glyphs_obj["face_name"] = obj["face_name"]
glyphs_obj["font_name"] = obj["font_name"]
def get_hash(o):
return hash(tuple(tuple(v.co) for v in o.data.vertices ))
for other_obj in find_objects_by_custom_property(glyphs_obj.children, "glyph", obj["glyph"]):
if get_hash(other_obj) == get_hash(obj) and not allow_duplicates:
return other_obj
# and now parent it!
if obj.parent != glyphs_obj:
obj.parent = glyphs_obj
for c in obj.users_collection:
c.objects.unlink(obj)
if fontcollection.objects.find(obj.name) < 0:
fontcollection.objects.link(obj)
return obj
def load_font_from_filepath(filepath):
if not filepath.endswith(".glb") and not filepath.endswith(".gltf"):
ShowMessageBox(f"{bl_info['name']} Font loading error", 'ERROR', f"Filepath({filepath}) is not a *.glb or *.gltf file")
return False
font3d_data = bpy.context.scene.font3d_data
allObjectsBefore = []
for ob in bpy.data.objects:
allObjectsBefore.append(ob.name)
bpy.ops.import_scene.gltf(filepath=filepath)
fontcollection = bpy.data.collections.get("Font3D")
if fontcollection is None:
fontcollection = bpy.data.collections.new("Font3D")
remove_list = []
all_objects = []
for o in bpy.data.objects:
all_objects.append(o)
print("all objects", len(all_objects))
for o in all_objects:
if o.name not in allObjectsBefore:
# must be new
if ("glyph" in o.keys()
and "face_name" in o.keys()
and "font_name" in o.keys()):
print("adding glyph", o)
glyph_id = o["glyph"]
font_name = o["font_name"]
face_name = o["face_name"]
glyph_obj = move_in_fontcollection(
o,
fontcollection)
Font.add_glyph(
font_name,
face_name,
glyph_id,
glyph_obj)
if glyph_obj != o:
remove_list.append(o)
found = False
for f in font3d_data.available_fonts.values():
if f.font_name == font_name:
found = True
break
if not found:
f = font3d_data.available_fonts.add()
f.font_name = font_name
print(f"{__name__} added {font_name}")
else:
remove_list.append(o)
print("add to remove list", o)
for o in remove_list:
bpy.data.objects.remove(o, do_unlink=True)
print("removed",o)
print("butils:: should have been doing loading the fonts")
def getPreferences(context):
preferences = context.preferences
return preferences.addons['font3d'].preferences
# clear available fonts
def clear_available_fonts():
bpy.context.scene.font3d_data.available_fonts.clear()
def load_available_fonts():
preferences = getPreferences(bpy.context)
currentObjects = []
for ob in bpy.data.objects:
currentObjects.append(ob.name)
print(f"assets folder: {preferences.assets_dir}")
font_dir = f"{preferences.assets_dir}/fonts"
for file in os.listdir(font_dir):
if file.endswith(".glb") or file.endswith(".gltf"):
font_path = os.path.join(font_dir, file)
load_font_from_filepath(font_path)
def ShowMessageBox(title = "Message Box", icon = 'INFO', message=""):
"""Show a simple message box
@ -379,7 +471,6 @@ def ShowMessageBox(title = "Message Box", icon = 'INFO', message=""):
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
def set_text_on_curve(text_properties):
print(f"set text on curve {utils.get_timestamp()} with {text_properties.letter_spacing} and {text_properties.get('letter_spacingor')}")
mom = text_properties.text_object
if mom.type != "CURVE":
return False
@ -430,7 +521,8 @@ def set_text_on_curve(text_properties):
if c == 'n':
next_line_advance = get_next_line_advance(mom, advance, glyph_advance)
if advance == next_line_advance:
self.report({'INFO'}, f"would like to add new line for {text_properties.text} please")
# self.report({'INFO'}, f"would like to add new line for {text_properties.text} please")
print(f"would like to add new line for {text_properties.text} please")
advance = next_line_advance
continue
is_command = False
@ -443,7 +535,8 @@ def set_text_on_curve(text_properties):
ob = None
if regenerate:
if glyph == None:
self.report({'ERROR'}, f"Glyph not found for {font_name} {font_face} {glyph_id}")
# self.report({'ERROR'}, f"Glyph not found for {font_name} {font_face} {glyph_id}")
print(f"Glyph not found for {font_name} {font_face} {glyph_id}")
continue
ob = bpy.data.objects.new(f"{glyph_id}", glyph.data)
@ -476,7 +569,10 @@ def set_text_on_curve(text_properties):
if ob.rotation_mode != 'QUATERNION':
ob.rotation_mode = 'QUATERNION'
q = mathutils.Quaternion()
q.rotate(mathutils.Euler((radians(90),0,0)))
q.rotate(text_properties.orientation)
# mathutils.Euler((radians(text_properties.orientation.x),
# radians(text_properties.orientation.y),
# radians(text_properties.orientation.z))))
ob.rotation_quaternion = (mom.matrix_world @ motor[0] @ q.to_matrix().to_4x4()).to_quaternion()
scalor = 0.001