How do i add multiple objects in script ?

I’m trying to add about 145 different objects with a python script in blender game engine .
I dont realy like to do :

Is there a better way ?
Thanks in advanced .

If the objects all start with ‘’ then you could do something like:

for obj in logic.getCurrentScene().objects:

    if 'sphere' in

        # do something with obj here

But this is only useful if your objects all start with the name ‘sphere’. This could also be changed to look for all the objects with a certain property. Just change the “if ‘sphere’ in” to “if ‘sphere’ in obj:” and you can loop through all the objects with the property ‘sphere’.

Thanks man this is awesome ! what command can i put in there that get all these objects approachable in the script ? usualy i have to assign each their own name like " sphere1 = scene.objects[‘sphere.001’] and so on… "

like :
sphereX = …
X = x+1
shereX = …

Rather than name check you do property check. In the above link you find a code sample with a function that does exactly what you want. It should not be to difficult to find it. You can feed it with the list of objects from klauser’s code snippet.

I don’t see why you need to assign each object to a variable. Why not just put all the object in a list and use list notation to access the object you want.

spheres = [obj for obj in scene.objects if 'sphere' in]

# sort list by object name
spheres.sort(key=lambda obj:

# itterate through list
for i in range(len(spheres)):
    # this is the ith sphere, do whatever you want with it
    obj = spheres[i]
    obj['index'] = i

As Mobious said, you should look into using a list or a dictionary to store all the variables. Storing each of them in their own variable will very quickly become unmanageable and you end up with a very messy and inefficient script.

If your slightly new to python, I would recommend taking a look at Goran Milovaovic’s youtube channel. Lots of bge tutorials with a huge focus on doing everything the pythonic way.

Could i have the full code for that.

in cases like this it is very beneficial to have a clear naming convention on all your objects so you can make effective searches in python (or in the outliner). When making a decently complex game you can have a couple hundred of objects, but when half of them are just named cube.0xx you dont really know what is what.
E.g. I use prefixes like PHY.<name> LOC.<name> GEO.<name> to indicate what the purpose of the object is, then have in the name more distinctions like GEO.rock.mossy.V1.001 so i can easily find objects and categories of them.
the ‘in’ is quite powerful in python and you can use it in a lot of cases where you search for objects of a certain kind when you set your names up well.
Also as mobius points out, using lists instead of single variables is a lot better for organization. lists automatically get larger when you add more to them and you can even use ‘in’ again on the list to find certain entries in them.

Of course, I’m just not too sure what you mean. If you mean the looking for objects with a property then:

for obj in logic.getCurrentScene().objects:

    if 'sphere' in obj:

        # Do something with the object, maybe add it to a list or attach a class to it

Let me know if thats not what you mean. :slight_smile:

Thank you all !
I managed to do it this way :

import random
import bge

def main():

cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

own.applyMovement((0,1,0), True)

particle = scene.objectsInactive['Particle']
counter = random.randint(0,1)

for obj in scene.objects :
    if "Sphere" in and counter == 1 :
        scene.addObject(particle,, random.randrange(5,10))


just a fireball that supposed to leave particles randomly after itself :slight_smile:
This is 145 objects parented to one moving as a fireball .

There is a problem though, when the fireball is created in the scene ( addobject by some other object ) the particles are created in the original fireball place and not following the new fireball object, what should i do ? :open_mouth:

Ok i fixed it by changing this :
scene.addObject(particle,, random.randrange(5,10))
to this :
scene.addObject(particle, obj, random.randrange(5,10))


scene.addObject(particle,, random.randrange(5,10))