Templating NML with Python - the non-comprehensive guide...

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

User avatar
2TallTyler
Route Supervisor
Route Supervisor
Posts: 495
Joined: 11 Aug 2019 18:15
Contact:

Re: Templating NML with Python - the non-comprehensive guide...

Post by 2TallTyler »

For those trying this tutorial with a more recent version of Python (I'm using 3.8 ), the only change required is the Print syntax, which now needs parenthesis around the string to be printed:

Code: Select all

print("Running script...")
Andy, any plans to dust off this tutorial with an explanation of how to import data from a separate file? :)
Eddi
Tycoon
Tycoon
Posts: 8258
Joined: 17 Jan 2007 00:14

Re: Templating NML with Python - the non-comprehensive guide...

Post by Eddi »

not much of a tutorial, but i use this to import a "tab-separated-values" (.tsv) table.
https://dev.openttdcoop.org/projects/ce ... ts/read.py
(note that this is a python2 file, and i've made no attempts at converting it to python3)
User avatar
andythenorth
Tycoon
Tycoon
Posts: 5656
Joined: 31 Mar 2007 14:23
Location: Lost in Music

Re: Templating NML with Python - the non-comprehensive guide...

Post by andythenorth »

2TallTyler wrote: 02 Jun 2020 22:25Andy, any plans to dust off this tutorial with an explanation of how to import data from a separate file? :)
Not so much :)

The general trend for templating newgrf has been to import something like a .csv file, or similar. I think the idea is that it's easier for non-technical contributors.

I tried that for FISH, in a few different ways using .cfg files.

Now I just declare everything directly in python files and import those.

Using non-python file formats inevitably requires dealing with escapes, and parsing data types (e.g. strings vs integers, strings vs. lists) etc.

It's not hard to open and parse a .csv file. Something like yaml or json might also be a valid route.

But I'd be inclined to put values in python dicts or classes and just import them.

The FIRS approach is probably too complex as a starting point, but the cargos are the simplest example https://github.com/andythenorth/firs/bl ... os/acid.py

That uses a Cargo class which provides many benefits, but could have just been a dict in a simpler world.

If you're doing houses, you could probably put them all in one python file, houses.py or so, with house_1 = {keys, values}, house_2 = {keys, values} etc. Then you just import houses.py to your main script, and you use it as houses.house_1[key] or houses.house_1.items() or whatever you need for the case you have.

Hope that helps, happy to expand.
User avatar
2TallTyler
Route Supervisor
Route Supervisor
Posts: 495
Joined: 11 Aug 2019 18:15
Contact:

Re: Templating NML with Python - the non-comprehensive guide...

Post by 2TallTyler »

Thanks for the informative reply, and for suggesting the cargos as a simple example. After some experimentation of my own I came to a similar conclusion that my original plan is probably more trouble than it's worth.

I'm moving forward with splitting up the set into smaller NML files which are then combined with a Python script based on how your tutorial added the header. Dead simple and all in NML so no translation problems or even templating, but it eliminates the problem of scrolling through a wall of code trying to locate a certain piece.
User avatar
andythenorth
Tycoon
Tycoon
Posts: 5656
Joined: 31 Mar 2007 14:23
Location: Lost in Music

Re: Templating NML with Python - the non-comprehensive guide...

Post by andythenorth »

2TallTyler wrote: 04 Jun 2020 13:38I'm moving forward with splitting up the set into smaller NML files which are then combined with a Python script based on how your tutorial added the header.
That makes sense :)

I'm not sure that houses would have the same pay off from templating that e.g. industries have. FIRS has many callbacks, complex industry layouts etc which all benefit from templating.

The major reasons I force all the industry or vehicle data into python are:
  • templating
  • can auto-generate all the docs, which possibly helps players
  • can then also use the docs to QA / check things, much faster for some things than play-testing in game
  • automated validation, python can find bad values and stop compiling much much faster than nmlc finds them :)
Also a question...how long does your house grf take to compile currently?
User avatar
2TallTyler
Route Supervisor
Route Supervisor
Posts: 495
Joined: 11 Aug 2019 18:15
Contact:

Re: Templating NML with Python - the non-comprehensive guide...

Post by 2TallTyler »

andythenorth wrote: 04 Jun 2020 17:20Also a question...how long does your house grf take to compile currently?
Less than 10 seconds. It’s only about 1,850 lines of code and I’d imagine the lack of sprites helps. :)
User avatar
Zardoz89
Engineer
Engineer
Posts: 6
Joined: 24 Jan 2021 14:04

Re: Templating NML with Python - the non-comprehensive guide...

Post by Zardoz89 »

Good thread. I got the idea, and go a step beyond. Here it's a prototype of the idea : https://github.com/Zardoz89/spainset-en ... ild_nml.py

Simply, reads a CSV file generated from LibreOffice (or Excel), and generates multiple PNML files for each row. Also, uses "Airspeed" (a port of Velocity template engine to Python) that allows to apply logical conditions (if,else, foreach...). The real utility it's for creating a vehicle set. or something similar that have a lot of boring repeating NML code with a few exceptions. I have intention of using it to try to make a Train set GRF.
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: No registered users and 7 guests