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:

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


 [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:



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:

# 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"))

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