Modifying existing track shapes with Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pgroenbaek
    Member
    • Jan 2024
    • 98
    • Denmark
    • ORTS

    #1

    Modifying existing track shapes with Python

    This is an experiment of mine that has turned out much better than I expected. So I thought I'd share it.

    I'm building a route where I needed some track shapes for DBTracks that do not exist. Like short tunnel sections to create a smooth grade change within tunnels and the ability to split a 2t tunnel into two 1t tunnels. The latter is not possible with the existing 1t tunnels because the walls end up overlapping.

    So what does a programmer do? Yes! Code the problems away.

    The result is a little collection of utilities that makes moving vertex points around quite easy to do in track shapes. I'm not adding/removing vertices or editing how they are connected. That's beyond the scope of this. But you can still do very interesting things with this both for straight and curved track shapes.

    How this works is we define the centerline of the track like in the image below. When we have that and can calculate the closest distance from each vertex point to the centerline, identifying what part of the track a point belongs to becomes simple for both straight and curved track.

    Trackcenters.png

    Add in the following and now we can do really interesting things:
    • Being able to check which prim_state (i.e. material) a point is associated with
    • Being able to calculate new positions of points to change their distance to the centerline
    • Being able to calculate new positions of points to change their distance from the start of the track
    Examples of what can be done with it:

    Creating 500r0_625d tunnels from 500r5d tunnels (code)

    Here we simply move all points that are beyond 0_625d along the track curve to place them at 0_625d while keeping their offset to the track centerline.

    Screenshot 2025-03-05 170957.png

    Converting DB1z 1t track shapes into V4hs_RKL 1t slab track (code)

    Here we move points of the trackbed, adjust the corresponding uv values, and change the textures to create RKL slab track.

    Screenshot 2025-03-05 170537.png


    Editing appearance of 5d switches (code)

    This was to make the DBTracks switches appear more like the ones we have here in Denmark. The DB_WM1c part is moved below the trackbed and some textures have been changed. The new textures appear black because I didn't add them to the route yet.

    Screenshot 2025-03-05 171410.png
    ​
    Keep in mind that this is still very experimental. The code is only a couple of days old. But I thought maybe other people here will find it useful as well.

    It is surprisingly easy to do stuff like this and automate it for a lot of track shapes.
    Last edited by pgroenbaek; 03-06-2025, 13:31.
    - Peter

    Route Projects: DK24
    Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils
  • eric
    9500 Post Veteran and Administrator



    • Dec 2002
    • 9583
    • NS MP245.4 CNO&TP South District
    • English, SQL
    • General

    #2
    In the context of Rick-MONON 's work, this is a lifesaver, assuming the width of just the railhead can be narrowed to match the profile for either ATracks, ScaleRail, etc.
    If you like what you see here at Trainsim.com, be it the discussions and knowledge in the forums, or items available in our library, I hope you'll consider a paid membership to help support keeping the site operating.... Thanks!

    Comment

    • pgroenbaek
      Member
      • Jan 2024
      • 98
      • Denmark
      • ORTS

      #3
      If we look at a cross section of the railheads like in the image below, 1 and 2 can be done with this. 3 cannot, as that would require a new pair of vertices to be inserted.

      Railheads.png
      - Peter

      Route Projects: DK24
      Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

      Comment

      • pgroenbaek
        Member
        • Jan 2024
        • 98
        • Denmark
        • ORTS

        #4
        Reading the other thread it seems like it is the rails of Norbert's Bahntrasse with built-in rails that are the issue. If I understood it correctly.

        I can give it a shot to change the rails on them so that they match the ATracks/ScaleRail railheads.

        I don't know what the ATracks/ScaleRail railheads look like exactly at the moment, but if they are like number 3 in the image above, Norbert's shapes can at least still be made to look much more like them even it it's not a 100% there.

        I just need an example shape from Rick's route to know what the modified texture names are called (assuming that he renamed them in Norbert's shapes).
        - Peter

        Route Projects: DK24
        Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

        Comment

        • eric
          9500 Post Veteran and Administrator



          • Dec 2002
          • 9583
          • NS MP245.4 CNO&TP South District
          • English, SQL
          • General

          #5
          They're definitely #3, but visually, having 2 intersecting with 3 won't be as distracting as what we have with 0 to 3.

          Ignoring the base, this is the rail profile offset for ATracks:

          Name ( "rail_outer" )
          Position ( 0.762 0.19 )
          Position ( 0.7895 0.325 )

          Name ( "rail_top" )
          Position ( 0.7895 0.325 )
          Position ( 0.7175 0.325 )

          Name ( "rail_inner" )
          Position ( 0.7175 0.325 )
          Position ( 0.745 0.19 )
          If you like what you see here at Trainsim.com, be it the discussions and knowledge in the forums, or items available in our library, I hope you'll consider a paid membership to help support keeping the site operating.... Thanks!

          Comment

          • pgroenbaek
            Member
            • Jan 2024
            • 98
            • Denmark
            • ORTS

            #6
            Thanks for that Eric.

            I might actually be able to create something that can handle 3 as well.

            I asked ChatGPT how one would go about modifying the indexed_trilist to add a new vertex between two existing ones, and the response kinda seems reasonable. So I'm going to try that out. We'll see what it turns into.
            - Peter

            Route Projects: DK24
            Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

            Comment

            • pgroenbaek
              Member
              • Jan 2024
              • 98
              • Denmark
              • ORTS

              #7
              I've done some significant progress on dealing with case #3.

              I can now reliably insert new vertices along the edges between existing vertices and correctly recreate the triangle lists. The surface of the new triangles are flipped sometimes due to the normals being calculated incorrectly, but that is solvable.

              Here are some pictures of ATracks-like rails on the NR_Emb_a1t10mStrt.s shape that I created with it. These were created from the perfectly square railheads that are part of those shapes normally. Don't mind the textures as I didn't adjust the UV points yet. And as you can see some of faces face inward due to the normals.

              Screenshot 2025-03-17 155108.png Screenshot 2025-03-17 155043.png

              I also implemented something that can soft-delete things by editing the triangle lists. I didn't want to do implement full deletion of everything (vertices, points, etc.) because then all indices everywhere must be updated. And that's just going to turn into a mess.

              Apart from the normals there are some things left to do before I can do this for all the NR_Emb shapes. Things like updating the headers of the subobjects and configuring the track center based on data from the global tsection.dat (because doing that manually stinks).

              So it is most definitely doable to create a set of new NR_Emb shapes with changed railheads that fit nicely alongside ATracks.

              Edit:

              Here is the script that did this. I did a full rewrite of everything in the python module, so it is much easier to work with and there is a lot less clutter when using it.

              Last edited by pgroenbaek; 03-17-2025, 09:44.
              - Peter

              Route Projects: DK24
              Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

              Comment

              • pgroenbaek
                Member
                • Jan 2024
                • 98
                • Denmark
                • ORTS

                #8
                Now I managed to get the normals right but it has become apparent that things are off.

                Very interesting. The operation probably needs to happen in the opposite order at one end.

                Fun stuff.

                Screenshot 2025-03-17 211126.png
                - Peter

                Route Projects: DK24
                Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

                Comment

                • pgroenbaek
                  Member
                  • Jan 2024
                  • 98
                  • Denmark
                  • ORTS

                  #9
                  Looks pretty decent to me now. Yesterday's geometry issue is fixed and I have also adjusted texture coordinates and normals.

                  The top right track section is an ATracks section and bottom left is an NR_Emb shape with edited and additional geometry on the rails.

                  eric and Rick-MONON I can do this for all of Norbert's Bahntrasse 2.1 shapes and upload them to the file library if you are interested. It's mostly work on configuring the trackcenters and making sure that the edited shapes are valid that's left in order to do that.

                  Screenshot 2025-03-18 160604.png
                  - Peter

                  Route Projects: DK24
                  Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

                  Comment

                  • pgroenbaek
                    Member
                    • Jan 2024
                    • 98
                    • Denmark
                    • ORTS

                    #10
                    Here's an update on the NR_Emb sections with ATracks rails:

                    The 1t sections are quite solid now, both for curves and straight sections. 2t and above are still not quite there yet.

                    The shapes with modified geometry now also load in Open Rails without errors, and they can be recompressed by ffeditc_unicode.exe and worked on by other tools like SFM without problems. Shape Converter still does not like the modified shapes for some reason. So the validity of the modified shapes have improved as well.

                    Screenshot 2025-03-23 203441.png
                    - Peter

                    Route Projects: DK24
                    Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

                    Comment

                    • scottb613
                      2500 Post Veteran


                      • Apr 2003
                      • 2848
                      • Downeast Maine (soon)
                      • ORTS

                      #11
                      Hi…

                      Really nice work - I like the integrated berms - they look great - I hadn’t seen them before.

                      Regards,
                      Scott
                      <a href=https://www.trainsim.com/forums/filedata/fetch?filedataid=80663&type=full title=thumb_80663.png >thumb_80663.png</a>​ My Blender Models

                      Comment

                      • pgroenbaek
                        Member
                        • Jan 2024
                        • 98
                        • Denmark
                        • ORTS

                        #12
                        Thanks Scott, this is all just one big experiment. But one that's turning out quite decent.

                        Those shapes are from Norbert Rieger's Bahntrasse 2.1, they also exist without integrated rails and that package is called Bahntrasse 1.04.

                        The packages are called nr_bt210.zip and nr_bt104.zip in the file library here if you want them.
                        - Peter

                        Route Projects: DK24
                        Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

                        Comment

                        • pgroenbaek
                          Member
                          • Jan 2024
                          • 98
                          • Denmark
                          • ORTS

                          #13
                          I have now ironed out the last issues with the script, so you can expect the NR Bahntrasse 2.1 shapes with ATracks rails to show up in the file library fairly soon.

                          All 4279 shapes just need to be run through it.

                          I will make some replacement shapes for the MONON-2 route as well. Some of the shapes in that route use other textures and I might as well do that too while I am at it. It's not going to take me very much additional time to do that.

                          This has really been a great test in terms of what can be done to existing shapes. At this point I wouldn't exactly call the Python module that I built for this pretty or stable in any way, but it's quite a lot more advanced than just moving points around now. It can quite literally be used to programmatically build new geometry in existing shapes. Which is what I'm doing here with the ATracks rails.

                          Screenshot 2025-03-28 102708.png
                          - Peter

                          Route Projects: DK24
                          Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

                          Comment

                          • pgroenbaek
                            Member
                            • Jan 2024
                            • 98
                            • Denmark
                            • ORTS

                            #14
                            The NR Bahntrasse 2.1 shapes with ATracks railheads can now be found in the file library (fileid 90029). There should be no visible differences between these and Eric's ATracks. They are used in the same way as the original shapes, but are named "NR_Emb_AT_*" instead of "NR_Emb_*" and so forth for the other types.

                            Let me know if you find any issues with them.

                            I also created shapes specifically for the MONON-2 route that are named so you can just drop them into the route folder structure (fileid 90030). The textures used by those shapes were adjusted as well where necessary.

                            There's also now overheadwire versions of Laci1959's and kapitaen's animated DblSlip7_5d switches available for DBTracks here. I copied the overhead wire from one of Norbert's shapes into the animated shapes.

                            Apparently shapes can use very different coordinate systems internally in terms of scaling and rotation, so this brought up the challenge of having to remap all the points and normals for the wires to show up in the right place when rendered. The code that I used to make these is available here.​

                            5b05ae8.png
                            - Peter

                            Route Projects: DK24
                            Utilities and Tools: ShapeIO, ShapeEdit, TrackshapeUtils

                            Comment

                            • kapitaen
                              Senior Member

                              • May 2010
                              • 331
                              • Germany
                              • German + a little bit English
                              • MSOR

                              #15
                              There is a problem with the zip archive. There are 158 empty files in the NR_EmbBase_AT folder and 79 in the NR_Emb_AT folder. There was probably a packing error.
                              Jan

                              Comment

                              Working...