Skip to content

Instantly share code, notes, and snippets.

Forked from anonymous/
Created April 25, 2014 13:36
Show Gist options
  • Save zeffii/11289804 to your computer and use it in GitHub Desktop.
Save zeffii/11289804 to your computer and use it in GitHub Desktop.
from node_s import *
from util import *
from mathutils import Vector, Euler, kdtree
import bpy
class SvKDTreeNode(Node, SverchCustomTreeNode):
bl_idname = 'SvKDTreeNode'
bl_label = 'Kdtree search'
def mode_change(self, context):
mode = self.mode
inputs = self.inputs
outputs = self.outputs
socket_map = {
'Verts': 'VerticesSocket',
'Check Verts': 'VerticesSocket',
'proximity Verts': 'VerticesSocket',
'proximity Indices': 'StringsSocket',
'n nearest': 'StringsSocket',
'n proxima .co': 'VerticesSocket',
'n proxima .idx': 'StringsSocket',
'grouped .co': 'VerticesSocket',
'grouped .idx': 'StringsSocket',
'grouped dist': 'StringsSocket'
def prepare_node(expected_inputs, expected_outputs):
io_map = [(inputs, expected_inputs), (outputs, expected_outputs)]
for io_puts, expected_io_puts in io_map:
# remove inappropriate sockets
for socket in io_puts:
if not socket in expected_io_puts:
# add desired sockets
for socket in expected_io_puts:
if not socket in io_puts:
socket_type = socket_map.get(socket, 'StringsSocket'), socket, socket)
standard_inputs = ['Verts', 'Check Verts']
if mode == 'FIND':
expected_inputs = standard_inputs
expected_outputs = ['proximity Verts', 'proximity Indices']
elif mode == 'FIND_N':
expected_inputs = standard_inputs + ['n nearest']
expected_outputs = ['n proxima .co', 'n proxima .idx']
elif mode == 'FIND_RANGE':
expected_inputs = standard_inputs + ['radius']
expected_outputs = ['grouped .co', 'grouped .idx', 'grouped dist']
prepare_node(expected_inputs, expected_outputs)
modes = [
("FIND", " 1 ", "Find nearest", 1),
("FIND_N", " n ", "Find n nearest", 2),
("FIND_RANGE", "radius", "Find within Distance", 3)
mode = bpy.props.EnumProperty(
items=modes, default='FIND', update=mode_change)
def draw_buttons(self, context, layout):
layout.label("Search mode:")
layout.prop(self, "mode", expand=True)
def init(self, context):'VerticesSocket', 'Verts', 'Verts')'VerticesSocket', 'Check Verts', 'Check Verts')
pVerts, pIdxs = 'proximity Verts', 'proximity Indices''VerticesSocket', pVerts, pVerts)'StringsSocket', pIdxs, pIdxs)
def update(self):
inputs = self.inputs
outputs = self.outputs
if not ('Verts' in inputs and inputs['Verts'].links):
verts = SvGetSocketAnyType(self, inputs['Verts'])
if self.mode == 'FIND':
''' [,..] =>
[Verts.idx,.] =>
=> [Main Verts]
=> [cVert,..]
elif self.mode == 'FIND_N':
''' [[,..n],..c] => from MainVerts closest to
[[Verts.idx,..n],.c] => from MainVerts closest to
=> [Main Verts]
=> [cVert,..]
=> [n, max n nearest
elif self.mode == 'FIND_RANGE':
''' [grouped [.co for p in MainVerts in r of v in cVert]] =>
[grouped [.idx for p in MainVerts in r of v in cVert]] =>
[grouped [.dist for p in MainVerts in r of v in cVert]] =>
=> [Main Verts]
=> [cVert,..]
=> n
# SvSetSocketAnyType(self, 'PolyEdge', edges_out)
def update_socket(self, context):
def register():
def unregister():
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment