Hi,
First: how can i find any examples about :
how to use some blender scripts function? Because i find nothing with Qwant, Google, inside the Blender code or the API. Second my problem is :
I can’t find the sequence type of the BVHTree.FromPolygons arguments. https://www.blender.org/api/blender_python_api_current/bpy.types.Mesh.html#bpy.types.Mesh
(Ok i don't use the BBCode code, font white on the background white)
TypeError: BVHTree.FromPolygons Error:
Python script fail, look in the console for now…
mesh = obj.data
vertices = []
indicies = []
for i, v in enumerate(mesh.vertices):
vertex = v.co * obj.matrix_world
vertices.append(vertex.x)
vertices.append(vertex.y)
vertices.append(vertex.z)
indicies.append(i)
Sim.bvh_trees.append(mathutils.bvhtree.BVHTree.FromPolygons(vertices, indicies))
I have try :
vertices.append(vertex)
vertices.append((vertex.x, vertex.y, vertex.z))
vertices.append([vertex.x, vertex.y, vertex.z])
Have you any idea to use this function ? Thanks a lot.
Ok i have find the solution :
<b>def </b><b>update_bvh_tree</b>():
Sim.bvh_trees.clear()
# https://www.blender.org/api/blender_python_api_current/mathutils.bvhtree.html
# https://blender.stackexchange.com/questions/45827/how-to-check-if-two-meshes-intersect-in-python-with-bvh-tree
scene = Sim.get_scene()
types = ['MESH', 'SURFACE'] # , 'META']
<b>for </b>obj <b>in </b>bpy.data.objects:
<b>if </b>obj.type <b>in </b>types: # and (not obj.is_visible(scene)): # is_visible doesn't works at all ... kill -9 blender devs
mesh = obj.data
vertices = []
indicies = []
# https://www.blender.org/api/blender_python_api_current/bpy.types.Mesh.html#bpy.types.Mesh
# https://www.blender.org/api/blender_python_api_2_75_release/bpy.types.Mesh.html
<b>for </b>i, v <b>in </b>enumerate(mesh.vertices):
vertex = obj.matrix_world * v.co
vertices.append(vertex.xyz)
<b>for </b>poly <b>in </b>mesh.polygons:
poly_indices = []
<b>for </b>loop_index <b>in </b>range(poly.loop_start,
poly.loop_start + poly.loop_total):
poly_indices.append(mesh.loops[loop_index].vertex_index)
indicies.append(poly_indices)
Sim.bvh_trees.append(mathutils.bvhtree.BVHTree.FromPolygons(vertices, indicies))
This function place all the scene geometrie in a bvhtree to be use like that :
<b>def </b><b>find_nearest</b>(position, distance=sys.float_info.max):
# https://www.blender.org/api/blender_python_api_current/mathutils.bvhtree.html
min_distance = distance
min_position = mathutils.Vector((0, 0, 0))
nearest_function = 0
<b>if </b>nearest_function == 0:
<b>for </b>bvh_tree <b>in </b>Sim.bvh_trees:
bvhtree_result = bvh_tree.find_nearest(position)#, distance)
<b>if </b>bvhtree_result[0] <b>is not None</b>:
bvhtree_position = bvhtree_result[0]
bvhtree_distance = bvhtree_result[3]
<b>if </b>bvhtree_distance < min_distance:
min_distance = bvhtree_distance
min_position = bvhtree_position