Script to find touching objects

I need a script which can tell me the names of objects which are in close proximity to a particular object I have selected. What would be the best way to write such a script?

Iterate through all objects and calculate the distance between each one and the selected object.

How good is your scripting? Make a start on it and I’ll help you in this thread if you get stuck.

Thanks for the reply. How do I calculate the distance b/w 2 objects? Is that distance b/w the objects’ centers or something else? I am new to scripting in Blender, so I don’t know much about it.

You can find the distance between two centres (this is the easiest and quickest way).

This is going to be the magnitude (length) of the vector between the two centres.

Have you done any programming before? Anything in python?

By center of object I take it you mean the loc property of the object. If I get the vector distance from the centers, how do I then get the objects which touch a selected object? For a spherical object, I can get this through the sum of the radii of the 2 objects, but what do I do in the case of non-spherical objects?
I have done Java, a bit of C and Python outside of Blender.

If you want to to figure out if a mesh of given object is inside another object (ie. “touches” it), you can use ray casting. One pretty standard way to check if a vertex of mesh object is inside another is to cast a ray. This is guaranteed to work only if you are dealing with a manifold object. Manifold means that you can explicitly state which is the outside and which is the inside of an object.

Let’s say you want to see if v1 (vertex) is inside some another object that we can call o. Pick some vertex of o and call it as v2. Now imagine a ray starting from v1 and going through v2. Next we need to check if this ray passes through the object should we give it some s (>0.0) to scale it as this is quite possible. If the amount of passes is odd, we can conclude that v1 is inside the mesh. Otherwise it’s outside.

You can handle the checking part (ray * s) per face for instance. It may be beneficial to split this even to simpler cases in which you check triangles. If you want, I can give some tips how to do this. It may be rewarding to try to figure it out on paper first though. :slight_smile:

Thanks for the suggestion. I would like to give this method a try as it seems to do what I want. Could you tell me how to create the infinitely long ray passing through v1,and v2(Scaling wouldnt work for some cases, what if the entire scaled ray was inside the object but never got to cross it?) and how to check if it crosses the object again?

I just noticed there’s a premade function you can use at the API. See . It handles all the math for you. Read the description well.

To answer the latter question just go through each face and accumulate amount of passes. Then finally if the amount is odd, you can conclude that the given vertex is inside.

Yeah that seems to work. The problem I am now facing is its not too efficient, and takes a pretty long time and lots of overall operations to complete the task. Could you suggest something quicker and more efficient to reduce the time and memory it takes?

There are two main ways to handle collision checking. First way is to use so called broad checks in which you use bounding boxes for instance. It’s fast and easy to check if a box is inside another. If it is, it’s possible that the meshes of the objects intersect too. Then we use narrow, real check.

Here are a couple of in game collision detection related links which I hope you find useful:

As in this case we are not dealing with moving objects, you can find listing Static Object Intersections available at particularly useful.