9. Lsystem and MTGs¶

Author:

Thomas Cokelaer <Thomas.Cokelaer@sophia.inria.fr>

Let us start from the following L-system

```angle = 20

context().turtle.setAngleIncrement(angle)

Axiom: X

def EndEach(lstring):

print lstring

derivation length: 7
production:
X --> F[+X]F[-X]+X
F --> FF

homomorphism:

F --> SetWidth(0.5) F

endlsystem
```

9.1. General usage¶

First, import some modules

```import openalea.lpy as lpy
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time
from openalea.plantgl.all import *
from openalea.mtg.io import lpy2mtg, mtg2lpy, axialtree2mtg, mtg2axialtree
from openalea.mtg.aml import *
```

```>>> l = lpy.Lsystem('example.lpy')
```

execute it:

```>>> tree = l.iterate()
F[+X]F[-X]+X...
```

and plot the results:

```>>> l.plot(tree)
```

that you can save into a PNG file as follows:

```>>> Viewer.frameGL.saveImage('output.png', 'png')
```

9.2. Extract information from the lsystem¶

9.2.1. axiom¶

Get the axiom into an axialtree object:

```l.axiom
```

9.2.2. context¶

context gets the production rules, group, iteration number

```>>> context = l.context()
>>> context.getGroup()
0
>>> context.getIterationNb()
6
```

9.2.3. last iteration¶

If the Lsystem finished nornally, the last iteration must be equal to the derivation length.

```>>> l.getLastIterationNb()
6
>>> l.derivationLength
7
```

9.3. Activate the lsystem with makecurrent¶

Todo

what is this for ?

```l.makeCurrent()
```

9.4. Executing the lsystem¶

9.4.1. animate¶

In order to run the lsystem step by step with a plot refreshing at each step, use animate(), for which you may provide a minimal time step between each iteration.

```l.animate(step)
```

where step is in seconds. Note that you may still set the animation to false using:

```Viewer.animation(False)
```

9.4.2. iterate¶

Run all steps until the end:

```>>> l.iterate()
```

or step by step:

```>>> l.iterate(1)
F[+X]F[-X]+X
AxialTree(F[+X]F[-X]+X)
>>> tree = l.iterate(1)
F[+X]F[-X]+X
>>> l.iterate(1, 1, tree) == l.iterate(2)
FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X
F[+X]F[-X]+X
FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X
True
```

Note

When using iterate() with 1 argument, the Lsystem is run from the beginning again. To keep track of a previous run, 3 arguments are required. In such case, the first is used only to keep track of the number of iteration, that is stored in l.getLastIterationNb(), the second argument is then the number of iteration required and the 3d argument is the axiom (i.e., the previous AxialTree output).

9.5. Transform the lstring/axialtree into MTG and vice-versa¶

```>>> axialtree = l.iterate()
```

9.5.2. axialtree2mtg method¶

```>>> axialtree = l.iterate()
F[+X]F[-X]+X...
>>> scales = {'F':1,'X':1}
>>> mtg1 = axialtree2mtg(axialtree, scales, l.sceneInterpretation(axialtree), None)
```

and come back to the original one:

```>>> tree1 = mtg2axialtree(mtg1, scales, None, axialtree)
>>> assert str(axialtree)==str(tree1)
True
```

9.5.3. mtg2lpy and lpy2mtg method¶

```>>> mtg2 = lpy2mtg(axialtree, l)
>>> tree2 = lpy2mtg(mtg2, l, axialtree)
>>> assert str(axialtree)==str(tree2)
True

>>> scene = l.sceneInterpretation(axialtree)
```

if no lsystem is availabe, you may use generateScene(axialtree) using from openalea.lpy import generateScene