To get the name of an object with only its location

Hello,

At first, I load a random object (located in a second window with all other objects). It comes in the first window at the position 0,0,0 (at the center!).
I know its position but not its name. Is there a python script to find its name?

Then I’d like to move this object, but without the name it seems difficult to me.
I hope I was enough clear.

Thank you for your help

You could loop through all the objects in the scene and look for objects with location being 0,0,0:

for o in bpy.data.objects:
              if o.location.x == 0 and o.location.y == 0 and o.location.z == 0:
                          print(o.name)

There are a couple of ways to detect the new object. The newly added object should be the active object. You should be able to

  • add the object
  • get the active object

That has the name as a property.

Another way is to add the object directly in Python without using the operator. If you do this then when you add, you get a reference to the new object and then you know the name that Blender gave it.

Another trick is to make a list of all the object names, add the object and then see which name was not in the list before.

Those are in addition to trying to find the object at (0,0,0).

I recommend using the active object or adding the object directly.

Do you know the same code but with BGE?

In the BGE the names are not guaranteed to be unique, you have to collect the object and name right after it is added.


import bge

scene = bge.logic.getCurrentScene()
newObj = scene.addObject(...)


The problem is I don’t know which object is download. It’s random
You can see my work on this link:
https://drive.google.com/folderview?id=0B0uRx1cKHvaGekhkYXNFWjJDRmc&usp=sharing
test.py is the script for download the object

That is a lot of code. It would take hours to understand what it means that this script “downloads” an object, but you should consider that line 73 is the only line that calls “scene.addObject”. That is the line that creates a new object and you get to access it immediately to get access to the object reference. In the BGE having a reference is much more important than having the object name since there can be multiple objects with the same name but each reference has to be unique.