Reading and writing MTG


The MTG data structure can be read/write from/to a MTG file format. The functions read_mtg() , write_mtg() , read_mtg_file()., mtg=None, has_date=False)[source]

Create an MTG from its string representation in the MTG format.

  • s (string) - a multi-lines string


an MTG


f = open('test.mtg')
txt =

g = read_mtg(txt)

See also

read_mtg_file()., mtg=None, has_date=False)[source]

Create an MTG from a filename.

>>> g = read_mtg_file('test.mtg')

See also

read_mtg()., properties=[], class_at_scale=None, nb_tab=None, display_id=False)[source]

Transform an MTG into a multi-line string in the MTG format.

This method build a generic header, then traverses the MTG and transform each vertex into a line with its label, topoloical relationship and specific properties.

  • g (MTG)

  • properties (list): a list of tuples associating a property name with its type.

    Only these properties will be written in the out file.

Optional Parameters:
  • class_at_scale (dict(name->int)): a map between a class name and its scale.

    If class _at_scale is None, its value will be computed from g.

  • nb_tab (int): the number of tabs used to write the code.

  • display_id (bool): display the id for each vertex


a list of strings.


# Export all the properties defined in `g`.
# We consider that all the properties are real numbers.

properties = [(p, 'REAL') for p in g.property_names() if p not in ['edge_type', 'index', 'label']]
mtg_lines = write_mtg(g, properties)

# Write the result into a file example.mtg

filename = 'example.mtg'
f = open(filename, 'w')


The two functions lpy2mtg() and mtg2lpy() allow to convert the MTG data-structure into lpy and vise-versa. It ease the communication between the two modules. Each structure are traversed and the properties are copied. Properties can be any pyton object., lsystem, scene=None)[source], lsystem, axial_tree=None)[source]

Create an AxialTree from a MTG with scales.


tree = mtg2lpy(g,lsystem)
  • g: The mtg which have been generated by an LSystem.

  • lsystem: A lsystem object containing various information.

    The lsystem is only used to retrieve the context and the parameters associated with each module name.

Optional Parameters:
  • axial_tree: an empty axial tree.

    It is used to avoid complex import in the code.


axial tree

See also


AxialTree, scale, scene, parameters=None)[source]

Create an MTG from an AxialTree.

Tha axial tree has been generated by LPy. It contains both modules with parameters. The geometry is provided by the scene. The shape ids are the same that the module ids in the axial tree. For each module name in the axial tree, a scale and a list of parameters should be defined. The scale dict allow to add a module at a given scale in the MTG. The parameters dict map for each module name a list of parameter name that are added to the MTG.

  • tree: The axial tree generated by the L-system

  • scale: A dict containing the scale for each symbol name.

  • scene: The scene containing the geometry.

  • parameters: list of parameter names for each module.




tree # axial tree
scales = {}
scales['P'] = 1
scales['A'] = 2
scales['GU'] = 3

params ={}
params['P'] = []
params['A'] = ['length', 'radius']
params['GU'] = ['nb_flower']

g = axialtree2mtg(tree, scales, scene, params), parameters=None, axial_tree=None)[source]

Create a MTG from an AxialTree with scales.

  • axial_tree: The axial tree managed by the L-system. Use an empty AxialTree if you do not want to concatenate this axial_tree with previous results.

  • parameters: list of parameter names for each module.




params = dict()
params ['P'] = []
params['A'] = ['length', radius']
tree = mtg2axialtree(g, params)

Cpfg, symbol_at_scale, functional_symbol={}, mtg=None)[source]

Read a string generated by a lsystem.


  • string: The lsystem string representing the axial tree.

  • symbol_at_scale: A dict containing the scale for each symbol name.

Optional parameters:

  • functional_symbol: A dict containing a function for specific symbols.

    The args of the function have to be coherent with those in the string. The return type of the functions have to be a dictionary of properties: dict(name, value)


MTG object

Mss, mtg, scene, envelop_type='CvxHull')[source]

Convert an MTG into the multi-scale structure implemented by fractalysis.

  • name: name of the structure

  • mtg: the mtg to convert

  • scene: the scene containing the geometry

  • envelop_type: algorithm used to fit the geometry.between scales.


mss data structure.

Download the source file ../../src/mtg/