Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2014-01-06 13:32:34 +0400
committerCampbell Barton <ideasman42@gmail.com>2014-01-06 13:32:34 +0400
commit4848f9029a7cf70324888f18ccfcdea3fa50392b (patch)
tree4a7735e561efaa96240756c9c585e891315bc24f /doc/python_api/examples/mathutils.kdtree.py
parent90efa345c2b512c7dbc615b6210127070ff8b03f (diff)
Patch D133: Python wrapper for BLI_kdtree (adds mathutils.kdtree)
Originally by Dan Eicher, with my own fixes and adjustments (see patch page for details). For details there are unit tests and api example usage. doc/python_api/sphinx-in-tmp/menu_id.png
Diffstat (limited to 'doc/python_api/examples/mathutils.kdtree.py')
-rw-r--r--doc/python_api/examples/mathutils.kdtree.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/doc/python_api/examples/mathutils.kdtree.py b/doc/python_api/examples/mathutils.kdtree.py
new file mode 100644
index 00000000000..d367582e5be
--- /dev/null
+++ b/doc/python_api/examples/mathutils.kdtree.py
@@ -0,0 +1,37 @@
+import mathutils
+
+# create a kd-tree from a mesh
+from bpy import context
+obj = context.object
+
+# 3d cursor relative to the object data
+co_find = context.scene.cursor_location * obj.matrix_world.inverted()
+
+mesh = obj.data
+size = len(mesh.vertices)
+kd = mathutils.kdtree.KDTree(size)
+
+for i, v in enumerate(mesh.vertices):
+ kd.insert(v.co, i)
+
+kd.balance()
+
+
+# Find the closest point to the center
+co_find = (0.0, 0.0, 0.0)
+co, index, dist = kd.find(co_find)
+print("Close to center:", co, index, dist)
+
+
+# Find the closest 10 points to the 3d cursor
+print("Close 10 points")
+for (co, index, dist) in kd.find_n(co_find, 10):
+ print(" ", co, index, dist)
+
+
+# Find points within a radius of the 3d cursor
+print("Close points within 0.5 distance")
+co_find = context.scene.cursor_location
+for (co, index, dist) in kd.find_range(co_find, 0.5):
+ print(" ", co, index, dist)
+