I'm using mathutils.kdtree.KDTree within a function to find the closest vertex to a particular location, and then extrude it to that exact point.

When I call the function to operate on one mesh it works perfectly, but when I subsequently call the same function to operate on another mesh, it has no effect.

There's no difference between the two meshes that would cause one to fail. I've tried swapping the order, and it's always the second mesh that is unaffected by the function.

I'm wondering whether the KDTree has to be specifically destroyed after use (perhaps when I add the second mesh's vertices it appends them to the tree formed from the first mesh?). I've tried setting it to None at the end of the function, but without success.

I can't find much more detail in the docs.

Here's how the tree is being used:

import mathutils

def add_corner_verts(feature_name):
    mesh = bpy.data.objects[feature_name].data
    size = len(mesh.vertices)
    kd = mathutils.kdtree.KDTree(size)
    for i, v in enumerate(mesh.vertices):
        kd.insert(v.co, i)

    # kd.find() and extrude stuff

    # kd = None # Adding this didn't seem to help

Does anyone know why a KDTree might only work the first time?

  • $\begingroup$ Fixed it. Nothing to do with kdtree after all, but an intervening function I called was changing mesh.select_mode to FACE when add_corner_verts required it to be VERT. Silly me. $\endgroup$ Commented Apr 7, 2016 at 16:37


You must log in to answer this question.

Browse other questions tagged .