跳转到主要内容

Godot场景文件解析的Python库

项目描述

Godot解析器

Build Status Coverage Status Downloads

这是一个用于解析Godot场景文件(.tscn)和资源文件(.tres)的Python库。它旨在使自动编辑场景文件或Godot中的资源更容易。

高级API

godot_parser有大约两个级别的API。低级API没有Godot特定的逻辑,只是文件格式的简单包装器。

高级API在顶部添加了一些应用逻辑来模拟Godot功能,并使执行某些任务更加容易。让我们通过为玩家创建一个新的场景文件来查看一个示例

  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")

当可用时,使用高级API会更容易,但它并不涵盖所有内容。请注意,use_tree() 确实支持继承的场景,并且通常按预期工作(例如,父场景中的节点将可用,并且对子场景的编辑将正确覆盖字段)。目前尚不支持更改场景的继承。

低级API

让我们看看如何使用低级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")

您可以看到,这需要您自己管理更多的应用逻辑,例如资源ID和节点结构,但它可以用于创建任何类型的TSCN文件。

更多示例

以下是更多如何使用此库的示例。

在您的项目中找到所有包含“传感器”节点的场景,并更改 碰撞层

  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])

注意事项

本文档是在 Godot TSCN 文档 的帮助下编写的,但它主要基于对我在工作中使用的 Godot 文件的视觉检查。如果您发现 godot_parser 无法处理的场景或它不支持的功能,请提交一个包含场景文件和所需行为的解释的问题。如果您想深入研究并提交一个 pull request,那就更好了!

如果您想对这个工具进行快速检查,可以使用 test_parse_files.py 脚本。传入您的 Godot 根目录,它将验证是否可以正确解析和重新序列化项目中所有场景和资源文件。

更新日志

0.1.7

  • 添加 py.typed 文件以提供类型信息 #8#10
  • 允许在键名中使用冒号 #9
  • 停止支持 Python 3.6

0.1.6

  • 修复使用新版本 pyparsing 时的错误

0.1.5

  • 修复序列化过程中的引号转义错误 #7
  • 为二进制场景提供更好的错误消息 #6

0.1.4

  • 支持节点组 #5

0.1.3

  • 支持列表定义中的尾随逗号。
  • 支持资源中的引号键。

0.1.2

  • 更好地支持继承场景

0.1.1

  • 支持继承场景的 use_tree()

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

godot_parser-0.1.7.tar.gz (17.1 kB 查看哈希值)

上传时间

构建分发

godot_parser-0.1.7-py3-none-any.whl (17.2 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面