[Bf-extensions-cvs] [99585846] master: Curve Tools 2: error correction, adding a spline selection when clicking on a spline

Spivak Vladimir cwolf3d noreply at git.blender.org
Fri Sep 13 02:43:56 CEST 2019


Commit: 9958584685b590d52687e7eab56bea2c55c3c3a1
Author: Spivak Vladimir (cwolf3d)
Date:   Fri Sep 13 03:43:19 2019 +0300
Branches: master
https://developer.blender.org/rBAC9958584685b590d52687e7eab56bea2c55c3c3a1

Curve Tools 2: error correction, adding a spline selection when clicking on a spline

===================================================================

M	curve_tools/PathFinder.py
M	curve_tools/__init__.py

===================================================================

diff --git a/curve_tools/PathFinder.py b/curve_tools/PathFinder.py
index e1b2aa17..801c5293 100644
--- a/curve_tools/PathFinder.py
+++ b/curve_tools/PathFinder.py
@@ -32,6 +32,7 @@ import time
 import threading
 
 import gpu
+import bgl
 from gpu_extras.batch import batch_for_shader
 
 import bpy
@@ -90,33 +91,53 @@ def get_points(spline, matrix_world):
                 x = (spline.points[len_points - 1].co.x + t / 100 * spline.points[0].co.x) / (1 + t / 100)
                 y = (spline.points[len_points - 1].co.y + t / 100 * spline.points[0].co.y) / (1 + t / 100)
                 z = (spline.points[len_points - 1].co.z + t / 100 * spline.points[0].co.z) / (1 + t / 100)
-                point_list.extend(matrix_world @ Vector((x, y, z)))
+                point_list.extend([matrix_world @ Vector((x, y, z))])
             point_list.extend([matrix_world @ Vector((spline.points[0].co.x, spline.points[0].co.y, spline.points[0].co.z))])
-    
     return point_list
 
-def draw_bezier_points(self, context, spline, matrix_world, path_color):
+def draw_bezier_points(self, context, spline, matrix_world, path_color, path_thickness):
     
     points = get_bezier_points(spline, matrix_world)
     
     shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
-    batch = batch_for_shader(shader, 'POINTS', {"pos": points})
+    batch = batch_for_shader(shader, 'LINES', {"pos": points})
     
     shader.bind()
     shader.uniform_float("color", path_color)
+    bgl.glEnable(bgl.GL_BLEND)
+    bgl.glLineWidth(path_thickness)
     batch.draw(shader)
     
-def draw_points(self, context, spline, matrix_world, path_color):
+def draw_points(self, context, spline, matrix_world, path_color, path_thickness):
     
     points = get_points(spline, matrix_world)
     
     shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
-    batch = batch_for_shader(shader, 'POINTS', {"pos": points})
+    batch = batch_for_shader(shader, 'LINES', {"pos": points})
     
     shader.bind()
     shader.uniform_float("color", path_color)
+    bgl.glEnable(bgl.GL_BLEND)
+    bgl.glLineWidth(path_thickness)
     batch.draw(shader)
 
+def near(location3D, point, radius):
+    factor = 0
+    if point.x > (location3D.x - radius):
+        factor += 1
+    if point.x < (location3D.x + radius):
+        factor += 1
+    if point.y > (location3D.y - radius):
+        factor += 1
+    if point.y < (location3D.y + radius):
+        factor += 1
+    if point.z > (location3D.z - radius):
+        factor += 1
+    if point.z < (location3D.z + radius):
+        factor += 1
+        
+    return factor
+
 def click(self, context, event):
     bpy.ops.object.mode_set(mode = 'EDIT')
     bpy.context.view_layer.update()
@@ -128,57 +149,83 @@ def click(self, context, event):
             radius = bpy.context.scene.curvetools.PathFinderRadius
             
             for spline in curvedata.splines:
-                for bezier_point in spline.bezier_points:
-                    factor = 0
-                    co = matrix_world @ bezier_point.co
-                    if co.x > (self.location3D.x - radius):
-                        factor += 1
-                    if co.x < (self.location3D.x + radius):
-                        factor += 1
-                    if co.y > (self.location3D.y - radius):
-                        factor += 1
-                    if co.y < (self.location3D.y + radius):
-                        factor += 1
-                    if co.z > (self.location3D.z - radius):
-                        factor += 1
-                    if co.z < (self.location3D.z + radius):
-                        factor += 1
-                    
-                    if factor == 6:
-                        
-                        args = (self, context, spline, matrix_world, self.path_color)
+                len_bezier_points = len(spline.bezier_points)
+                factor_max = 0
+                for i in range(0, len_bezier_points):
+
+                    co = matrix_world @ spline.bezier_points[i].co
+                    factor = near(self.location3D, co, radius)
+                    if factor > factor_max:
+                                factor_max = factor
                         
-                        self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw_bezier_points, args, 'WINDOW', 'POST_VIEW'))
+                    if i < len_bezier_points - 1:
+                        for t in range(0, 100, 2):
+                            h = Math.subdivide_cubic_bezier(spline.bezier_points[i].co,
+                                           spline.bezier_points[i].handle_right,
+                                           spline.bezier_points[i + 1].handle_left,
+                                           spline.bezier_points[i + 1].co,
+                                           t/100)
+                            co = matrix_world @ h[2]
+                            factor = near(self.location3D, co, radius)
+                            if factor > factor_max:
+                                factor_max = factor
+
+                    if spline.use_cyclic_u and len_bezier_points > 2:
+                        for t in range(0, 100, 2):
+                            h = Math.subdivide_cubic_bezier(spline.bezier_points[len_bezier_points - 1].co,
+                                           spline.bezier_points[len_bezier_points - 1].handle_right,
+                                           spline.bezier_points[0].handle_left,
+                                           spline.bezier_points[0].co,
+                                           t/100)
+                            co = matrix_world @ h[2]
+                            factor = near(self.location3D, co, radius)
+                            if factor > factor_max:
+                                factor_max = factor
 
-                        for bezier_point in spline.bezier_points:
-                            bezier_point.select_control_point = True
-                            bezier_point.select_left_handle = True
-                            bezier_point.select_right_handle = True
+                if factor_max == 6:
+                    args = (self, context, spline, matrix_world, self.path_color, self.path_thickness)
+                    self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw_bezier_points, args, 'WINDOW', 'POST_VIEW'))
+
+                    for bezier_point in spline.bezier_points:
+                        bezier_point.select_control_point = True
+                        bezier_point.select_left_handle = True
+                        bezier_point.select_right_handle = True
             
             for spline in curvedata.splines:
-                for point in spline.points:
-                    factor = 0
-                    co = matrix_world @ Vector((point.co.x, point.co.y, point.co.z))
-                    if co.x > (self.location3D.x - radius):
-                        factor += 1
-                    if co.x < (self.location3D.x + radius):
-                        factor += 1
-                    if co.y > (self.location3D.y - radius):
-                        factor += 1
-                    if co.y < (self.location3D.y + radius):
-                        factor += 1
-                    if co.z > (self.location3D.z - radius):
-                        factor += 1
-                    if co.z < (self.location3D.z + radius):
-                        factor += 1
-                    if factor == 6:
-                        
-                        args = (self, context, spline, matrix_world, self.path_color)
-                        
-                        self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw_points, args, 'WINDOW', 'POST_VIEW'))
+                len_points = len(spline.points)
+                factor_max = 0
+                for i in range(0, len_points):
+                    co = matrix_world @ Vector((spline.points[i].co.x, spline.points[i].co.y, spline.points[i].co.z))
+                    factor = near(self.location3D, co, radius)
+                    if factor > factor_max:
+                        factor_max = factor
+
+                    if i < len_bezier_points - 1:
+                        for t in range(0, 100, 2):
+                            x = (spline.points[i].co.x + t / 100 * spline.points[i + 1].co.x) / (1 + t / 100)
+                            y = (spline.points[i].co.y + t / 100 * spline.points[i + 1].co.y) / (1 + t / 100)
+                            z = (spline.points[i].co.z + t / 100 * spline.points[i + 1].co.z) / (1 + t / 100)
+                            co = matrix_world @ Vector((x, y, z))
+                            factor = near(self.location3D, co, radius)
+                            if factor > factor_max:
+                                factor_max = factor
+
+                    if spline.use_cyclic_u and len_points > 2:
+                        for t in range(0, 100, 2):
+                            x = (spline.points[len_points - 1].co.x + t / 100 * spline.points[0].co.x) / (1 + t / 100)
+                            y = (spline.points[len_points - 1].co.y + t / 100 * spline.points[0].co.y) / (1 + t / 100)
+                            z = (spline.points[len_points - 1].co.z + t / 100 * spline.points[0].co.z) / (1 + t / 100)
+                            co = matrix_world @ Vector((x, y, z))
+                            factor = near(self.location3D, co, radius)
+                            if factor > factor_max:
+                                factor_max = factor
+
+                if factor_max == 6:
+                    args = (self, context, spline, matrix_world, self.path_color, self.path_thickness)
+                    self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw_points, args, 'WINDOW', 'POST_VIEW'))
                         
-                        for point in spline.points:
-                            point.select = True    
+                    for point in spline.points:
+                        point.select = True    
 
 class PathFinder(bpy.types.Operator):
     bl_idname = "curvetools2.pathfinder"
@@ -206,6 +253,7 @@ class PathFinder(bpy.types.Operator):
         
         # color change 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list