20Feb/0911
Hacking Trees
From the Under-the-Hood dept.
Today, I thought I'd show you how to attach a LSL/OSSL script to a tree, and in the process touch on some interesting concepts.
The first thing you need to know is that tree and grass are really just prims with a special 'PCode' that tells the viewer to render them differently. In theory, you would be able to attach scripts to trees and grass in Second Life™ - with OpenSim, theory becomes practise!
- set up an empty region
- create a Tree, call it 'My Tree' or something so you can identify it later
- create a Box, call it 'My Box'
- create a script in the box, call it 'Tree Script'.
- on the opensim region console, issue the command
save xml2 test.xml - the file will be saved in the OpenSim root /bin directory as test.xml. Open it up, preferrably, in an xml-savvy editor for readablity.
- inspect the xml code. It's a mouthful, but after some careful scrutiny, you will start to understand some of the nodes. You're now looking at the under-the-hood representation of objects in OpenSim, which incidentally is based on the binary data protocol that the viewer and region uses to communicate. Doesn't look anything like the numbers you see in the edit object tabs, right? That's because the viewer is trying to present stuff in a way that you as a user will feel more familiar with.
- Now copy the full <Shape>...</Shape> node block of 'My Tree' and paste it over the full <Shape>...</Shape> node block of 'My Box', replacing it. This is the tricky part, but done right, you've replaced the box shape values with the tree shape values, turning the box definition into a tree definition.
- Save the modified test.xml file to disk.
- Delete 'My Box' and 'My Tree' from the region, making room for importing the new hacked versions.
- Issue the command
load xml2 test.xml - Hey Presto! You will have two trees, probably of wildly differing sizes. (If so, this is because the <Scale> of 'My Box' was different from that of 'My Tree'.)

- Now, right click on 'My Tree', and chose 'Open' (not 'Edit'!) from the Pie menu - In OpenSim you can actually Open the contents of a tree - in Second Life™, this pie option woul be grayed out. In 'My Tree' the content is empty.
- Now, right click on the tree called 'My Box', and chose 'Open' - we still have our script there!
- Now, script away, you can basically do anything with the tree/grass that you can do with any other prim - set the params, size, position, communicate, do http requests. Go wild!
- Here's a little demo script to get you started: it toggles the tree position and size every second, while saying 'timer' every tick.
integer toggle = 0;
default
{
state_entry()
{
llSay(0, "Script running");
llSetTimerEvent( 1.0 );
}
timer()
{
toggle = 2-toggle;
float newval = toggle + 3;
llSay(0, "timer");
list rules = [ PRIM_SIZE, < newval, newval, newval >, PRIM_POSITION, < 128+newval, 128+newval, 30+newval >];
llSetPrimitiveParams( rules );
}
}
- You can now take this object to inventory, clone it and give it to your fellow man.
For extra credit:
- The <State> field of the tree prim represents the tree type*.
- Explore changing the TextureEntry, Particle Systems, SculptTexture, CollisionSound of a tree - does it do anything? Please report back to me.

- What with defining sit targets, attaching to link sets, defining touch_start() events? Can you find a way to interact with the trees in interesting ways?
- Can you add other objects into the content? Make a wishing tree where people can actually 'Open' the tree and pillage the goodies?
- How about "Megatrees"? Find the <Scale> nodes and create a 100 meter Tolkien forest? (There are two but only one of them needs changing, figure out which)
- What fun can be had with 'grass'? (Create and save 'My Grass' and inspect the xml.)
- How would one go about to export the scripted trees to other grids, where you don't have console access?
- Is there an even simpler way to do this? Can Prims Rez trees from inventory, give them active scripts, and if so, can trees Rez their spawn? Please report back with your findings, or blog about them.
- Take screenshots of the steps along the way, and send them to me so I can illustrate this article better.
Thank you for taking your time reading all of this. I hope that you have learned at least something about Prims, Trees, Grass, Love, OpenSim, the xml2 format and life in general.
Best regards,
Stefan Andersson aka lbsa71
* Pine1 = 0, Oak = 1, TropicalBush1 = 2, Palm1 = 3, Dogwood = 4, TropicalBush2 = 5, Palm2 = 6, Cypress1 = 7, Cypress2 = 8, Pine2 = 9, Plumeria = 10, WinterPine1 = 11, WinterAspen = 12, WinterPine2 = 13, Eucalyptus = 14, Fern = 15, Eelgrass = 16, SeaSword = 17, Kelp1 = 18, BeachGrass1 = 19, Kelp2 = 20
February 21st, 2009 - 00:12
Nice to read about vegetation !
I remember how cool it was the first time we managed to get them persisted one year ago
February 21st, 2009 - 14:12
Tried the tree hacking out Stefan.
So far not so good.
Box is a tree alright, script is in contents when opened.
Quote
“Can you add other objects into the content? Make a wishing tree where people can actually ‘Open’ the tree and pillage the goodies?”
Erm, nope.
Contents tab is greyed out when in edit mode, and unfortunately, no contents can be added when “tree” is opened via pie menu.
May need some seriously more capable coders than I, to script it to do much.
It would appear, for the moment at least, any objects contained in the “tree\box” must be hand crafted into the xml file.
Editing and saving the script is not functioning within the tree\box, it may be that there is something in the “PCode” that precludes script functions within that object class?
It was fun anyway and it works to the point of making a box into a tree.
Stops dead at functional scripts inside it, won’t compile at all.
Shortdog
February 21st, 2009 - 14:26
Shortdog, thank you for trying it out!
Yes, I was thinking one would first create a Box with all that was needed, then just switch it to a tree thru xml hacking.
No, there should be nothing stopping you from double-clicking the script in the tree/box editing it and saving/compiling – I was doing that galore yesterday, even with the trees jumping around.
Scripts not compiling? What exacly happens?
February 22nd, 2009 - 03:25
Hey Stefan.
I copy and pasted your entire post to a notecard, created a new region on my sim for the test, followed to the letter your instructions.
All that works exactly as you said.
I can “open” the box(tree hacked), I can edit the script it contains.
I can save the edited script.
I used scripts I know work on this svn because they are working on our home region adjacent.
But nothing recompiles for me.
Don’t even get a compile error window.
Or, it may be my config is shot.
Opensim ini file is configured for ODE, Meshmerizer, DotNet, LSL only,
OSFunctions all false, as default settings.
And, as I mentioned, it is impossible to add an object to the greyed out contents tab to make a Wishing or Gift Giving tree.
At the moment.
Take a real coder to do it , not a copy and paste hacker like me.
My setup.
Running standalone using HG. Svn is OSG download latest 8534.
Machine is Athlon 64 bit 3400, 1GB DDR2 RAM, OS Win XP Pro Corp SP3.
Machine is dedicated to opensim over 100Mb LAN.
Connected to net for Hypergrid links, via wireless DSL, speed nominal 1500Kb download, 256Mb upload.
(Tyke and myself still enjoy the peer to peer way you and Darren started, and we both actually miss TN.).
That’s about all I can think of for now mate.
There must be some difference in that xml, between user created objects and the hardcoded. The shape came over beautifully, scales well, the script is there, is editable, just won’t compile\recompile for me.
Sunday afternoon here, I’ll delve back in, see what else I can break.
Cheerio.
SD
February 22nd, 2009 - 03:57
I’m an idiot forget what I said.
Stefan it works mate, it works.
I had to shutdown and restart the server for the scripts to recompile.
I now have Linden trees, oddly named My Box, belching particle smoke.
Hehehe, you did it mate. We can play silly things with trees and grasses.
Next is to see if a copy will rezz and work.
SD.
February 22nd, 2009 - 05:00
I’ve figured it out.
Do all the fun stuff to the box first.
THEN, save the xml2 file and edit it.
If you copy and paste a little more, you change more of the box.
It worked for me using the scale parameters as well.
=============================================
{ lbsa71 snips some xml that got mangled by WordPress }
=======================================================
cut copy paste from between the lines.
call it something.xml, load it, copy the Trop-Box.
It’s scripted to play ambient forest sounds.
Thank’s Stefan.
SD.
February 22nd, 2009 - 08:28
Shortdog,
wow, that’s amazing, just the kind of stuff I thought you’d be able to do.
It seems your Xml didn’t survive WordPress comments, will have to see what to do about that.
Thank you so very much for doing this, and I’ll try to upload your images ASAP.
February 22nd, 2009 - 08:56
Stefan, further investigations.
While this is functioning perfectly on standalone.
So far attempts to import to grid have failed to import the embedded script or the textures to either OSGrid or a sneak I’ve found at SL..
Back to you there I’m afraid.
SD.
February 22nd, 2009 - 09:11
Shortdog,
The script content isn’t saved in the xml2, just a reference to it. This means that the script won’t follow if you load the xml2 on another grid. The solution is to do the whole procedure on a region that is connected to OSGrid from the start (or use the oar file format on the whole region) Also, Second Inventory is sketchy on handling prim content, and I think ‘tree content’ would be even worse, as that’s not even allowed in SL. You could talk to the SI guys about it, as it actually should work theoretically.
February 22nd, 2009 - 09:22
Stefan.
We have proof of concept at least.
If someone has a grid connected region and wants to continue?
If a code illiterate retired steamship engineer can get it to work, anyone can.
Back to tweaking xml’s ciao.
SD.
March 6th, 2009 - 21:59
did the tree put the script in realy depends on what you want to do with it seams you cant change like the textures for the tree but did manage to reperduce the tree populator in script form so you can watch it grow and set it to a point were you want it to stop growing atm i have it so grow about 0.216 meters every half hour but can set the growth rate to go slower havent put the tree rezzer part in thinking have it grow to a height then rezz a tree then die will see how it gos have fun