Python library for parsing Godot scene files
Project description
Godot Parser
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, ExtResource scene = GDScene() tex_id = 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": ExtResource(tex_id)}, ) ) scene.write("Player.tscn")
It’s much easier to use the high-level API when it’s available, but it doesn’t cover everything.
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): with open(filepath, 'r') as ifile: scene = load(ifile) updated = False with scene.use_tree() as tree: sensor = tree.root.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
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
Built Distribution
File details
Details for the file godot_parser-0.1.tar.gz
.
File metadata
- Download URL: godot_parser-0.1.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0661daf2774f6ca837afcf6d0c2f35e230b6f37d90a5d6e0b9ad6a5c9d0ec82 |
|
MD5 | 52f200660982eabe3b786f64ad5ae9d9 |
|
BLAKE2b-256 | 2d20dea3ad6b2981f25ba77f66b4bec29d607351e19d9b5da520824ca474205c |
File details
Details for the file godot_parser-0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: godot_parser-0.1-py2.py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63ab4de88e37a5a45ba981807346c667494b3f0033c03108d7ce0a4bc617d474 |
|
MD5 | f8893487dd96b6fe76509edbfc4c0c35 |
|
BLAKE2b-256 | 9ba5f17e8643757867d96dec5742b52203c1decaec6f158394e88877531650e9 |