From a07c25597f3c1eed656bf23b7fc00ca1ecdb168b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 12 Aug 2019 10:55:41 +0200 Subject: [PATCH] Added a code example to the README.md --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index c01380f..bfdcc77 100644 --- a/README.md +++ b/README.md @@ -49,3 +49,34 @@ The code statically type-checked with [mypy](http://mypy-lang.org/). Mypy likes to see the return type of `__init__` methods explicitly declared as `None`. Until issue [#604](https://github.com/python/mypy/issues/604) is resolved, we just do this in our code too. + + +## Code Example + +BAT can be used as a Python library to inspect the contents of blend files, without having to +open Blender itself. Here is an example showing how to determine the render engine used: + + #!/usr/bin/env python3.7 + import sys + from pathlib import Path + + from blender_asset_tracer import blendfile + from blender_asset_tracer.blendfile import iterators + + if len(sys.argv) != 2: + print(f'Usage: {sys.argv[0]} somefile.blend', file=sys.stderr) + sys.exit(1) + + bf_path = Path(sys.argv[1]) + bf = blendfile.open_cached(bf_path) + + window_managers = bf.find_blocks_from_code(b'WM') + assert window_managers, 'The Blend file has no window manager' + window_manager = window_managers[0] + windows = window_manager.get_pointer((b'windows', b'first')) + + for window in iterators.listbase(windows): + scene = window.get_pointer(b'scene') + break + + print(scene[b'r', b'engine'].decode('utf8'))