Instance Names when Loading same model

What i’m doing is a loop and it loads the same model multiple times assigns it to a name and then displays it where its suppose to display.

However when I do this i’m wondering what the names of the instances are. Kinda of confusing but here is the code:

        while loop_count <= num_planets:
            #Load the planet model and render it
            base.planet = loader.loadModel("eve_rts/models/planets/planet_sphere.egg")

            #Set the coor of the planet

            #Set the planet size
            planet_size = random.uniform(1.0,0.3) #Get random float for planet size
            base.planet.setScale(planet_size,planet_size,planet_size) #Set random planet size uniformally scaled

            #Select a random num for the texture file name (how many textures are in the folder
            #Assign it to the planet then
            texture_num = random.randint(1,30)
            texture_path = "eve_rts/models/planets/textures/" + str(texture_num) + ".jpg"
            texture = loader.loadTexture(texture_path)
            base.planet.setTexture(texture, 1)

            #Reparent the planets to the sun so they spin WEEE

            #Update the counter by 1
            planet_counter = planet_counter + 2
            planet_counter2 = planet_counter2 + 2
            loop_count = loop_count + 1

so now if i do say base.planet.setScale() it scales one of the planets so thats one of the names of the names of one of the planets.

However how do I find the other names of the planets?

And or is there an easier way of loading the same model while giving it a unique name such as planet01, planet02 etc. etc.?


First point. Please don’t add other fields to the base object. In OOP every class should manage a specific task; base does other things, it doesn’t manage planets. :slight_smile: So, you could define a World class that manages your world.

Now your question. Every Python variable contains a value (e.g. 10, True, 3.2, …) or a reference to an object. In this case planet contains a reference to a model. When you reassign it in your while cycle, the old reference is lost. So, you must assign it to a “new” variable, not overriding the old one.

One approach could be to maintain separate variables:

planet1 = loader.loadModel(...)
planet2 = loader.loadModel(...)

If you want to maintain the cyclic approach (or, if you have too many planets), you can hold a list with these references:

planets = []
for ... :
  planets += [ loader.loadModel(...) ]

In this case every planet could be accessed by its index, e.g.

planets[ 5 ].setScale( ... )

If you want to maintain the cyclic approach and you don’t want to hold the planets in a list, but you really want separate variables, you can do like this:

class World:
  def ... ( self, ... ):
    while loop_count <= num_planets:
      setattr( self, 'planet' + str( loop_count ), loader.loadModel( ... ) )

Then you can access the planets via fields, e.g.

world.planet5.setScale( ... )

Obviously there could be other approaches. For example you can navigate the scene graph and retrieve the nodes, but I think these approaches are simpler initially. When you’ll know deeply the scene graph you can solve this problem in more advanced ways.