Scene.addObject Question

Hello everyone,

Hopefully this will be a simple problem to address! I am adding a series of objects into the active layer using the scene.addObject function, and I want to add each subsequent object at the origin of the previous object. However, it appears that when an object is added using the scene.addObject function, that object’s position information is not updated to reflect its new position in the active layer, which causes the subsequent object to be added at the location of the previous object’s origin in the non-active layer. At least, this is what appears to be happening. Is there a way to update an object’s position information after it has been added to the active layer?

Thank you in advance for your help! :slight_smile:

Try something like this:


if 'previousObjectPosition' not in owner:
    owner['previousObjectPosition'] = None
object = scene.addObject(...)
if owner['previousObjectPosition'] != None:
    object.worldPosition = owner['previousObjectPosition'].copy()
else:
    object.worldPosition = [...]
lastObject = object
owner['previousObjectPosition'] = lastObject.worldPosition

If you do someObject.worldPosition = otherObject.worldPosition, you might run into problems
with someObject updating its position every time otherObject moves.

EDIT: I’ve edited the code above.

this starts it with the original object then adds an object, then makes the last object added the “adder” object. Theres a blend example


own["adder"] = own
if own["timer"] > 1.00:
    lastobject = scene.addObject("Sphere", own["adder"], 0)
    own["adder"] = lastobject
    own["timer"] = 0


addo.blend (430 KB)

So if i am reading right you want to spawn objects at a location.
This is easy done if you make a reference to the last added object, and use that to set a new_position.


def add_object(cont):
    
    own     = cont.owner
    scene   = own.scene
    
    #the object you want to spawn
    object      = 'an_object'
    
    #lifetime of the spawned object
    time        = 0
    
    #spawn object from this location
    spawn_from  = own
    
    #the new location, if you have locations use it here
    new_location = own.worldPosition
    
    #adding the object, and at the same time make reference to it.
    added_object = scene.addObject(object, spawn_from, time)
    
    #set a new position for the added_object
    added_object.worldPosition = new_location

I just had to adapt superflip’s convenient blend file:
addFromLastObject.blend (469 KB)

I think this is what you’re looking for. Notice that the spheres add other spheres when they are the last objects added. Thanks to superflip for his blend file.