diff options
author | Spivak Vladimir (cwolf3d) <cwolf3d@gmail.com> | 2019-09-16 01:34:28 +0300 |
---|---|---|
committer | Spivak Vladimir (cwolf3d) <cwolf3d@gmail.com> | 2019-09-16 01:34:28 +0300 |
commit | 541840f949d279e4c9ef7ec06d3aeed7ed29530c (patch) | |
tree | f2616aeed0557997e90ebd6b5724dc34c829c5c2 | |
parent | d860b3c7e2fef6d9517cd1a8ca667a838eb869d0 (diff) |
Curve Tools: Fixed bugs. Added spline sequence change
-rw-r--r-- | curve_tools/PathFinder.py | 50 | ||||
-rw-r--r-- | curve_tools/ShowCurveResolution.py | 44 | ||||
-rw-r--r-- | curve_tools/SplinesSequence.py | 275 | ||||
-rw-r--r-- | curve_tools/__init__.py | 39 |
4 files changed, 362 insertions, 46 deletions
diff --git a/curve_tools/PathFinder.py b/curve_tools/PathFinder.py index 2f661382..4df49c41 100644 --- a/curve_tools/PathFinder.py +++ b/curve_tools/PathFinder.py @@ -18,7 +18,7 @@ bl_info = { 'name': 'PathFinder', - 'author': 'Spivak Vladimir (http://cwolf3d.korostyshev.net)', + 'author': 'Spivak Vladimir (cwolf3d)', 'version': (0, 5, 0), 'blender': (2, 80, 0), 'location': 'Curve Tools addon. (N) Panel', @@ -225,7 +225,16 @@ def click(self, context, event): self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw_points, args, 'WINDOW', 'POST_VIEW')) for point in spline.points: - point.select = True + point.select = True + +def remove_handler(handlers): + for handler in handlers: + try: + bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW') + except: + pass + for handler in handlers: + handlers.remove(handler) class PathFinder(bpy.types.Operator): bl_idname = "curvetools.pathfinder" @@ -254,41 +263,32 @@ class PathFinder(bpy.types.Operator): context.area.tag_redraw() if event.type in {'ESC', 'TAB'}: # Cancel - for handler in self.handlers: - try: - bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW') - except: - pass - for handler in self.handlers: - self.handlers.remove(handler) - self.report({'INFO'}, "PathFinder deactivated") + remove_handler(self.handlers) return {'CANCELLED'} if event.type in {'X', 'DEL'}: # Cancel - for handler in self.handlers: - try: - bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW') - except: - pass - for handler in self.handlers: - self.handlers.remove(handler) + remove_handler(self.handlers) bpy.ops.curve.delete(type='VERT') return {'RUNNING_MODAL'} - elif event.alt and event.type == 'LEFTMOUSE': + elif event.alt and not event.shift and event.type == 'LEFTMOUSE': + remove_handler(self.handlers) + bpy.ops.curve.select_all(action='DESELECT') + click(self, context, event) + + elif event.alt and event.shift and event.type == 'LEFTMOUSE': click(self, context, event) elif event.alt and event.type == 'RIGHTMOUSE': + remove_handler(self.handlers) + bpy.ops.curve.select_all(action='DESELECT') click(self, context, event) + + elif event.alt and not event.shift and event.shift and event.type == 'RIGHTMOUSE': + click(self, context, event) elif event.type == 'A': - for handler in self.handlers: - try: - bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW') - except: - pass - for handler in self.handlers: - self.handlers.remove(handler) + remove_handler(self.handlers) bpy.ops.curve.select_all(action='DESELECT') elif event.type == 'MOUSEMOVE': # diff --git a/curve_tools/ShowCurveResolution.py b/curve_tools/ShowCurveResolution.py index 97cf310e..69271704 100644 --- a/curve_tools/ShowCurveResolution.py +++ b/curve_tools/ShowCurveResolution.py @@ -22,7 +22,6 @@ import bpy from bpy import * from bpy.props import * -from bpy.types import AddonPreferences, PropertyGroup import bgl import blf @@ -34,11 +33,7 @@ import mathutils from mathutils import Vector from mathutils.geometry import interpolate_bezier -import bpy_extras -from bpy_extras.view3d_utils import location_3d_to_region_2d as loc3d2d - - def get_points(spline, matrix_world): bezier_points = spline.bezier_points @@ -47,6 +42,8 @@ def get_points(spline, matrix_world): return [] r = spline.resolution_u + 1 + if r < 2: + return [] segments = len(bezier_points) if not spline.use_cyclic_u: @@ -67,16 +64,18 @@ def get_points(spline, matrix_world): return point_list -def draw(self, context, spline, curve_vertcolor, matrix_world): - - points = get_points(spline, matrix_world) +def draw(self, context, splines, curve_vertcolor, matrix_world): - shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') - batch = batch_for_shader(shader, 'POINTS', {"pos": points}) + for spline in splines: + points = get_points(spline, matrix_world) + + shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') + + batch = batch_for_shader(shader, 'POINTS', {"pos": points}) - shader.bind() - shader.uniform_float("color", curve_vertcolor) - batch.draw(shader) + shader.bind() + shader.uniform_float("color", curve_vertcolor) + batch.draw(shader) class ShowCurveResolution(bpy.types.Operator): @@ -109,17 +108,15 @@ class ShowCurveResolution(bpy.types.Operator): # color change in the panel curve_vertcolor = bpy.context.scene.curvetools.curve_vertcolor - # the arguments we pass the the callback - - splines = context.active_object.data.splines matrix_world = context.active_object.matrix_world - for spline in splines: - args = (self, context, spline, curve_vertcolor, matrix_world) - # Add the region OpenGL drawing callback - # draw in view space with 'POST_VIEW' and 'PRE_VIEW' - self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, args, 'WINDOW', 'POST_VIEW')) + # the arguments we pass the the callback + args = (self, context, splines, curve_vertcolor, matrix_world) + + # Add the region OpenGL drawing callback + # draw in view space with 'POST_VIEW' and 'PRE_VIEW' + self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, args, 'WINDOW', 'POST_VIEW')) context.window_manager.modal_handler_add(self) return {'RUNNING_MODAL'} @@ -127,3 +124,8 @@ class ShowCurveResolution(bpy.types.Operator): self.report({'WARNING'}, "View3D not found, cannot run operator") return {'CANCELLED'} + + @classmethod + def poll(cls, context): + return (context.object is not None and + context.object.type == 'CURVE') diff --git a/curve_tools/SplinesSequence.py b/curve_tools/SplinesSequence.py new file mode 100644 index 00000000..c64ceec1 --- /dev/null +++ b/curve_tools/SplinesSequence.py @@ -0,0 +1,275 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and / or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### +# + + +import bpy + +import bgl +import blf +import gpu +from gpu_extras.batch import batch_for_shader + +import math +import mathutils +from mathutils import Vector + +from bpy.props import ( + EnumProperty, + ) + +# ------------------------------------------------------------ +# ShowSplinesSequence + +def draw_number(n, co, font_height): + + point_list = [] + + numeral = [ + [Vector((0, 0, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((0, 0, 0))], + [Vector((0, 1, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0))], + [Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((0, 0, 0)), Vector((0, 0, 0)), Vector((1, 0, 0))], + [Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((0, 0, 0))], + [Vector((0, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0))], + [Vector((1, 2, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((0, 0, 0))], + [Vector((1, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((0, 0, 0)), Vector((0, 0, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((0, 1, 0))], + [Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((0, 0, 0))], + [Vector((0, 1, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((0, 0, 0)), Vector((0, 0, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0))], + [Vector((0, 0, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0))], + ] + + for num in numeral[n]: + point_list.extend([num * font_height + co]) + + return point_list + + +def draw(self, context, splines, sequence_color, font_thickness, font_size, matrix_world): + + splines_len = len(splines) + for n in range(0, splines_len): + + res = [int(x) for x in str(n)] + + i = 0 + for r in res: + # draw some text + if splines[n].type == 'BEZIER': + first_point_co = matrix_world @ splines[n].bezier_points[0].co + else: + first_point = matrix_world @ splines[n].points[0].co + first_point_co = Vector((first_point.x, first_point.y, first_point.z)) + + first_point_co = Vector((i, 0, 0)) + first_point_co + points = draw_number(r, first_point_co, font_size) + + shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') + + batch = batch_for_shader(shader, 'LINES', {"pos": points}) + + shader.bind() + bgl.glLineWidth(font_thickness) + shader.uniform_float("color", sequence_color) + batch.draw(shader) + i += font_size + font_size * 0.5 + +class ShowSplinesSequence(bpy.types.Operator): + bl_idname = "curve.show_splines_sequence" + bl_label = "Show Splines Sequence" + bl_description = "Show Splines Sequence / [ESC] - remove" + + handlers = [] + + def modal(self, context, event): + context.area.tag_redraw() + + if event.type in {'ESC'}: + for handler in self.handlers: + try: + bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW') + except: + pass + for handler in self.handlers: + self.handlers.remove(handler) + return {'CANCELLED'} + + return {'PASS_THROUGH'} + + + def invoke(self, context, event): + + if context.area.type == 'VIEW_3D': + + # color change in the panel + sequence_color = bpy.context.scene.curvetools.sequence_color + font_thickness = bpy.context.scene.curvetools.font_thickness + font_size = bpy.context.scene.curvetools.font_size + + splines = context.active_object.data.splines + matrix_world = context.active_object.matrix_world + + # the arguments we pass the the callback + args = (self, context, splines, sequence_color, font_thickness, font_size, matrix_world) + + # Add the region OpenGL drawing callback + # draw in view space with 'POST_VIEW' and 'PRE_VIEW' + self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, args, 'WINDOW', 'POST_VIEW')) + + context.window_manager.modal_handler_add(self) + return {'RUNNING_MODAL'} + else: + self.report({'WARNING'}, + "View3D not found, cannot run operator") + return {'CANCELLED'} + + @classmethod + def poll(cls, context): + return (context.object is not None and + context.object.type == 'CURVE') + +# ------------------------------------------------------------ +# RearrangeSpline + +def rearrangesplines(dataCurve, select_spline1, select_spline2): + + spline1 = dataCurve.splines[select_spline1] + spline2 = dataCurve.splines[select_spline2] + + bpy.ops.curve.select_all(action='SELECT') + bpy.ops.curve.spline_type_set(type='BEZIER') + bpy.ops.curve.select_all(action='DESELECT') + + type1 = spline1.type + type2 = spline2.type + + len_spline1 = len(spline1.bezier_points) + len_spline2 = len(spline2.bezier_points) + + newSpline = dataCurve.splines.new(type=type1) + newSpline.bezier_points.add(len_spline1 - 1) + newSpline.use_cyclic_u = spline1.use_cyclic_u + for n in range(0, len_spline1): + newSpline.bezier_points[n].co = spline1.bezier_points[n].co + newSpline.bezier_points[n].handle_left_type = spline1.bezier_points[n].handle_left_type + newSpline.bezier_points[n].handle_left = spline1.bezier_points[n].handle_left + newSpline.bezier_points[n].handle_right_type = spline1.bezier_points[n].handle_right_type + newSpline.bezier_points[n].handle_right = spline1.bezier_points[n].handle_right + spline1.bezier_points[n].select_control_point = True + + spline1.bezier_points[0].select_control_point = False + spline1.bezier_points[0].select_left_handle = False + spline1.bezier_points[0].select_right_handle = False + bpy.ops.curve.delete(type='VERT') + + spline1.bezier_points[0].select_control_point = True + bpy.ops.curve.spline_type_set(type=type2) + + bpy.ops.curve.select_all(action='DESELECT') + + spline1.bezier_points.add(len_spline2 - 1) + spline1.use_cyclic_u = spline2.use_cyclic_u + for n in range(0, len_spline2): + spline1.bezier_points[n].co = spline2.bezier_points[n].co + spline1.bezier_points[n].handle_left_type = spline2.bezier_points[n].handle_left_type + spline1.bezier_points[n].handle_left = spline2.bezier_points[n].handle_left + spline1.bezier_points[n].handle_right_type = spline2.bezier_points[n].handle_right_type + spline1.bezier_points[n].handle_right = spline2.bezier_points[n].handle_right + spline1.bezier_points[n].select_control_point = False + spline1.bezier_points[n].select_left_handle = False + spline1.bezier_points[n].select_right_handle = False + spline2.bezier_points[n].select_control_point = True + + spline2.bezier_points[0].select_control_point = False + spline2.bezier_points[0].select_left_handle = False + spline2.bezier_points[0].select_right_handle = False + bpy.ops.curve.delete(type='VERT') + + spline2.bezier_points[0].select_control_point = True + bpy.ops.curve.spline_type_set(type=type1) + + spline2.bezier_points.add(len_spline1 - 1) + spline2.use_cyclic_u = newSpline.use_cyclic_u + for m in range(0, len_spline1): + spline2.bezier_points[m].co = newSpline.bezier_points[m].co + spline2.bezier_points[m].handle_left_type = newSpline.bezier_points[m].handle_left_type + spline2.bezier_points[m].handle_left = newSpline.bezier_points[m].handle_left + spline2.bezier_points[m].handle_right_type = newSpline.bezier_points[m].handle_right_type + spline2.bezier_points[m].handle_right = newSpline.bezier_points[m].handle_right + + bpy.ops.curve.select_all(action='DESELECT') + for point in newSpline.bezier_points: + point.select_control_point = True + bpy.ops.curve.delete(type='VERT') + + spline2.bezier_points[0].select_control_point = True + +def rearrange(dataCurve, select_spline, command): + len_splines = len(dataCurve.splines) + if command == 'NEXT': + if select_spline < len_splines - 1: + rearrangesplines(dataCurve, select_spline + 1, select_spline) + + if command == 'PREV': + if select_spline > 0: + rearrangesplines(dataCurve, select_spline, select_spline - 1) + +class RearrangeSpline(bpy.types.Operator): + bl_idname = "curve.rearrange_spline" + bl_label = "Rearrange Spline" + bl_description = "Rearrange Spline" + + Types = [('NEXT', "Next", "next"), + ('PREV', "Prev", "prev")] + command : EnumProperty( + name="command", + description="Command (prev or next)", + items=Types + ) + + def execute(self, context): + bpy.ops.object.mode_set(mode = 'EDIT') + bpy.context.view_layer.update() + + dataCurve = context.active_object.data + + splines = context.active_object.data.splines + + select_spline = 0 + + sn = 0 + for spline in splines: + for bezier_points in spline.bezier_points: + if bezier_points.select_control_point: + select_spline = sn + sn += 1 + + sn = 0 + for spline in splines: + for point in spline.points: + if point.select: + select_spline = sn + sn += 1 + + rearrange(dataCurve, select_spline, self.command) + + return {'FINISHED'} + + @classmethod + def poll(cls, context): + return (context.object is not None and + context.object.type == 'CURVE') diff --git a/curve_tools/__init__.py b/curve_tools/__init__.py index 2a5abf5b..6549d8c6 100644 --- a/curve_tools/__init__.py +++ b/curve_tools/__init__.py @@ -56,6 +56,7 @@ from . import auto_loft from . import curve_outline from . import PathFinder from . import ShowCurveResolution +from . import SplinesSequence from . import internal, cad, toolpath, exports if 'internal' in locals(): @@ -220,6 +221,27 @@ class curvetoolsSettings(PropertyGroup): soft_min=2, description="Path thickness (px)" ) + sequence_color: FloatVectorProperty( + name="OUT", + default=(0.2, 0.9, 0.9, 1), + size=4, + subtype="COLOR", + min=0, + max=1 + ) + font_thickness: IntProperty( + name="Font thickness", + default=1, + min=1, max=1024, + soft_min=2, + description="Font thickness (px)" + ) + font_size: FloatProperty( + name="Font size", + default=0.5, + precision=3, + description="Font size" + ) class VIEW3D_PT_CurvePanel(Panel): @@ -373,6 +395,21 @@ class VIEW3D_PT_CurvePanel(Panel): row.prop(context.scene.curvetools, "curve_vertcolor", text="") row = col.row(align=True) row.operator("curve.show_resolution", text="Run [ESC]") + + # D.1 set spline sequence + row = col.row(align=True) + row.label(text="Show and rearrange spline sequence:") + row = col.row(align=True) + row.prop(context.scene.curvetools, "sequence_color", text="") + row.prop(context.scene.curvetools, "font_thickness", text="") + row.prop(context.scene.curvetools, "font_size", text="") + row = col.row(align=True) + oper = row.operator("curve.rearrange_spline", text="<") + oper.command = 'PREV' + oper = row.operator("curve.rearrange_spline", text=">") + oper.command = 'NEXT' + row = col.row(align=True) + row.operator("curve.show_splines_sequence", text="Run [ESC]") # D.2 remove splines row = col.row(align=True) @@ -475,6 +512,8 @@ classes = cad.operators + toolpath.operators + exports.operators + Operators.ope SeparateOutline, PathFinder.PathFinder, ShowCurveResolution.ShowCurveResolution, + SplinesSequence.ShowSplinesSequence, + SplinesSequence.RearrangeSpline, ] def register(): |