Templating NML with Python - the non-comprehensive guide...
Posted: 01 Feb 2012 20:11
Introduction
Want to create newgrfs? NML is very elegant and very easy to pickup. This will not be a guide to NML.
Find a guide here: http://newgrf-specs.tt-wiki.net/wiki/NML:Main
Do I need this?
For those creating a small set you'll do fine with plain NML. You should consider a code repository (git), and you might want to try using a makefile to build your grf.
For those who create larger sets, templating repetitive code soon starts to become a concern. Templating reduces hassles like maintenance, formatting, debugging, keeping up with changes in NML spec etc.
A 'gotcha' for NFO authors
[Skip this if you're not used to working with complex NFO]
For those who are used to working with large NFO sets, there's one of aspect of NML which can be filed under 'gotcha': you can't repeat arbitrary blocks of varaction 2. Switch identifiers in NML must be unique. Try it and you'll see what I mean. It's not a showstopper, but means some common design and templating patterns can't be used.
NML, templates and YOU
NML lends itself well to templating. You can probably bend [template language of choice] to fit.
One proven option is the C Pre-Processor, providing constants, file includes, and using variadic macros to construct switch identifiers and other useful things. http://en.wikipedia.org/wiki/C_preprocessor
Another option is the macro language m4. This is proven for use with NFO, and will probably work well with NML. http://en.wikipedia.org/wiki/M4_(computer_language)
http://www.ttdpatch.de/grfspecs/m4nfoMa ... Intro.html
For various reasons I opted to try templating with Python. I think the results are interesting.
Not a comprehensive 'how-to'
I'm not a good teacher. I can't write a comprehensive 'how-to' guide. But I can share some tips, tricks and ideas for approaching NML templating with Python; posts in this thread will do just that.
Want to create newgrfs? NML is very elegant and very easy to pickup. This will not be a guide to NML.

Find a guide here: http://newgrf-specs.tt-wiki.net/wiki/NML:Main
Do I need this?
For those creating a small set you'll do fine with plain NML. You should consider a code repository (git), and you might want to try using a makefile to build your grf.
For those who create larger sets, templating repetitive code soon starts to become a concern. Templating reduces hassles like maintenance, formatting, debugging, keeping up with changes in NML spec etc.
A 'gotcha' for NFO authors
[Skip this if you're not used to working with complex NFO]
For those who are used to working with large NFO sets, there's one of aspect of NML which can be filed under 'gotcha': you can't repeat arbitrary blocks of varaction 2. Switch identifiers in NML must be unique. Try it and you'll see what I mean. It's not a showstopper, but means some common design and templating patterns can't be used.
NML, templates and YOU
NML lends itself well to templating. You can probably bend [template language of choice] to fit.
One proven option is the C Pre-Processor, providing constants, file includes, and using variadic macros to construct switch identifiers and other useful things. http://en.wikipedia.org/wiki/C_preprocessor
Another option is the macro language m4. This is proven for use with NFO, and will probably work well with NML. http://en.wikipedia.org/wiki/M4_(computer_language)
http://www.ttdpatch.de/grfspecs/m4nfoMa ... Intro.html
For various reasons I opted to try templating with Python. I think the results are interesting.
Not a comprehensive 'how-to'
I'm not a good teacher. I can't write a comprehensive 'how-to' guide. But I can share some tips, tricks and ideas for approaching NML templating with Python; posts in this thread will do just that.