regenerate on demand, easier orientation, various

This commit is contained in:
jrkb 2024-07-10 16:34:43 +02:00
parent 846b84b6f4
commit 07c38fcdaf
2 changed files with 200 additions and 70 deletions

View file

@ -89,8 +89,8 @@ def align_rotations_auto_pivot(mask, input_rotations, vectors, factors, local_ma
old_rotation = input_rotation.to_matrix()
old_axis = (old_rotation @ local_main_axis).normalized()
new_axis = vector
rotation_axis = (-(old_axis) + new_axis).normalized()
# rotation_axis = old_axis.cross(new_axis).normalized()
# rotation_axis = (-(old_axis) + new_axis).normalized()
rotation_axis = old_axis.cross(new_axis).normalized()
if rotation_axis.length < 1e-6:
# Vectors are linearly dependent, fallback to another axis
@ -276,9 +276,6 @@ def find_font_face_object(font_obj, face_name):
return None
def move_in_fontcollection(obj, fontcollection):
# print(turn_collection_hierarchy_into_path(obj))
# if scene.collection.objects.find(obj.name) >= 0:
# scene.collection.objects.unlink(obj)
for c in obj.users_collection:
c.objects.unlink(obj)
if fontcollection.objects.find(obj.name) < 0:
@ -305,6 +302,7 @@ def move_in_fontcollection(obj, fontcollection):
face_obj.empty_display_type = 'PLAIN_AXES'
face_obj["is_face"] = True
fontcollection.objects.link(face_obj)
# ensure custom properties are set
face_obj["face_name"] = obj["face_name"]
face_obj["font_name"] = obj["font_name"]
@ -367,22 +365,39 @@ 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
regenerate = False
glyph_objects = []
for g in text_properties.glyphs:
glyph_objects.append(g.glyph_object)
context_override = bpy.context.copy()
context_override["selected_objects"] = list(glyph_objects)
with bpy.context.temp_override(**context_override):
bpy.ops.object.delete()
for g in glyph_objects:
bpy.data.objects.remove(g, do_unlink=True)
# bpy.ops.object.delete({"selected_objects": glyph_objects})
text_properties.glyphs.clear()
# check if perhaps one glyph was deleted
if (type(g.glyph_object) == type(None)
or type(g.glyph_object.parent) == type(None)
or g.glyph_object.parent.users_collection != g.glyph_object.users_collection):
regenerate = True
if len(text_properties.text) != len(text_properties.glyphs):
regenerate = True
# if we regenerate.... delete objects
if regenerate:
context_override = bpy.context.copy()
context_override["selected_objects"] = list(glyph_objects)
with bpy.context.temp_override(**context_override):
bpy.ops.object.delete()
# remove deleted objects
# this is necessary
for g in glyph_objects:
if type(g) != type(None):
bpy.data.objects.remove(g, do_unlink=True)
text_properties.glyphs.clear()
#TODO: fix selection with context_override
previous_selection = bpy.context.selected_objects
@ -394,7 +409,6 @@ def set_text_on_curve(text_properties):
glyph_advance = 0
is_command = False
for i, c in enumerate(text_properties.text):
print(f"trying letter ({c})")
if c == '\\':
is_command = True
continue
@ -407,16 +421,21 @@ def set_text_on_curve(text_properties):
continue
is_command = False
glyph_id = c
glyph = Font.get_glyph(text_properties.font_name,
text_properties.font_face,
glyph_id)
if glyph == None:
self.report({'ERROR'}, f"Glyph not found for {font_name} {font_face} {glyph_id}")
continue
ob = None
if regenerate:
if glyph == None:
self.report({'ERROR'}, f"Glyph not found for {font_name} {font_face} {glyph_id}")
continue
ob = bpy.data.objects.new(f"{glyph_id}", glyph.data)
ob['linked_textobject'] = text_properties.text_index
ob = bpy.data.objects.new(f"{glyph_id}", glyph.data)
ob['linked_textobject'] = text_properties.text_id
else:
ob = text_properties.glyphs[i]['glyph_object']
distribution_type = 'CALCULATE'
if distribution_type == 'FOLLOW_PATH':
@ -431,7 +450,7 @@ def set_text_on_curve(text_properties):
location, tangent = calc_point_on_bezier_curve(mom, advance, True)
ob.location = mom.matrix_world @ location
mask = [0]
input_rotations = [mathutils.Vector((radians(90.0), 0.0, 0.0))]
input_rotations = [mathutils.Vector((0.0, 0.0, 0.0))]
vectors = [tangent]
factors = [1.0]
local_main_axis = mathutils.Vector((1.0, 0.0, 0.0))
@ -442,37 +461,29 @@ def set_text_on_curve(text_properties):
local_main_axis)
if ob.rotation_mode != 'QUATERNION':
ob.rotation_mode = 'QUATERNION'
ob.rotation_quaternion = (mom.matrix_world @ motor[0]).to_quaternion()
q = mathutils.Quaternion()
q.rotate(mathutils.Euler((radians(90),0,0)))
ob.rotation_quaternion = (mom.matrix_world @ motor[0] @ q.to_matrix().to_4x4()).to_quaternion()
scalor = 0.001
glyph_advance = (-1 * glyph.bound_box[0][0] + glyph.bound_box[4][0]) * scalor + text_properties.letter_spacing
ob.scale = (scalor, scalor, scalor)
mom.users_collection[0].objects.link(ob)
advance = advance + glyph_advance
glyph_data = text_properties.glyphs.add()
glyph_data.glyph_id = glyph_id
glyph_data.glyph_object = ob
glyph_data.letter_spacing = 0
ob.select_set(True)
# selected_objects.append(ob)
# selected_objects.append(mom)
mom.select_set(True)
bpy.context.view_layer.objects.active = mom
bpy.ops.object.parent_set(type='OBJECT')
# bpy.ops.object.select_all(action='DESELECT')
# for o in previous_selection:
# o.select_set(True)
# context_override = bpy.context.copy()
# context_override["selected_objects"] = selected_objects
# context_override["active_object"] = mom
# with bpy.context.temp_override(**context_override):
# bpy.ops.object.parent_set(type='OBJECT')
if regenerate:
mom.users_collection[0].objects.link(ob)
glyph_data = text_properties.glyphs.add()
glyph_data.glyph_id = glyph_id
glyph_data.glyph_object = ob
glyph_data.letter_spacing = 0
ob.select_set(True)
if regenerate:
mom.select_set(True)
bpy.context.view_layer.objects.active = mom
bpy.ops.object.parent_set(type='OBJECT')
return True