Skip to main content
removed `print` statement and unused `import`
Source Link
Markus von Broady
  • 42.4k
  • 3
  • 37
  • 107

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from collections import defaultdict
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
individual_offsets = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    individual_offsets.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, found)

# Edit: as @batFINGER mentioned, OP asks to keep the original length of the path:
ratio = current_path_length / offset.length
offset *= ratio

for v, individual_offset in zip(found, individual_offsets):
    print(origin, individual_offset, offset)
    v.co = origin + (individual_offset / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode:

Edit: as pointed by batFINGER, this didn't keep the length of the path, so I added a small update:

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from collections import defaultdict
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
individual_offsets = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    individual_offsets.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, found)

# Edit: as @batFINGER mentioned, OP asks to keep the original length of the path:
ratio = current_path_length / offset.length
offset *= ratio

for v, individual_offset in zip(found, individual_offsets):
    print(origin, individual_offset, offset)
    v.co = origin + (individual_offset / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode:

Edit: as pointed by batFINGER, this didn't keep the length of the path, so I added a small update:

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
individual_offsets = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    individual_offsets.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, found)

# Edit: as @batFINGER mentioned, OP asks to keep the original length of the path:
ratio = current_path_length / offset.length
offset *= ratio

for v, individual_offset in zip(found, individual_offsets):
    v.co = origin + (individual_offset / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode:

Edit: as pointed by batFINGER, this didn't keep the length of the path, so I added a small update:

update to keep the length of the path, small stylistic code improvements
Source Link
Markus von Broady
  • 42.4k
  • 3
  • 37
  • 107

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from collections import defaultdict
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
distancesindividual_offsets = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    distancesindividual_offsets.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, (vfound)

# forEdit: vas in@batFINGER foundmentioned, OP asks to keep the original length of the path:
ratio = current_path_length / offset.keys()))length
offset *= ratio

for iv, vindividual_offset in enumeratezip(found.keys(), individual_offsets):
    print(origin, distances[i]individual_offset, offset)
    v.co = origin + (distances[i]individual_offset / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode:

Edit: as pointed by batFINGER, this didn't keep the length of the path, so I added a small update:

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from collections import defaultdict
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
distances = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    distances.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, (v for v in found.keys()))

for i, v in enumerate(found.keys()):
    print(origin, distances[i], offset)
    v.co = origin + (distances[i] / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode:

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from collections import defaultdict
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
individual_offsets = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    individual_offsets.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, found)

# Edit: as @batFINGER mentioned, OP asks to keep the original length of the path:
ratio = current_path_length / offset.length
offset *= ratio

for v, individual_offset in zip(found, individual_offsets):
    print(origin, individual_offset, offset)
    v.co = origin + (individual_offset / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode:

Edit: as pointed by batFINGER, this didn't keep the length of the path, so I added a small update:

Source Link
Markus von Broady
  • 42.4k
  • 3
  • 37
  • 107

I see I'm late, but here's my take on this problem:

import bpy, bmesh
from collections import defaultdict
from functools import reduce
from mathutils import Vector

mesh = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

found = {}  # key: vertex object, value: number of connected vertices, which are selected

def recursive_add(found_vertex):
    found[found_vertex] = 0
    for edge in found_vertex.link_edges:
        for vertex_candidate in edge.verts:
            if vertex_candidate is not found_vertex and vertex_candidate.select:
                found[found_vertex] += 1
                if vertex_candidate not in found:
                    recursive_add(vertex_candidate)

recursive_add(active)
ends = [vertex for vertex, connections in found.items() if connections == 1]
origin = ends[0].co
offset = ends[1].co - origin
distances = [0]
current_path_length = 0

def analyze_path(a, b):
    global current_path_length
    distance = (a.co - b.co).length
    distances.append(current_path_length + distance)
    current_path_length += distance
    return b

reduce(analyze_path, (v for v in found.keys()))

for i, v in enumerate(found.keys()):
    print(origin, distances[i], offset)
    v.co = origin + (distances[i] / current_path_length) * offset

bmesh.update_edit_mesh(mesh)

at least one of selected vertices has to be active, and it only works in vertex selection mode: