NewGrowth interactive tree modeling for Blender: new version
A new tree addon, Part VII: an all C implementation
As indicated in this BlenderArtists post I have started developing a version of the space tree addon with most of the core calculations performed by C code.
Currently most functionality is available except for apical control and pruning but a 4x speed up (approximately, your mileage may vary) is worth it. This version is at the moment only available for 64bit Windows platforms (because it was difficult enough already to get that running with Visual Studio 2012) and unlike the previous C enhancements there will be no fallback on pure Python.
Code availability
This version is developed in its own branch, 'all_c' and the installation follows the exact same pattern as for other addons (don't forget to remove any older version). The zipfile is located here. As of May 2, 2014, this is linked against the 3.4 Python library as shipped with the Blender dailt build. If you use a slightly older version of Blender you might want to use the previous commit that was linked against 3.3.
The (almost) pure Python version stays available in the master branch, the zip file for this version is located here.
Future developments
I am thinking about making a Linux version available but that's not high on my priorities. If someone is willing to compile the module however I can include it with the addon. I think it is entirely possible to have a csca.pyd and a csca.so side-by-side. Having different .so files for Linux and OSX is something else, that would require tweaking the Python imports based on the OS type.
The current speed improvements only apply to the generation of the tree skeleton. I think the same improvement should be possible for native skinning algorithm. Also, there is yet no optimization of the C code and no multithreading, so we might improve the speed even more.
Another area of interest that I am currently spending time on is creating good leaf textures. Even with a decent flatbed scanner this is not trivial, yet now that spring is in full gear in my hemisphere I have started on cherry, apple and oak leaves.
References
The space tree addon itself is introduced in a few articles on this blog and is available on GitHub. Relevant links are listed below:A new tree addon, part VI: continuing development
The latest version (0.2.14) includes a module written in C that implements some of the most time consuming functions. The result is a 20 - 30 % speedup for the generation of the branch skeleton. For now this is only available for 64 bit windows but on a different platform the addon will fall back on pure python implementations of these functions so cross platform portability is still guaranteed.
This is a quite noticable speedup: on my machine the generation of a tree consisting of 3000 branch segments went from 3.7s to 2.7s.
Implementation notes
The C extension module was compiled with Visual Studio 2013 and seems to work fine both with the official 2.70 distribution (compiled with MSVC 2008) and with the daily builds (compiled with MSVC 2013) but will only work with 64 versions of Blender. The Visual Studion solution is part of the spacetree repository so if people are interested in porting the module to other platforms they can have a go. Detailed notes on how to compile a native Python extension with Visual Studio 2013 were published in a separate article.
Undocumented stuff
This version of the addon also contains a first try at the implementation of branch shedding.
References
The space tree addon itself is introduced in a few articles on this blog and is available on GitHub. Relevant links are listed below:A new tree addon, Part III: development continues
The latest version (0.2.10) adds a particle system with acorns to the library (not because it is briliantly modelled but to show an example usage) and adds a shadow density option if you use a shadow group (because no shadow is absolute). I also added an option to change the size of the faces of the emitter mesh. That way leaves and other objects can be made to sit closer to the branches.
Version (0.2.9) of the spacetree addon features cleaner code, the posibility to add two particle systems and an update to the bundled library: it now features a particle system of chestnut leaves as well as an additional bark material. I have also reordered the options in the UI to a somewhat more logical groouping and I am working on an update to the user manual (old version is still available, concepts discussed there are still valid)

Sample image of a tree with the chestnut leaf particle system. The crown was shaped with a crown group consisting of just a couple of icospheres.
References
- Part I (contains updated road map)
- Part II
- Download and installation instructions (on GitHub)
- Blender Artists thread
Future work
Several suggestions have been done to improve the addon (and a few items from the roadmap are still open) but these are the most interesting ones in my opinion, although I am the first to admit that most serve more an academical interest than an artistical one:
- Apical control
- The suppression of side shoots lower down the branch by the growing tip. Is in place but functions unsatisfactorily. Should give straighter branches.
- Branch angle control
- The angle of a side branch is usually more or less fixed for a given species of tree. Having this could add to the believability of the generated tree.
- Branch sheding
- Some trees (e.g. conifers) drop old branches that don't receive enough light, resulting in bare lower trunks.
- Shadow group density
- Currently the shadow group will receive no endpoint markers at all. This should be modulated by some density factor to emulate half shadow (done, see version 0.2.10)
- Tree roots
- Fairly easy to add I think as it is mainly a not so comples upside down tree (at least as an approximation :-). The trick is to match the diameter of the trunk to the top of the root system.
If you really like my addon you might consider having a look at my Amazon wish list :-)
Random Vertex Colors, a simple addon
In a previous article I presented an addon to generate a floor board with individual planks. Each individual plank was adorned with a random vertex color that can be used to create materials with random color variations for each face.
When working with the Ivy Generator recently I wanted this same feature and indeed my own space tree addon could benefit from this as well, because just like IvyGen it generates a single mesh object with a face. for each leaf. So I decided to separate this functionality from the floor board generator so it can be used in different situations.
Once the addon is installed it can be used on any mesh object when in vertex paint mode as shown below: 
It will replace the vertex colors of the active vertex color layer with colors that are random but equal for each face. In Cycles vertex colors can be accessed with an attribute node, an example node setup can be found in the article on the floor board generator referred to at the start of this article.
Code availability
All functionality of this add-on plus a lot more is now available as a convenient all-in-one add-on on BlenderMarket. It comes with an extensive PDF manual and your purchase will encourage me to develop new Blender add-ons.
The simple code shown in this article is available for download at GitHub and is uploaded to the upload section of Blender extensions tracker so if you find bugs you may post them there as well.
[2016 Feb 7] The version currently in GitHub is much faster for large meshes because it uses Numpy. Check this article to see why.
A new tree addon part VII: tweaking leaf UVs
This article is the seventh intallment in a longer series on the development of a new tree add-on for Blender, based on the space colonization algorithm. The add-on is available on Github, and documentation on this page.
It is now also in Blender's Contrib repository and the documentation is being moved to the Blender wiki
Previous articles about this add-on:
Now that the addon is in Blender's Contrib repository I thought it was time to start acting on some of the feedback from the BlenderArtists thread.
This release contains two minor improvements:
- rotated UV-maps for the leaves and
- an option to set the offset of the leaves from the twigs
Depending on the image that you use as a leaf texture it might be useful to be able to control the point where the leaves are joined to the twig. This offset is now provided as an option slider in the leaves section. Note that even if this offset is zero the leaf plane might still be intersected by a twig. This happens when the branches are skinned with Blender's skin modifier (instead of the native skinning method).Before this skin modifier the addon adds a subsurface modifier which has the effect of cutting corners. Choosing a smaller internode distance and removing this subsurface modifier will prevent this unwanted intersection but in most cases (I.e. with enough leaves) this probably won't be noticeable anyway.
A new tree add-on part VI: speed improvements for complex trees
This article is the sixth intallment in a longer series on the development of a new tree add-on for Blender, based on the space colonization algorithm. The add-on is available on Github, and documentation on this page.
Previous articles about this add-on:
Trees that contain many branchpoints take a progressively longer time to generate as they become more complex. This is due to the time it takes to determine which branch segment is closest to any endpoint. The code up to version 0.0.7 did a linear scan through all the branch segments for each endpoint and while this was still fast enough for moderately sized trees (the time it takes doesn't explode exponentially because each iteration removes endpoints within kill distance of a branch) it took many seconds for a tree with thousand endpoints, short internode lengths and a short kill distance. In other words it got slow when the trees became interesting.
Version 0.0.8 (available on Github, click view raw to download) adds a significant speed up for larger trees (3-6 times depending on parameter settings). Quite complex tree skeletons can now be generated in a few seconds instead of half a minute or more (depending on the power of your pc of course. And remember, this is a pure python implementation so having multiple cores makes no difference, it's the raw speed of a single core that counts). Please note that because endpoints within kill distance are removed in a different order, trees generated in version 0.0.8 are not identical to trees generated in previous versions but their general appearance stays the same.
For the technically inclined: this speed up is realized by storing all branch segments in a kd-tree, so looking for a nearest neighbor now scales as the log of the number of branch segments instead of taking linear time. This is a very noticeable difference once your tree grows to hundreds or even thousands of branch segments. The kd-tree is a pure python implementation in its own, separate module kdtree.py that might be interesting for other applications as well.
A new tree add-on part V: speeding up skinning of the tree skeleton
The new version (0.0.7) is as usual available on GitHub. How to use and install this add-on is documented in previous articles: I II III IV.
The new skinning method is the default if you uncheck the no modifiers checkbox but if you like you can still select Blenders skin modifier from the drop down as shown on the right. (This can also be done later because we leave the skeleton itself intact; as it only consists of edges it won't show up in renders but you could use it to add any modifier later on). Timings
The table below lists some timings for my computer, all values were left to their defaults except for the number of endpoints:| Timings in seconds for generating a tree skeleton | |||
|---|---|---|---|
| Endpoints | No modifier | Own skinning | Blenders skinmod |
| 100 | 0.02 | 0.04 | 0.46 |
| 200 | 0.04 | 0.07 | 0.74 |
| 400 | 0.08 | 0.16 | 1.51 |
| 400 | 0.56 | 0.87 | 14.43 |
The last line shows the results for a fair sized, bushy tree (400 endpoints, but with the internode length and kill distances reduced to 0.5 and 2.5 respectively and with 3000 extra endpoints per 1000 iterations). The difference between less than a second and more than 14 seconds is the difference between comfortable modelling and frustration, and there is still some room for improvement because the code is still riddled with print statements.
There are three factors that help to speed up the skinning operation compared to blenders built-in skin modifier:
- unlike the modifier we do not have to make a copy of the mesh,
- we also generate a lot less geometry it seems and
- we don't have to deal with special cases: the skin modifier can handle any geometry while our skinning just has to deal with straight edges or simple forks.
Results
When we apply a subsurface modifier to our skinned branches the result is already quite acceptable although I see room for improvement, specifically in the shape of the forks. As you can see there is some bulging were the branches join and this is especially noticeable when there is either a very sharp anngle between the branches or when both branches bend out of plane. Fortunately the highlighted 5-gon lends itself to all kinds of angle dependant adaptations so I expect I can tweak this further to get smoother results.The focus for the next minor release will be on bug fixing and code cleanup to prepare for a full 1.0.0 release. This will be accompanied by better (hopefully: -) docs as well.
A new tree add-on part III: multiple trunks
multiple trunks
When you want to create a hedgerow or a small copse of trees combining multiple trees might not lead to an acceptable result because branches might cross and the shape is not quite the way you want it to be. Also the amount of work needed to create a hedge of thirty plants can add up. I therefore added the possibility to define any number of starting points by refering to the locations of objects in a group.
Part of the scene setup used the create the example image is shown on the left.
The empties (represented by the green cone shapes) are all in the same group and their locations are used as starting points.The parameter settings were as shown on the right. As usual the code is available as a zip-file on GitHub. More info on how to use the tree add-on can be found in two previous posts: A new tree add-on and A new tree add-on part II: better workflow
Future directions
I noticed that the mesh created by the skin and subsurface modifiers contains not only a huge amount of faces but also contains quite a precentage of vertices (20% or so) that are so close together that they practically overlap. Generating such a mesh takes a considerable amount of time so I think I will try to come up with a skinning method that is suitable for our tree trunks but hopefully somewhat faster.New tree addon part II: a better workflow
Version 0.0.4 of my new tree add-on is focused on defining the crown shape with groups of objects
usability enhancements
In the previous version of the add-on the markers or endpoints that represent the resources that the growing branches compete for were distributed inside an invisible ellipsoid. The resulting distribution could me made visible with the show markers option but from a usability point of view that was not ideal. likewise the su bstraction of markers from the distribution for those markers that were in the set of currently selected objects was less than intuitive, the more so since producing a tree would deselectsll objects forcing the user to select them again when trying for a different tree shape.
In this new version we allow for different way to define a crown shape using groups. If you select the use groups option you can choose three different groups:
- the Crown group
- any markers generated will lie inside the objects that are part of this group.
- the Shadow group
- any markers generated will not lie in any objects in this group. This group is optional.
- the Exclusion group
- growing branches will not penetrate into any objects in this group but stop. This group is also optional.
workflow
The workflow now becomes pretty straight forward:
- create a crowngroup, for example a bunch of different icospheres,

- assign anything casting a shadow to a shadow group (here a box),

- optional: assign anything impenetrable to an exclusion group (or reuse the shadow group),
- position the 3d cursor at the place where the bole of the tree should start,
- click update tree (here shown with the parameter settings that produce the tree in the screenshot),

Code availability
This release is version 0.0.4 and avaible as a zipfile on GitHub
A new tree add-on
A new kind of tree
Documentation for this add-on now has its own page.
[Some additional sample trees are near the end of this article] Because IvyGen is limited to climbing shrubs and Sapling requires a fair amount of user interaction to get nice results, I thought it would be interesting to explore the possibilities of generating trees with the space colonization algorithm as described by Adam Runions. This algorithm models the growth of a tree by letting branches compete for resources (think light) as they grow. The distribution and density of these resources control the final shape and the algorithm lends itself easily to adapt to environmental influences like walls blocking light etc. a feature that comes in handy when doing architectural modelling for example.
In the first version of this add-on many features are already available but it is not yet a finished tool. Any remsrks and criticisms are welcome of course. This article describes how to install and use the add-on but does not give an in depth account of its design or underlying principles, that is left for a later article. I do present some example images with their parameter settings to get you started.
The code is available on github. Just download the latest zip from the release directory and install the add-on with
File->user preferences->addons install from file by pointing it to the zip file. You can then toggle the checkbox to use it (It is in the Add Mesh category as SCA Tree Generator). Remebr to uninstall the add-on first if you installed an earlier version by clicking the remove button. Creating a tree
To add a tree, position your 3d cursor where you would like the tree to grow and selectAdd->Mesh->Add Tree to Scene. A small tree skeleton will appear and in the toolbar on the left of the 3d view will be a number of options. (Press T if the tool bar isn't visible) User interaction is copied from IvyGen so changing the values of options doesn't have an immediate effect but new values are applied when you click update tree. This is necessary because generating larger trees can take quite some time and if that was attempted on each value change the add-on would become unusably slow. An overview of all the options is available with the source, here we just highlight the more important ones.(tip: before you start experimenting it might be a good idea to check the
no modifiers checkbox because the tree trunk is given substance by the skin modifier and will take many seconds on larger trees} The whole idea of the space colonization algorithm is to populate a volume (an ellipsoid in our case) with markers and let a growth point (at the the 3d cursor) start growing in the direction of the nearest markers. As a growth point (called internode or branchpoint in the add-on in a not entirely botanically correct fashion) approaches a marker close enough, this marker is removed and subsequent growth is in the direction of a slightly different set of nearest markers. A branchpoint can have any number of markers in its sphere of influence but conversely a marker has only a single closest branchpoint. Because of that branches may develop sideshoots (read more on this in the original article)
This means that the number of markers and their distribution are the crucial parameters in determining the shape of the final tree,(tip if you check the
show markers option in the debug panel, the markers that were generated at the start of the tree generation will be visualized as small tetrahedrons. That way you get a clear picture of their distribution. This feature is used in most images that follow.) Some example trees and their marker distributions
Note that only the parameters that differ from their defaults are shown in the picture.Few endpoints vs. many endpoints


Near central axis vs. near surface


More endpoints near the top

Mix and match


Even shrubs are possible as shown on the right.
adding leaves
When you click theadd leaves button a new panel with options will appear with controls for the leaves. The number of leaves controls the probability that an internode (branch segment) carries a leaf. The size and size variation of these leaves can be controlled, as well as the variation in orientation of the leaves. The number of connections option controls how much 'old wood' carries leaves. If you set this to 1 only new twigs will carry leaves.

Shaping the branches
When you click on theadd modifiers option, three modifiers are added to the trunk skeleton (shown in the opening image of this article): - a subsurface modifier to smooth the branches,
- a skin modifier to give volume to the branches and
- another subsurface modifier to smooth this skin.
The thickness of the trunk and branches and the way the thickness of forking branches is combined is controlled by the
Scale and Power options respectively.Environmental influence
A final feature of this add-on that almost comes naturally with the space colonization algorithm is the ability to factor in environmental influences. For example if one side of the growing tree receives more shadow than the other side due to a wall or building, that side will grow less. We can easily mimick this by not placing any markers in a shadowed area. To accomplish this the add-on checks whether any of the markers is generates is inside any of the selected mesh objects in the scene and discards them if they are. The workflow is then:- select one or more objects (first image below)
- position the 3d cursor where you want the tree to start
- tweak any options
- update the tree
Note that we did not select the actual wall, but a larger duplicate (that we will not render), because the shadow influence normally extends beyond the wall and because a new branch might still pass through this exclusion zone towards markers that are above it. By chosing a wider volume we prevent those branches to pass through the actual wall.
Limitations
In this first version there will undoubtably be any number of bugs. One of the most annoying issues is that when the kill distance is chosen too small, a developing branch may terminate in a myriad of tiny segments. This will not only take an enormous amount of time when adding the skin modifier but will also cause a strange clump of hundreds of leaves at that point.Another thing is that even when not actually generating new geometry, the user interface is sometimes slow when modifiers are selected. I have a hunch this might be related to Blender keeping track of this complicated geometry in its undo cache but I am not sure.
Of course there is also plenty of room for functional improvements: it would be nice if you could select more than one starting point for example to create hedgerows etc. and also have arbitrary objects instead of square leaves, but those are nice to haves for the future.
Some more examples
The following images show the same type of tree as the opening image of this article, but at more mature stages:


The masked leaf texture as well as the concrete and brick textures are from cgtextures.com, the latter two converted to various types of specular and bump maps with the free shadermap commandline tool. The hdri-background is Alex Hart's 'Arboretrum in Bloom'.
The most mature tree was generated with the settings shown below:
A OSL leaf shape shader for Cycles
In this first part of a series I present a simple shader that can be used to generate different leaf shapes. In later articles we will add shaders that produce the veins of the leaf.
Simple leaf shape with cubic splines
In the example image we have used the IvyGen addon to generate a single limb of some climbing plant species and used the shader from this article to produce the leaf shapes The node setup for this specific material is dicussed later on. (the stone texture is from cgtextures.com and was converted to a normal map and a displacement map with Shadermap CL. The backplate and environment lighting are from the Topanga Forest B collection by Blochi as found on Sibl archive)The leaf shape shader is essentially creates one side of a leaf from a cubic spline and mirrors that to the other side to create a symmetrical shape. The spline that is used has four controls.
It starts on the left in the direction of P1 and ends on the right coming from the direction of P2. The lenght and angles of the vectors going to P1 and P2 are inputs to the shader. Some of the shapes you can create are shown below. Note that it is also possible to define a shape that crosses itself in which case the behavior of the shader is undefined.The annotated code for the shader is shown below. It includes a small library of functions that will be reused by shaders that will appear on this blog in the near future and this include has its separate page where you can download it and find installation instructions.
#include "equations.h"
shader leaf(
point Pos = P,
float Angle1 = 70,
float L1 = 1,
float Angle2 = 70,
float L2 = 1,
output float Leaf = 0
){
// calculate the four control point of the cubic spline
float x1,y1,x2,y2;
sincos(radians(Angle1),y1,x1);
sincos(radians(Angle2),y2,x2);
point P0 = point(0 , 0 ,0);
point P1 = point(x1 , y1,0)*L1;
point P2 = point(1-x2*L2, y2*L2,0);
point P3 = point(1 , 0 ,0);
// to determin the y value(s) of the spline at the x position we
// are located, we want to solve spline(t) - x = 0
// we therefore gather all factors and solve the cubic equation
float tfactor[4] = { P0[0]-Pos[0],
3*P0[0]+3*P1[0],
3*P0[0]-6*P1[0]+3*P2[0],
P0[0]+3*P1[0]-3*P2[0]+P3[0] };
float t[3];
int nrealroots;
cubic(tfactor, t, nrealroots);
// at this point, the array t holds up to 3 real roots
// remove any real root that is not in range [0,1]
int i=0;
while(i < nrealroots){
if ((t[i] < 0) || (t[i] > 1)) {
int j=i;
while(j < (nrealroots-1)){
t[j]=t[j+1];
j++;
}
nrealroots--;
}
i++;
}
// note that a cubic funtion can have 3 real roots,
// but in this case we ignore such very warped curves
// TODO: w. 3 real roots w could set leaf = 1, if y < y0 OR y between y1,y2
// TODO: seration, possible by determining the closest
// distance (if inside leaf) to the spline and
// determining if w are within some periodic funtion f(t)
// we generate the shape mirrored about the x-axis
float y = Pos[1];
if(y<0) y = -y;
if(nrealroots > 0){
point Sy0 = cubicspline(t[0],P0,P1,P2,P3);
if(nrealroots > 1){
// if we have 2 roots we calculate and order the y values
// and check whether the current y values is between them
point Sy1 = cubicspline(t[1],P0,P1,P2,P3);
if ( Sy1[1] < Sy0[1] ){
if( (y > Sy1[1]) && (y < Sy0[1]) ) Leaf = 1;
}else{
if( (y > Sy0[1]) && (y < Sy1[1]) ) Leaf = 1;
}
}else{
// with a single value we check if we are below the y value
if( y < Sy0[1] ) Leaf = 1;
}
}
}
Example node setup
The node setup used to create the sample shapes look like this:
As you can see there is nothing fancy going on here. The leaves are simple squares with a reset uv-map and a mapping node is used to position the leave onto this square (in this case rotated 90 degrees). The output of the shader is then used to drive a mix shader which shows some green material where there is a leaf and a fully transparent material where there isn't.Combining things with the Ivy Generator
The sample image at the beginning was create with the IvyGen addon. The leaves it produces are simple square faces that are not connected but nevertheless consist of a single mesh object. Because we want to orient each individual leaf a bit different we need a random number for each leaf. We therefore have to separate each face into its own object.
The workflow to achieve that then becomes:
- Create your ivy
- Assign the material shown in the noodle below to the leaf object
- Goto edit mode
- Select all
- Select Mesh->Vertices->Separate->By loose parts
- Go back to object mode.
Example node setup II
The noodle consists of three distinct parts:- The green part adds a small random rotation around the z axis to each uv map. This will be different for each individual leaf. The actual rotation is done by another OSL shader (given below) because the vector mapping node cannot be driven by inputs.
- The red part maps the rotated uv to the correct position before handing it to our leaf shader. This mapping is necessary because the squares created by IvyGen are sort of centered on the vines and we want our leaves to protrude from the vined instead of being pierced by them.
- The blue part is just a simple mottled green shader with some gloss.
The rotation of the uv-map arond the z-axis is performed by an OSL shader because the vector mapping node has no inputs to control the rotation and it would take a lot of vector math nodes to achieve what we want whereas thanks to OSL this rotation is a no-brainer:
shader rotate_z(
point Pos = P,
float Angle = 0,
output point Pout = P
){
Pout = rotate(Pos,radians(Angle),point(0,0,0),point(0,0,1));
}
Next steps
In a next article I will show how to add veins to the leaf shapes.










