Add multiple objects to Terrain using empty nodes

I created a terrain in blender (using Blender 2.78 cause I’m poor and my system was made around the time of Christ lol). I created a bunch of empty dupli nodes and now I want to fill those nodes with my tree models…keeping it simple and using just one model.

Here is my issue. Besides being new to programming in general( I’m a systems engineer did mostly server scripting and a few small programs to work with AS/400 and the such) I am not sure how to run a loop to check for each empty node since they are names like empty.001, empty.002…etc. I know Im going about this wrong or least proficient way but this is how I have added the trees before:

    treePos= self.environ.find ("**/Empty.001").getPos()
    tree = loader.loadModel("env/trees/simplepine")
    tree.reparentTo(render)
    tree.setPos (treePos)

    treePos2= self.environ.find ("**/Empty.002").getPos()
    tree2 = loader.loadModel("env/trees/simplepine")
    tree.reparentTo(render)
    tree.setPos (treePos2)

But I really dont want to add the other 76 this way lol. How do you code it to check the .egg file for each instance of the empty and the add the object to the empty node? Been cracking my head over this for a bit.

Do this:

for i in range(1, 77):
    treePos= self.environ.find ("**/Empty.00" + i).getPos()
    tree = loader.loadModel("env/trees/simplepine")
    tree.reparentTo(render)
    tree.setPos (treePos)

Thanks for the fast reply :), That is what I was looking for.
I will give it a try later on,about to hit the sack now. I will let you know how it goes
again thank you very much

1 Like

Let me add that there’s an easier way: the “findAllMatches” method should produce a collection of nodes that match its pattern, and wildcards should allow one to specify only the relevant part of the file-name.

Something like this:

treePosCollection = self.environ.findAllMatches("**/Empty*")
for treePosNP in treePosCollection:
    treePos = treePosNP.getPos()
    tree = loader.loadModel("env/trees/simplepine")
    tree.reparentTo(render)
    tree.setPos(treePos)

This also has the advantage that you don’t need to know exactly how many objects there are–adding or removing models should leave the code yet working–and there should be no problem with gaps in the numbering.

@atomicsniperdude , hope it works! Note I made a mistake in the code I sent you, instead of doing:

treePos= self.environ.find ("**/Empty.00" + i).getPos()

do:

treePos= self.environ.find ("**/Empty.00" + str(i)).getPos()

I wanted to suggest that, but if he/she knew the concept of loops, it could help him in other places too.

Ah, fair enough. Still, I think that it’s worth mentioning the way that I suggested, as it is more convenient and more reliable, I believe.

1 Like

You also have technically used a loop :slight_smile: :slight_smile: :slight_smile:

This is also true! I’d actually forgotten that I did! ^^;

1 Like

You can also add a Game Property called “model” and search for it like so:
findAllMatches('**/=model'))
Then use getTag to extract the value of that tag and load the model contained in that value.

1 Like

Hey guys, thanks for all the reply’s back. And yeah I tested the script around 4am this morning lol, and I had noticed the error about concatenate, so I had changed it to str(i). And it was working Then logged on to here and found the messages lol. I’m about to try findAllMatches.

Like I said before Im really new to this side of programming. Coding with python is a lot different to programming with Cobalt (sort of) lol. I am teaching myself from sctratch and I really do thank you for showing me with the loops. Because like panda3dmastercoder said knowing the concept really helps. And it will help me in the other places :).

I started writing a game mostly because I wanted to make a game for the wife as she was getting tired of the pay to play crap and our system was too old for games like 7-days to die and such. So if I get the bugs worked out on the low-end system now and get a good concept going, I am sure I can make a much better game with higher graphic on a better system :).

2 Likes

Thank you Bud, it worked perfectly, so simple and easy to do. Now that I have the terrain working how I want I can start with the rest of the mechanics :).

1 Like

I’m glad to read it! I hope then that the rest of the mechanics go well! :slight_smile:

Once I get something together I will throw it up to Git :slight_smile:

1 Like

I am glad to see it worked. Why I told you to put str(i) is because i was of type int and python can’t concatenate string and int. So str just converts the int to type str. Hope it helps.

Yeah it helped quit a lot. :slight_smile: