Skip to main content

A python library to parse and dump Unity YAML files

Project description

Unity YAML Parser

travis

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
hero_prefab_file = 'UnityProject/Assets/Prefabs/Hero.prefab'
doc = UnityDocument.load_yaml(hero_prefab_file)
for entry in doc.entries:
  if entry.__class__.__name__ == 'MonoBehaviour' and getattr(entry, 'MaxHealth', None) is not None:
    entry.MaxHealth = str(int(entry.MaxHealth) + 10)
    break
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.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

unityparser converts every scalar value in the YAML to string when loading the data for safety purposes. Any key from the mappings in the YAML included.

You have to make sure to convert every modified/added value(or dict key) in the loaded data object back to string before dumping the document. Otherwise the written YAML will contain ugly tags.

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.

Unity scenes are not yet supported. It shouldn't require much effort to do so.

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-0.0.1.post1.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

unityparser-0.0.1.post1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file unityparser-0.0.1.post1.tar.gz.

File metadata

  • Download URL: unityparser-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for unityparser-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 e050e59ded57e70bbbe269a5ec2235df18c1ce098bf074238f11511b320cc3d7
MD5 0ca30e312becd45190ed8cac7eeb089e
BLAKE2b-256 03baf4f2e8ad1508139975493ad07052f9e562141ddf5cd1e65c2ffe728ccc6f

See more details on using hashes here.

File details

Details for the file unityparser-0.0.1.post1-py3-none-any.whl.

File metadata

  • Download URL: unityparser-0.0.1.post1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for unityparser-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 65ed777feb1d8056f561dd752926a106d2558633752de557fe4f919c236d0c90
MD5 5017910bc3427006974f77f68c8daf8d
BLAKE2b-256 afca1e3036f3820f2ee3d6d2c1689431741e47f6357a3511b748cb3877865ff8

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