Package: godot-parser
Godot ParserThis 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 APIgodot_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:fromgodot_parserimportGDScene,Nodescene=GDScene()res=scene.add_ext_resource("res://PlayerSprite.png","PackedScene")withscene.use_tree()astree: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 thatuse_tree()doessupport 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 APILet's look at creating that same Player scene with the low-level API:fromgodot_parserimportGDFile,ExtResource,GDSection,GDSectionHeaderscene=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 ExamplesHere are some more examples of how you can use this library.Find all scenes in your project with a "Sensor" node and change thecollision_layer:importosimportsysfromgodot_parserimportloaddefmain(project):forroot,_dirs,filesinos.walk(project):forfileinfiles:ifos.path.splitext(file)[1]=='.tscn':update_collision_layer(os.path.join(root,file))defupdate_collision_layer(filepath):scene=load(filepath)updated=Falsewithscene.use_tree()astree:sensor=tree.get_node('Sensor')ifsensorisnotNone:sensor['collision_layer']=5updated=Trueifupdated:scene.write(filepath)main(sys.argv[1])CaveatsThis was written with the help of theGodot 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 thetest_parse_files.pyscript. 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.Changelog0.1.7Add py.typed file to provide type information#8,#10Allow colon in key names#9Drop support for python 3.60.1.6Fix errors when using new version of pyparsing0.1.5Fix quote escaping during serialization#7Better error message for binary scenes#60.1.4Supports node groups#50.1.3Supports trailing commas in list definitions.Supports quoted keys in resources.0.1.2Better support for inherited scenes0.1.1Support foruse_tree()with inherited scenes