Board index FlightGear Development Scenery

Night texture xml quicker way?  Topic is solved

Questions and discussion about enhancing and populating the FlightGear world.

Night texture xml quicker way?

Postby dom_vc10 » Wed Jun 22, 2022 10:38 am

Hi

I have been working on some scenery models converted from xplane. A lot of these have night textures and I have figured out from the wiki how to create the xml for one object and it worked fine. I am now looking at creating around 35 xml files is there some advice, tips, small script from others that can help speed up this amazingly boring task? :D or I just need to suck it up and manually do it?
dom_vc10
 
Posts: 339
Joined: Mon Jul 27, 2020 8:33 am
Location: CZ - LKTB
Version: nightly
OS: Linux Mint 20.2

Re: Night texture xml quicker way?

Postby TheEagle » Wed Jun 22, 2022 9:20 pm

If you know Python (or basically, any scripting language) it should be fairly simple to come up with a script that parses the AC file, extracts the object names and texture path, then writes those into an XML file (supposing that the night texture always has the same name like the day texture but with a pre- or suffix) ! :)
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?

Postby dom_vc10 » Thu Jun 23, 2022 9:37 am

yes I was thinking that this morning working through the logic of how it could be scripted the issue I have is I don't really have any coding skills, big disadvantage for me. I have started some python course and can print hello world so far so maybe next year :D
dom_vc10
 
Posts: 339
Joined: Mon Jul 27, 2020 8:33 am
Location: CZ - LKTB
Version: nightly
OS: Linux Mint 20.2

Re: Night texture xml quicker way?

Postby TheEagle » Thu Jun 23, 2022 10:47 am

I can maybe write a small script quickly in the evening, inbetween two feedings of a small sparrow who has fallen from his nest ! :wink: But for that I need some infos:
Are all objects in each AC file meant to have the texture changed at night ?
How are the default textures and night textures named ?
Also, I need the one XML file you already made.
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?  

Postby dom_vc10 » Thu Jun 23, 2022 11:53 am

That would be very kind of you.

Are all objects in each AC file meant to have the texture changed at night ?
Not all however I can just move all that do require it into another directory along with the texture files.

How are the default textures and night textures named ?

All named different however all night textures end with _LIT.png.
One example ..
railway_LIT.png -->night texture
railway.png -->day texture

The texture filename for the daytime texture is always in the .ac file

Also, I need the one XML file you already made.

Here it is, this was the first one I made. All models have just one object so no need to search for multiple objects in model file.

Code: Select all
<PropertyList>
  <path>railway.obj.ac</path>

<animation>
 <type>material</type>
 <object-name>OBJ0</object-name>
 <condition>      
  <greater-than>      
   <property>/sim/time/sun-angle-rad</property>
   <value>1.49</value>
  </greater-than>
 </condition>
 <emission>   
  <red>1</red>   
  <green>1</green>   
  <blue>1</blue>
 </emission>
 <texture>railway_LIT.png</texture>
</animation>

<animation>
 <type>material</type>
 <object-name>OBJ0</object-name>
 <condition>      
  <less-than-equals>      
   <property>/sim/time/sun-angle-rad</property>
   <value>1.49</value>
  </less-than-equals>
 </condition>
 <emission>   
  <red>0</red>   
  <green>0</green>   
  <blue>0</blue>
 </emission>
 <texture>railway.png</texture>
</animation>
</PropertyList>
dom_vc10
 
Posts: 339
Joined: Mon Jul 27, 2020 8:33 am
Location: CZ - LKTB
Version: nightly
OS: Linux Mint 20.2

Re: Night texture xml quicker way?

Postby merspieler » Thu Jun 23, 2022 9:53 pm

I've already kinda got such a script (exactly for the purpose of mass xp->fg conversion) but I ran into issues getting that stuff into a docker...
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2241
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Re: Night texture xml quicker way?

Postby TheEagle » Thu Jun 23, 2022 10:40 pm

That sounds great ! But I don't get why you'd need a Docker image for a simple Python script …
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?

Postby TheEagle » Thu Jun 23, 2022 11:29 pm

Is the object always called OBJ0 ? In that case, the script could be this (tested with Python 3 only but should also work for Python 2):
Code: Select all
#!/usr/bin/env python

import os, sys, argparse

def get_texture_paths(folder="."):
    texture_paths = {}
    for name in os.listdir(folder):
        path = os.path.relpath(os.path.join(folder, name), start=folder)
        if name.endswith(".ac"):
            with open(path, "r") as f:
               for line in f:
                   if line.strip().startswith("texture"):
                       texture_paths[path] = line.split('"')[1]
                       break
               else:
                   print(path, "contains no texture, skipping")
    if len(texture_paths) == 0:
        print(os.path.abspath(folder), "does not contain any AC files - exiting")
        sys.exit(1)
   
    return texture_paths

def write_xml_files(texture_paths, lit_suffix, overwrite):
    for ac_path in texture_paths:
        xml_path = ac_path.replace(".ac", ".xml")
        texture_path = texture_paths[ac_path]
        texture_lit_path = texture_path.split(".")
        texture_lit_path[-2] += lit_suffix
        texture_lit_path = ".".join(texture_lit_path)
       
        if os.path.isfile(texture_lit_path):
            if os.path.isfile(xml_path) and not overwrite:
                 print("XML file", xml_path, "already exists - skipping")
                 continue
           
            with open(xml_path, "w") as xml_f:
                xml_f.write("""<?xml version="1.0"?>
<PropertyList>
    <path>""" + ac_path + """</path>

    <animation>
        <type>material</type>
        <object-name>OBJ0</object-name>
        <condition>
            <greater-than>
                <property>/sim/time/sun-angle-rad</property>
                <value>1.49</value>
            </greater-than>
         </condition>
         <emission>
             <red>1</red>
             <green>1</green>   
             <blue>1</blue>
         </emission>
         <texture>""" + texture_lit_path + """</texture>
    </animation>

    <animation>
        <type>material</type>
        <object-name>OBJ0</object-name>
        <condition>
            <less-than-equals>
                <property>/sim/time/sun-angle-rad</property>
                <value>1.49</value>
            </less-than-equals>
         </condition>
         <emission>
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
         </emission>
         <texture>""" + texture_path + """</texture>
     </animation>
</PropertyList>""")
        else:
            print("Night texture", texture_lit_path, "does not exist - not writing XML file")

def main():
    argp = argparse.ArgumentParser()
   
    argp.add_argument("-o", "--overwrite",
        help="Whether to overwrite XML files if any already exist, defaults to not overwriting",
        action="store_true"
    )
   
    argp.add_argument("-s", "--lit-suffix",
        help="What suffix the lit texture has (of the name itself, not the file extension), default is _LIT",
        default="_LIT"
    )
   
    argp.add_argument("folder",
        help="Folder / directory where the AC files and textures are in, default is working directory",
        default="."
    )
   
    args = argp.parse_args()
   
    if not os.path.isdir(args.folder):
        print("Folder", args.folder, "does not exist - exiting")
        sys.exit(1)
   
    texture_paths = get_texture_paths(args.folder)
    write_xml_files(texture_paths, args.lit_suffix, args.overwrite)

if __name__ == "__main__":
    main()
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?

Postby dom_vc10 » Fri Jun 24, 2022 8:56 am

TheEagle wrote in Thu Jun 23, 2022 11:29 pm:Is the object always called OBJ0 ?


Yep it will be. For any models that have multiple objects I am joining them so I end up with one. It's easier to work with and if I understand correctly it is a better approach for models.
Thanks so much for the script I will give it a test today, this could save me hours :)
dom_vc10
 
Posts: 339
Joined: Mon Jul 27, 2020 8:33 am
Location: CZ - LKTB
Version: nightly
OS: Linux Mint 20.2

Re: Night texture xml quicker way?

Postby TheEagle » Fri Jun 24, 2022 10:15 am

Yep, especially for scenery models, the less objects you have the better for the loading times and FPS. As for the script, if you are lost about it's arguments just do script.py --help ! ;)
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?

Postby dom_vc10 » Fri Jun 24, 2022 1:44 pm

great :-)

I am having one issue the .ac files have the texture line texture "objects\texture.png so the script you made is looking for the filename objects\texture.png . I need to remove this from the .ac FG ignores it so I never thought about it. I am trying to use sed to remove it but the backslash seems to make that tricky when it should be easy
dom_vc10
 
Posts: 339
Joined: Mon Jul 27, 2020 8:33 am
Location: CZ - LKTB
Version: nightly
OS: Linux Mint 20.2

Re: Night texture xml quicker way?

Postby TheEagle » Fri Jun 24, 2022 2:08 pm

TheEagle wrote:use a double backslash when you want a backslash ! ;)

It's the same in Python, btw - and in every other programming language I know, too !
Last edited by TheEagle on Fri Jun 24, 2022 4:27 pm, edited 1 time in total.
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?

Postby dom_vc10 » Fri Jun 24, 2022 2:39 pm

TheEagle wrote in Fri Jun 24, 2022 2:08 pm:use a double backslash when you want a backslash ! ;)


sed 's/objects\\//g' -i *.ac YES!!! the double backslash was the fix. Thank you so much. Now I finally tested your script and I have a lot of xml files :D Thank you so much for your help and code. Really appreciate it :D
dom_vc10
 
Posts: 339
Joined: Mon Jul 27, 2020 8:33 am
Location: CZ - LKTB
Version: nightly
OS: Linux Mint 20.2

Re: Night texture xml quicker way?

Postby TheEagle » Fri Jun 24, 2022 4:25 pm

No problem ! :) Don't forget to mark the topic as solved ! :wink:
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Night texture xml quicker way?

Postby merspieler » Fri Jun 24, 2022 5:17 pm

TheEagle wrote in Thu Jun 23, 2022 10:40 pm:That sounds great ! But I don't get why you'd need a Docker image for a simple Python script …


Just for that, no... but it does everything, convert images from .dds to .png, convert the models from... so I'm trying to build a container with all the dependencies and stuff in it for that to work.
Nia (you&, she/her)

Please use gender neutral terms when referring to a group of people!

Be the change you wish to see in the world, be an ally to all!

Join the official matrix space
merspieler
 
Posts: 2241
Joined: Thu Oct 26, 2017 11:43 am
Location: Wish to be in YBCS
Pronouns: you&, she/her
Callsign: you&, she/her
IRC name: merspieler
Version: next
OS: NixOS

Next

Return to Scenery

Who is online

Users browsing this forum: No registered users and 9 guests