# Creating multiple RigidbodyNodes with a Loop?

I am currently in need to create 20+ rigidbodynodes for my simulation that are placed equally apart from each other. Instead of copying the same parts of code, I want to create a loop that generates a number of nodes and places them apart by iterating a variable for its position.

Does anyone have experience doing this kind of loop? I have tried out multiple things like:

``````    l=0
while l < 6:
i = [1,2,3,4,5]
self.modelnodeB[i] = self.worldNP.attachNewNode(BulletRigidBodyNode("modelA"))
``````

or

`````` [self.modelnodeB + I] = self.worldNP.attachNewNode(BulletRigidBodyNode("modelA"))
``````

but I am too inexperienced in python to find the right solution. Is it possible to generate multiple objects with a loop anyway?

It should be very possible, indeed!

First of all, what is âself.modelnodeBâ? Is it a list of some sort? And what is it that you are trying to do in passing in the list âiâ to it in your first excerpt, or by running â[self.modelnodeB + I]â in your second?

Second, a simple way to do this might be to use a âforâ-loop. If youâre not familiar with these, they essentially loop a given number of times. Something like this:

``````# "range" is a built-in Python method that produces a result that
# Python can iterate over in a loop.
for i in range(20): # Loop 20 times
# <Generate objects here>
# As a side-note, you can also use for-loops with lists,
# in which case you just put the list in place of the
# "range(20)" used above.
``````

Simply what I tried to do with both of these code lines was to create a number of different rigidbodynodes in a loop:

self.modelnodeB1
self.modelnodeB2
âŚ
self.modelnodeB"x"

The way I tried it was either by applying the number opf the loop (example 1) o by running through a list (example 2). Both ways didnât work and gave me an error message of " âSimulationâ object has no attribute 'modelnodeB"

The information with the loop function is really helpful and a lot easier than what I was doing, thank you for that!
But the main problem I am facing is, how I can iterate the name of the object (RgidBodyNode) per loop cycle?

Ah, I see, I believe.

The thing is, what you put on the left-hand side of a single equals-sign is taken by the program to be the variable to which you want to assign whatever you have on the right-hand side of the equals-sign.

Thus, in your first excerpt, you were assigning the result of the call to âattachNewNodeâ to to i-th item in a variable named âself.modelnodeBââand if you hadnât yet created anything called âself.modelnodeBâ, then the program would fail as a result.

Do you mean the internal node-name (i.e. what you currently have as âmodelAâ)? If so, then you can use string-methodsâa simple way would be something like this:

``````for i in range(20):
name = "modelA" + str(i)
``````

That should take the value in âiââwhich should update with each run of the âforâ-loopâthen convert it into a string, and then append that string to âmodelAâ. The result, if printed in each iteration, might be something like this:

``````modelA0
modelA1
modelA2
# And so on...
``````

(There are arguably-better ways, but those are more complex, and Iâd rather stick to simple approaches for the moment.)

If, however, you mean the name of the variable in which you store the result of the call to âattachNewNodeâ, then thatâs a little more complicated.

Defining a new variable each iteration is possible, but a little complex.

However, a relatively-simple approach might be to store your results in a list, adding to the list via the âappendâ method. Something like this:

``````# define the list before the loop
self.myList = []

for i in range(20):
newNodePath = self.worldNP.attachNewNode(BulletRigidBodyNode("modelA"))
self.myList.append(newNodePath)
``````

You can then access these items via their index. Something like this:

``````someObject = self.myList[2]

# "len" is a built-in method that returns the length
# of a list or other such structure
for i in range(len(self.myList)):
print (self.myList[i])
``````

That said, when iterating in a loop, itâs perhaps simpler to just iterate over the list itself. Something like this:

``````for someObject in self.myList:
print (someObject)
``````

Yes, I was referring to the variable name, in which the result is stored.

I implement ur code now and its works perfectly!
Thank you so much for your help!

1 Like