Skip to main content

Python library for parsing Godot scene files

Project description

Godot Parser

Build Status Coverage Status Downloads

This is a python library for parsing Godot scene (.tscn) and resource (.tres) files. It's intended to make it easier to automate certain aspects of editing scene files or resources in Godot.

High-level API

godot_parser has roughly two levels of API. The low-level API has no Godot-specific logic and is just a dumb wrapper for the file format.

The high-level API has a bit of application logic on top to mirror Godot functionality and make it easier to perform certain tasks. Let's look at an example by creating a new scene file for a Player:

  from godot_parser import GDScene, Node

  scene = GDScene()
  res = scene.add_ext_resource("res://PlayerSprite.png", "PackedScene")
  with scene.use_tree() as tree:
      tree.root = Node("Player", type="KinematicBody2D")
      tree.root.add_child(
          Node(
              "Sprite",
              type="Sprite",
              properties={"texture": res.reference},
          )
      )
  scene.write("Player.tscn")

It's much easier to use the high-level API when it's available, but it doesn't cover everything. Note that use_tree() does support inherited scenes, and will generally function as expected (e.g. nodes on the parent scene will be available, and making edits will properly override fields in the child scene). There is no support yet for changing the inheritence of a scene.

Low-level API

Let's look at creating that same Player scene with the low-level API:

  from godot_parser import GDFile, ExtResource, GDSection, GDSectionHeader

  scene = GDFile(
      GDSection(GDSectionHeader("gd_scene", load_steps=2, format=2))
  )
  scene.add_section(
      GDSection(GDSectionHeader("ext_resource", path="res://PlayerSprite.png", type="PackedScene", id=1))
  )
  scene.add_section(
      GDSection(GDSectionHeader("node", name="Player", type="KinematicBody2D"))
  )
  scene.add_section(
      GDSection(
          GDSectionHeader("node", name="Sprite", type="Sprite", parent="."),
          texture=ExtResource(1)
      )
  )
  scene.write("Player.tscn")

You can see that this requires you to manage more of the application logic yourself, such as resource IDs and node structure, but it can be used to create any kind of TSCN file.

More Examples

Here are some more examples of how you can use this library.

Find all scenes in your project with a "Sensor" node and change the collision_layer:

  import os
  import sys
  from godot_parser import load

  def main(project):
      for root, _dirs, files in os.walk(project):
          for file in files:
              if os.path.splitext(file)[1] == '.tscn':
                  update_collision_layer(os.path.join(root, file))

  def update_collision_layer(filepath):
      scene = load(filepath)
      updated = False
      with scene.use_tree() as tree:
          sensor = tree.get_node('Sensor')
          if sensor is not None:
              sensor['collision_layer'] = 5
              updated = True

      if updated:
          scene.write(filepath)

  main(sys.argv[1])

Caveats

This was written with the help of the Godot TSCN docs, but it's still mostly based on visual inspection of the Godot files I'm working on. If you find a situation godot_parser doesn't handle or a feature it doesn't support, file an issue with the scene file and an explanation of the desired behavior. If you want to dig in and submit a pull request, so much the better!

If you want to run a quick sanity check for this tool, you can use the test_parse_files.py script. Pass in your root Godot directory and it will verify that it can correctly parse and re-serialize all scene and resource files in your project.

Changelog

0.1.6

  • Fix errors when using new version of pyparsing

0.1.5

  • Fix quote escaping during serialization #7
  • Better error message for binary scenes #6

0.1.4

  • Supports node groups #5

0.1.3

  • Supports trailing commas in list definitions.
  • Supports quoted keys in resources.

0.1.2

  • Better support for inherited scenes

0.1.1

  • Support for use_tree() with inherited scenes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

godot_parser-0.1.6.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

godot_parser-0.1.6-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file godot_parser-0.1.6.tar.gz.

File metadata

  • Download URL: godot_parser-0.1.6.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for godot_parser-0.1.6.tar.gz
Algorithm Hash digest
SHA256 397b552b32537e5bb91ffc0defdf849ae50f2d7bdee0732fdf175ca8f61e20e2
MD5 6e53d78f0ecef06efbaec6b6eb9e7c06
BLAKE2b-256 d67202ed50f9caf9c607ece38e7a1dc0bad18c9d35a3ba685daa11a3f9c499a3

See more details on using hashes here.

File details

Details for the file godot_parser-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: godot_parser-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for godot_parser-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d84f22db6658bc13c118f5133dd4770297c710649e7a9b3f6cecd2e6ffe5bba0
MD5 e359979ab77bdad019e99b93dde7e890
BLAKE2b-256 e3eee01541ed732c97203a8819075b06d83f6c0d94b9e32fe3b5879a1f76354d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page