Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/node_KDTreeEdges.py
Created April 26, 2014 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/11333394 to your computer and use it in GitHub Desktop.
Save zeffii/11333394 to your computer and use it in GitHub Desktop.
from node_s import *
from util import *
from mathutils import Vector, kdtree
import bpy
# documentation/blender_python_api_2_70_release/mathutils.kdtree.html
class SvKDTreeEdgesNode(Node, SverchCustomTreeNode):
bl_idname = 'SvKDTreeEdgesNode'
bl_label = 'Kdtree Edges'
bl_icon = 'OUTLINER_OB_EMPTY'
def draw_buttons(self, context, layout):
pass
def init(self, context):
self.inputs.new('VerticesSocket', 'Verts', 'Verts')
self.inputs.new('StringsSocket', 'minDistance', 'minDistance')
self.inputs.new('StringsSocket', 'maxDistance', 'maxDistance')
self.inputs.new('StringsSocket', 'maxNum', 'maxNum')
self.outputs.new('StringsSocket', 'Edges', 'Edges')
def update(self):
inputs = self.inputs
outputs = self.outputs
# if not ('Verts' in inputs and inputs['Verts'].links):
# return
try:
verts = SvGetSocketAnyType(self, inputs['Verts'])[0]
mindist = SvGetSocketAnyType(self, inputs['minDistance'])[0][0]
maxdist = SvGetSocketAnyType(self, inputs['maxDistance'])[0][0]
maxNum = SvGetSocketAnyType(self, inputs['maxNum'])[0][0]
# print(mindist, maxdist)
linked = outputs[0].links
except (IndexError, KeyError) as e:
return
# make kdtree
# documentation/blender_python_api_2_70_release/mathutils.kdtree.html
size = len(verts)
kd = mathutils.kdtree.KDTree(size)
for i, vtx in enumerate(verts):
kd.insert(Vector(vtx), i)
kd.balance()
# makes edges
e = []
maxNum = max(maxNum, 1)
for i, vtx in enumerate(verts):
num_edges = 0
for (co, index, dist) in kd.find_range(vtx, maxdist):
if i == index or (num_edges > maxNum):
continue
if dist < mindist:
continue
e.append([i, index])
num_edges += 1
SvSetSocketAnyType(self, 'Edges', e)
def update_socket(self, context):
self.update()
def register():
bpy.utils.register_class(SvKDTreeEdgesNode)
def unregister():
bpy.utils.unregister_class(SvKDTreeEdgesNode)
if __name__ == "__main__":
register()
@ly29
Copy link

ly29 commented Apr 27, 2014

Wrap the output edges on more time.

How about avoiding duplicate edges?
if e is a set
e.add(tuple(sorted([i,index])))

We could write or own edgSort to keep index in order if the overhead is to large.

It would also be nice to try to prune extra edges at mesh border for non cyclic meshes. That is of course an advanced topic

@nortikin
Copy link

@ly29, but why tuple? Why not leave lists, that is usual thing. Tuples could be needed on other case maybe.

@ly29
Copy link

ly29 commented Apr 27, 2014

A set can't have lists as members since they are mutable. Afterwards we can use list(map(list,e)) to change to lists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment