grandchild
grandchild

Reputation: 1366

Documenting Blender Python code with Sphinx

Blender runs its own Python. When I write Python code like this

import bpy
print(bpy.data.objects)

for Blender it runs fine with

$ blender --background --python my_code.py
<bpy_collection[3], BlendDataObjects>

But when I want to document my code with Sphinx and the autodoc extension it cannot import bpy because it is unknown outside Blender.

$ cd doc/
$ make html
[...]
ModuleNotFoundError: No module named 'bpy'

How can I create documentation output for my Blender code with Sphinx?

Upvotes: 1

Views: 400

Answers (2)

Germanunkol
Germanunkol

Reputation: 230

There's another option: Using a fake (i.e. empty) bpy module: https://github.com/nutti/fake-bpy-module

You can install this simply via pip:

pip install fake-bpy-module-<version>

Afterwards, your Sphinx (i.e. the Python build used by Sphinx) will import these modules instead of the blender modules.

Note: I'm not the author and I don't know how this module works with Blender's GPL license.

Upvotes: 0

grandchild
grandchild

Reputation: 1366

The solution involves modifying the Makefile generated by sphinx-quickstart as well as writing a little wrapper script around sphinx.cmd.build:

Replace the SPHINXBUILD variable in the Makefile:

SPHINXBUILD = blender --background --python blender_sphinx.py --

and make sure that the actual recipe line at the end of the file contains the -M flag.

Then create a file blender_sphinx.py in the same directory as the Makefile with the following contents:

import sys
from sphinx.cmd import build

first_sphinx_arg = sys.argv.index('-M')
build.make_main(sys.argv[first_sphinx_arg:])

Now if you run

$ make html

from the doc/ directory it will allow autodoc to find bpy and import all the modules.

Upvotes: 1

Related Questions