Give objects different properties when using addObject()

Ok scenario:

I have a script that adds say 7 units to the scene at different locations. Now I need to delete a unit at location 3.

Can I use python to delete the unit at location 3? Since all of the units have the same object name it will delete them all.
or if I try to change the objects property it changes them all!
Maybe just not using the right code?

example



for i in range(7):
    scene.addObject(unit)
    scene.objects['unit']['prop'] += 1


you should assign the added object to a variable and you need another variable to increment the prop cause with the code you posted all the added objects will have the same prop value, something like this:


var=0
for i in range(7):
   add= scene.addObject(unit)
   add['prop'] = var
   var+=1

to delete one of them if you know the location you can do something like this:


from mathutils import Vector
ob_list=[o for o in scene.objects if o.name=="obj_name"]
for i in ob_list:
    if i.worldPosition==Vector([x,y,z]):
        i.endObject()

It should work… at least it think lol

You can do basically what giobbo suggested. But you can use “i” to assign the property like such:

for i in range(7):
   newObj = scene.addObject("Cube")
   newObj['prop'] = i

This is just easier to read in my opinion.

Then when you want to remove the object, you can use the property to get a reference like so:

for obj in scene.objects:
    if obj.name == "Cube" and obj['prop'] == 3:
        obj.endObject()

This would then work even if the object moves.

lol i didn’t understand he wanted to use properties to take the object…

I’ll try this as soon as I can. I have already tried things similar to this. I’ll post back when I do.

you can have an empty that adds the object check a global or a property stored in a item and add to the property /set the property in the object.

in general you should not rely on object names when you create copies of an object. It makes no sense, not just because of the same name, but also, beause they are equal objects. So you need a criteria that makes same comparable to each other.

You wrote you want to differ them by position. At the first sight, this sounds a bit naive to me, as an object can usally move around which changes position during live time. Nevertheless it is a valid discriminator. Unfortunately position is not really precise.

You mention a “location 3”. This sounds handy and is pretty abstract. You need to define what “location 3” is. How you map an object to such a location.

According to your snipet, I guess you mean “the third object I created”. This is completely different and has nothing to do with location. It is a really good idea as you know how many objects you create and it makes the discriminator unique for all of them. But you need to be careful on how to manage them. You need to ensure they remain unique.

A very general approach is an “id” generator that generates unique ids to be used. E.g.

idGenerator.py


availableId= 0

def generateId():
    global availableId
    availableId +=1
    return availableId

Be aware the ids will be unique throughout the game session. This means you get a different number each time you call generateId().

You can use it in your code:


import idGenerator
...

for i in range(7):
    addedObject = scene.addObject(unit)
   addedObject["customId"] = idGenerator.generateId()

This results in 7 objects each with different property “customId” (e.g. 1,2,3,4,5,6,7 or 6013,6014,6015,6016,6017,6018,6019). Be aware the ids will be unique throughout the game session. This means you get a different number each time you call generateId().

How to search for object with customId coantining 3?


objectsWithThree = [object for object in scene.objects if object.get("customId") ==3]
objectWithThree = objectsWithThree[0] # will fail if such an object does not exist

I think it is better to read when you encapsulate it into a function:


def findObjectById(id):
    objectsWithId = [object for object in scene.objects if object.get("customId") ==id]
    return objectsWithId[0] # will fail if such an object does not exist

...
objectWithThree = findObjectById(3)
...

It works using object properties. Thanks!