I am trying to check if an imported obj file is colliding with others by trying tu use the closest_point_on_mesh function. Here is the loop:

    coords = [(the_object.matrix_world * v.co) for v in the_object.data.vertices]
    for p_obj in list_of_passive_bodies:
        for c in coords:
            mesh_inside = is_inside(c,p_obj.name,max_dist=0.1)
            if mesh_inside == True:

and the function is_inside:

 def is_inside(p,obj,max_dist= 1.84467e+19):
        point, normal, face = obj.closest_point_on_mesh(p, max_dist)
        p2 = point-p
        v = p2.dot(normal)
        return not(v < 0.0)

coords is the list of vertices of the imported mesh from the obj file and list_of_passive_bodies is the list of already existing objects in the scene.

For example, p_obj returns :

bpy_struct, Object("Blob.obj_0_Blob.002")

and c returns :

Vector (-7.2836, 2.1763, 5.8258)

I constantly get a

ValueError: too many values to unpack (expected 3)


Look at the documentation of closest_point_on_mesh: https://docs.blender.org/api/2.79/bpy.types.Object.html#bpy.types.Object.closest_point_on_mesh

Return (result, location, normal, index):

  • result, boolean
  • location, The location on the object closest to the point, float array of 3 items in [-inf, inf]
  • normal, The face normal at the closest point, float array of 3 items in [-inf, inf]
  • index, The face index, -1 when original data isn’t available, int in [-inf, inf]

So the function is returning 4 values, but you only unpack 3. It should be called like this:

result, point, normal, face = obj.closest_point_on_mesh(p, max_dist)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.