Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

July 03, 2011

Sorting Edge keys Part II

mostly rewritten, this is currently prototype so clunky/verbose code. but works ! :) assumes the polyline (edgebased) object is
1) not closed,
2) not interupted,
3) not already correctly sorted



import bpy

print("\n")
print("="*50)

cobject = bpy.context.active_object

idx_vert_list = []
for i in cobject.data.vertices:
idx_vert_list.append([i.index, i.co])
# print(i.co, i.index)

for i in idx_vert_list:
print(i)


# existing edges
print("=== +")
ex_edges = []
existing_edges = []
for i in cobject.data.edges:
edge_keys = [i.vertices[0], i.vertices[1]]
ex_edges.append(edge_keys)
item = [i.index, edge_keys]
existing_edges.append(item)
print(item)


# proposed edges
print(" becomes")
proposed_edges = []
num_edges = len(existing_edges)
for i in range(num_edges):
item2 = [i,[i,i+1]]
proposed_edges.append(item2)
print(item2)


# find first end point, discontinue after finding a lose end.
current_sequence = []
iteration = 0
while(iteration <= num_edges):
count_presence = 0
for i in existing_edges:
if iteration in i[1]:
count_presence += 1

print("iteration: ", iteration, count_presence)
if count_presence == 1:
break
iteration += 1

init_num = iteration
print("end point", init_num)


# find connected sequence
seq_list = []
glist = []

def generate_ladder(starter, edge_key_list):

def find_vert_connected(vert, mlist):
if len(mlist) == 1:
for g in mlist:
for k in g:
if k is not vert:
return(k, -1)

for i in mlist:
if vert in i:
idx = mlist.index(i)
for m in i:
if m is not vert:
return(m, idx)

stairs = []
while(True):
stairs.append(starter)
starter, idx = find_vert_connected(starter, edge_key_list)
if idx == -1:
stairs.append(starter)
break
edge_key_list.pop(idx)
return(stairs)

seq_list = generate_ladder(init_num, ex_edges)


# make verts and edges
Verts = []
Edges = []

for i in range(len(idx_vert_list)):
print(i)
old_idx = seq_list[i]
myVec = idx_vert_list[old_idx][1]
Verts.append((myVec.x, myVec.y, myVec.z))

for i in Verts: print(i)

for i in proposed_edges:
Edges.append(tuple(i[1]))
print(Edges)

bpy.ops.object.mode_set(mode = 'OBJECT')

prof_mesh = bpy.data.meshes.new("test_mesh2")
prof_mesh.from_pydata(Verts, Edges, [])
prof_mesh.update()
cobject.data = prof_mesh

bpy.ops.object.mode_set(mode = 'EDIT')

This code inspects the edges/verts, strings them in the correct order, makes a new mesh, replaces the current object.data (mesh) with it.


#terminal output.
==================================================
[0, Vector((1.0, 0.9999999403953552, 0.0))]
[1, Vector((0.9999999403953552, -0.9999999403953552, 0.0))]
[2, Vector((-1.0000001192092896, -0.9999998211860657, 0.0))]
[3, Vector((-0.9999996423721313, 1.0000003576278687, 0.0))]
[4, Vector((1.0, 0.0, 0.0))]
=== +
[0, [1, 2]]
[1, [2, 3]]
[2, [0, 4]]
[3, [1, 4]]
becomes
[0, [0, 1]]
[1, [1, 2]]
[2, [2, 3]]
[3, [3, 4]]
iteration: 0 1
end point 0
[0, 4, 1, 2, 3]
(1.0, 0.9999999403953552, 1.0)
(1.0, 0.0, 1.0)
(0.9999999403953552, -0.9999999403953552, 1.0)
(-1.0000001192092896, -0.9999998211860657, 1.0)
(-0.9999996423721313, 1.0000003576278687, 1.0)
[(0, 1), (1, 2), (2, 3), (3, 4)]
looks like

1 comment:

  1. Hi, nice work. I've also created a script to sort edge keys, but for cyclic polygons. So closed, not interrupted and have only one face. Hopefully it helps someone, the script can be copy & pasted from here: http://vizalac.blogspot.nl/2014/01/algorithm-to-sort-edge-list-of-cyclic.html

    ReplyDelete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.