diff --git a/__init__.py b/__init__.py index 28c6c3a..a478ec4 100644 --- a/__init__.py +++ b/__init__.py @@ -167,6 +167,7 @@ class FONT3D_glyph_properties(bpy.types.PropertyGroup): class FONT3D_text_properties(bpy.types.PropertyGroup): def update_callback(self, context): butils.set_text_on_curve(self) + text_index: bpy.props.IntProperty() font_name: bpy.props.StringProperty() font_face: bpy.props.StringProperty() text_object: bpy.props.PointerProperty(type=bpy.types.Object) @@ -245,6 +246,14 @@ class FONT3D_PT_panel(bpy.types.Panel): for i, t in enumerate(font3d_data.available_texts): if type(t.text_object) == type(None): remove_list.append(i) + continue + remove_me = True + for c in t.text_object.children: + if len(c.users_collection) > 0 and (c.get('linked_textobject')) != type(None) and c.get('linked_textobject') == t.text_index: + remove_me = False + if remove_me: + remove_list.append(i) + for i in remove_list: font3d_data.available_texts.remove(i) @@ -414,82 +423,14 @@ class FONT3D_OT_TestFont(bpy.types.Operator): font_name = "NM_Origin" font_face = "Tender" - # text_text_object = bpy.data.objects.new(f"{selected.name}_text", None) - # text_text_object.empty_display_type = 'PLAIN_AXES' - # selected.users_collection[0].objects.link(text_text_object) - distribution_type = 'DEFAULT' - bpy.ops.object.select_all(action='DESELECT') t = font3d_data.available_texts.add() - - offset = mathutils.Vector((0.0, 0.0, 0.0)) - advance = 0 - for i, c in enumerate(scene.font3d.test_text): - glyph_id = c - glyph = Font.get_glyph(font_name, font_face, glyph_id) - - 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) - - distribution_type = 'CALCULATE' - - if selected.type == "CURVE" and distribution_type == 'FOLLOW_PATH': - distribution_type = "FOLLOW_PATH" - curve_length = butils.get_curve_length(selected) - - ob.constraints.new(type='FOLLOW_PATH') - ob.constraints["Follow Path"].target = selected - ob.constraints["Follow Path"].use_fixed_location = True - ob.constraints["Follow Path"].offset_factor = advance / curve_length - ob.constraints["Follow Path"].use_curve_follow = True - ob.constraints["Follow Path"].forward_axis = "FORWARD_X" - ob.constraints["Follow Path"].up_axis = "UP_Y" - - # butils.ShowMessageBox("WHAT","INFO","I don't really know what you mean, lsaidry") - elif selected.type == "CURVE" and distribution_type == 'CALCULATE': - location, tangent = butils.calc_point_on_bezier_curve(selected, advance, True) - ob.location = selected.matrix_world @ location - mask = [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)) - motor = butils.align_rotations_auto_pivot(mask, - input_rotations, - vectors, - factors, - local_main_axis) - - ob.rotation_quaternion = (selected.matrix_world @ motor[0]).to_quaternion() - print("what") - - else: - offset.x = advance - ob.location = selected.location + offset - - scalor = 0.001 - - glyph_advance = (-1 * glyph.bound_box[0][0] + glyph.bound_box[4][0]) * scalor - - ob.scale = (scalor, scalor, scalor) - selected.users_collection[0].objects.link(ob) - - advance = advance + glyph_advance - tc = t.glyphs.add() - tc.glyph_id = c - tc.glyph_object = ob - tc.letter_spacing = 0 - - ob.select_set(True) - - selected.select_set(True) - bpy.context.view_layer.objects.active = selected - bpy.ops.object.parent_set(type='OBJECT') - bpy.ops.object.select_all(action='DESELECT') + text_index = 0 + for i, tt in enumerate(font3d_data.available_texts): + while text_index == tt.text_index: + text_index = text_index + 1 + t.text_index = text_index t.font_name = font_name t.font_face = font_face diff --git a/butils.py b/butils.py index 9fb4f91..338350a 100644 --- a/butils.py +++ b/butils.py @@ -41,6 +41,21 @@ def get_curve_length(curve_obj, resolution = -1): return total_length +def get_curve_line_lengths(curve_obj, resolution = -1): + output = [] + for spline in curve_obj.data.splines: + output.append(spline.calc_length(resolution=resolution)) + return output + +def get_next_line_advance(curve_obj, current_advance, previous_glyph_advance, resolution = -1): + curve_line_lengths = get_curve_line_lengths(curve_obj, resolution) + total_length = 0 + for cll in curve_line_lengths: + total_length += cll + if current_advance - previous_glyph_advance < total_length: + return total_length + return current_advance + def calc_point_on_bezier(bezier_point_1, bezier_point_2, t): p1 = bezier_point_1.co h1 = bezier_point_1.handle_right @@ -364,6 +379,8 @@ def set_text_on_curve(text_properties): 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() @@ -374,7 +391,21 @@ def set_text_on_curve(text_properties): curve_length = get_curve_length(mom) advance = 0 + 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 + if is_command: + 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") + advance = next_line_advance + continue + is_command = False glyph_id = c glyph = Font.get_glyph(text_properties.font_name, text_properties.font_face, @@ -385,6 +416,7 @@ def set_text_on_curve(text_properties): continue ob = bpy.data.objects.new(f"{glyph_id}", glyph.data) + ob['linked_textobject'] = text_properties.text_index distribution_type = 'CALCULATE' if distribution_type == 'FOLLOW_PATH':