Create objects with specific positions.

Hi! I’m working on a simulation using Blender & Python. The thing is that i need to add objects to the scene in positions according to the results of some calculations made by a python script. Is there a way to do that? Also is there a way to eliminate those objets so i can create them again in new positions? Thanks in advance!!

1:Set up your logic bricks on an empty like this:
(Whatever sensor)-------->Python(script name)------->Edit Object(Add object)
name the edit object actuator “addObj” (no quotes)


g = GameLogic
cont = g.getCurrentController()
own = cont.getOwner()

own.setPosition([0,0,0])
#the numbers in the list [0,0,0] represent global coordinates. First number is for X-axis, second for Y-axis and third Z-axis.
#Change these numbers to whatever you want

add = cont.getActuator("addObj")
#Find the actuator named "addObj"
g.addActiveActuator(add,1)
#Turn on the actuator. Turning it off would be g.addActiveActuator(add,<b>0</b>)

This will make the empty move to position [0,0,0] or whatever you set it as, then add an object.

2:You don’t need to destroy them to put them in a new position, just set their position straight away with the part of the script above that goes like this:

 own.setPosition([0,0,0])

Hope that helps.

Thanks for the help! It works but i still have one problem. I’m trying to create multiple objects calling the python file only one time using a for cycle and i can’t make it work. The positions are decided by random numbers but only one object appears. If i call the file again it works but i need to do it calling it just once.

then youre looking for this bit of code


addObjectActuator.instantAddObject()

That will activate the atuatuator for each line that is read so for multiple addings just put it in a for loop ie


for x in range(10):
    addObjectActuator.instantAddObject()

this will add 10 objects in one run of the script

hope it helps :wink:

It works like a charm.
Thanks :wink: