-
-
Save zeffii/11333394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, but why tuple? Why not leave lists, that is usual thing. Tuples could be needed on other case maybe.
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
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