Skip to main content

A python library to parse and dump Unity YAML files

Project description

Unity YAML Parser

This project aims to provide a python3 API to load and dump Unity YAML files(configurations, prefabs, scenes, serialized data, etc) in the exact same format the internal Unity YAML serializer does.

Using this API you will be able to easily manipulate(as python objects) Unity YAML files and save them just the same, keeping the YAML structure exactly as Unity does. This has the advantages of, first not having to configure PyYAML beforehand to deal with Unity YAMLs, and second as the modified file keeps the same structure and formatting that Unity does, when the YAML file is loaded by Unity it won't make formatting changes to it that will make any VCS report unexpected file changes.

Installing

Install and update using pip:

pip install -U unityparser

A Simple Example

from unityparser import UnityDocument

# Loading and modifying a config file with a single YAML document
project_settings_file = 'UnityProject/ProjectSettings/ProjectSettings.asset'
doc = UnityDocument.load_yaml(project_settings_file)
ProjectSettings = doc.entry
ProjectSettings.scriptingDefineSymbols[1] += ';CUSTOM_DEFINE'
ProjectSettings.scriptingDefineSymbols[7] = ProjectSettings.scriptingDefineSymbols[1]
doc.dump_yaml()

# You can also load YAML files with multiple documents and filter for a single or multiple entries
hero_prefab_file = 'UnityProject/Assets/Prefabs/Hero.prefab'
doc = UnityDocument.load_yaml(hero_prefab_file)
# accessing all entries
doc.entries
# [<UnityClass>, <UnityClass>, ...]
# accessing first entry
doc.entry
# <UnityClass>
# get single entry uniquely defined by filters
entry = doc.get(class_name='MonoBehaviour', attributes=('m_MaxHealth',))
entry.m_MaxHealth += 10
# get multiple entries matching a filter
entries = doc.filter(class_names=('MonoBehaviour',), attributes=('m_Enabled',))
for entry in entries:
    entry.m_Enabled = 1
doc.dump_yaml()
# calling entry method for a doc with multiple document will return the first one
print(doc.entry.__class__.__name__)
# 'Prefab'

Classes

unityparser.UnityDocument

Main class to load and dump files.

unityparser.UnityDocument.load_yaml(file_path)

Classmethod: Load the given YAML file_path and return a UnityDocument file

unityparser.UnityDocument.dump_yaml(file_path=None)

Dump the UnityDocument to the previously loaded file location(overwrite). If file_path argument is provided, dump the document to the specified location instead.

This method keeps line endings of the original file when it dumps.

unityparser.UnityDocument.entries

Property: Return the list of documents found in the YAML. The objects in the list are of types Class named after the serialized Unity class(ie. MonoBehaviour, GameObject, Prefab, CustomName, etc).

unityparser.UnityDocument.entry

Property: Return the first document in the YAML, useful if there is only one. Equivalent of doing UnityDocument.entries[0].

unityparser.UnityDocument.get(class_name=None, attributes=None)

Method: Return a single entry uniquely matching the given filters. Must exist exactly one.

unityparser.UnityDocument.filter(class_names=None, attributes=None)

Method: Return a list of entries matching the given filters. Many or none can be matched.

unityparser.loader.UnityLoader

PyYAML's Loader class, can be used directly with PyYAML to customise loading.

unityparser.dumper.UnityDumper

PyYAML's Dumper class, can be used directly with PyYAML to customise dumping.

Considerations

Text scalars which are single or double quoted that span multiple lines are not being dumped exactly as Unity does. There's a difference in the maximum length allowed per line and the logic to wrap them.

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

unityparser-2.2.2.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

unityparser-2.2.2-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file unityparser-2.2.2.tar.gz.

File metadata

  • Download URL: unityparser-2.2.2.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.2 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/5.1.0 keyring/23.11.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.16

File hashes

Hashes for unityparser-2.2.2.tar.gz
Algorithm Hash digest
SHA256 70ff2118d087b7169ba02b9643d1915614d2147fa4fc4e1c7035ec97f4022f35
MD5 d1f5534c0eb974b0005bd25da890a1dd
BLAKE2b-256 469198cb21734509e96effa0df835aa683ca1ecfeed544ad444f5ca18bb89585

See more details on using hashes here.

File details

Details for the file unityparser-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: unityparser-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.2 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/5.1.0 keyring/23.11.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.9.16

File hashes

Hashes for unityparser-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d09e8c2a656a79256905ae269b62ac3fb316aecea83d7d7342583c1264f0887a
MD5 1933479e568a322351b0943eba820a74
BLAKE2b-256 1b4670a1254d229fdd201b47610c9792c3b3f90be644f65578d85ce85a75485f

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