Resample added points?

I am adding points on a surface using length stepping modal.

7hJxiZ8uBP

end_loc =  self.hit

if self.start_loc:
 
            
    
    distance = (end_loc - self.start_loc).length
    
    while distance >= self.length:
        
        factor = self.length / distance 
        
        location = self.start_loc.lerp(end_loc, factor) 
        
        self.start_loc = location
        distance -= self.length
        
        
        #if len(self.vertices) == 0:
        vertex = location
   
        
        if vertex is not None: 
            
            self.vertices.append(vertex)

Is there any way to ‘resample’ the points down to 4 points while keeping them evenly distributed along the drawn path(added points)?

Okay, that was puzzling.
But I think I cracked it.

This function takes a list of points as a path and a number of samples to return, and it will integrate over the length of the path to create new points at the right distance, it also returns a list of points.

def resmple(points, n_samples=4, eps=0.0001):
    tot_length = 0
    for i in range(len(points) - 1):
        pt1 = points[i]
        pt2 = points[i + 1]
        tot_length += (pt1 - pt2).length

    cost = tot_length / n_samples
    
    new_samples = []
    wallet = 0
    
    for i in range(len(points) - 1):
        pt1 = points[i]
        pt2 = points[i + 1]
        tip = (pt1 - pt2).length
        wallet += tip

        while (wallet - cost) > -eps:
            wallet -= cost
            fac = -(wallet - tip) / tip
            new_samples.append(pt1.lerp(pt2, fac))

    return new_samples

edit:

whoops, there was a bug in the first code,

it wasnt creating the first point in the correct place,
here’s the fixed function

def resmple(points, n_samples=4, eps=0.0001):
    tot_length = 0
    for i in range(len(points) - 1):
        pt1 = points[i]
        pt2 = points[i + 1]
        tot_length += (pt1 - pt2).length

    cost = tot_length / (n_samples - 1)
    
    new_samples = [points[0]]
    wallet = 0
    
    for i in range(len(points) - 1):
        pt1 = points[i]
        pt2 = points[i + 1]
        tip = (pt1 - pt2).length
        wallet += tip

        while (wallet - cost) > -eps:
            wallet -= cost
            fac = -(wallet - tip) / tip
            new_samples.append(pt1.lerp(pt2, fac))

    return new_samples

import bpy
import bmesh

bm = bmesh.from_edit_mesh(bpy.context.active_object.data)

points = [v.co for v in bm.verts]

for new_point in resmple(points, 4):
    bm.verts.new(new_point)

bmesh.update_edit_mesh(bpy.context.active_object.data)
3 Likes

Awesome, I’ll see if I can implement it in my modal.