Generate Renderdoc Python-API Stubs | Blurred code

Generate Renderdoc Python-API Stubs

2023/06/17

LastMod:2023/06/17

Categories: CG

Which Mode?

Renderdoc has a friendly Python API for writing custom scripts. It allows both modes:

Generate_Renderdoc_Python_Stubs-2023-06-17-01-06-14

With custom Python scripts, developers have free access to all the data stored in a capture, and can analyze some additional information, such as how many drawcalls in a pass, how many textures/render targets are used, and sort them by their sizes in top-down order when analyzing some bottlenecks about VRAM/memory.

Python lib

The installer from official website doesn't include Renderdoc Python libraries, which means that only Mode-1 is allowed(RenderDoc has an embedded Python3.6.4 which is distributted with the installer). We have to build RenderDoc from source if Python libraries are needed. The good news is that maintainers do a great job, and it's very easy to build it. With a MSVC environment, it's just a simple click on the green triangle, and that's enough.

After build, check <PATH>\renderdoc-1.26\x64\Development\pymodules for renderdoc libs. There should be renderdoc.pyd and qrenderdoc.pyd(on Linux it may be renderdoc.so/qrenderdoc.so) in it.

How to Generate Stubs?

Official documentations provides a basic description about the dev environment and a detailed reference about the Python API. Renderdoc is not a native Python application, which is actually based on cpp. It employs Swig to generate its Python bindings, which make python linter difficult to work with. Whatsworse, it doesn't provide any Python stub files. Instead, it provides an Pycharm Patch for developers to generate stubs by themselves.

Although I'm a big fan of JetBrains and have a valid subscription of Pycharm, I feel uncomfortable with installing Pycharm just to generate stubs. Also, it has a strict version requirement(PyCharm 2020.3.2), which is not friendly. So I decide to find other ways to generate it.

There are some others facing the same problem, and uploads their result to github, such as this repo. However, neither it is up-to-date nor it provides any instructions about how to regenerate it on latest version.

https://github.com/Lex-DRL/renderdoc-py-stubs/blob/master/_skeletons_combined/renderdoc.py

After some search, I found mypy provides a tool stubgen to generate stubs from pyd . It's relatively easy to use.

pip install --user mypy
cd <PATH>\renderdoc-1.26\x64\Development\pymodules
stubgen --parse-only -m renderdoc
stubgen --parse-only -m qrenderdoc

It should generate renderdoc.pyi and qrenderdoc.pyi in the folder, which holds API infos of Renderdoc libraries.

Copy the two pyi to typings folder in the same level with our Renderdoc scripts, Python linters will found them.

.
├── iter_actions.py
└── typings
    ├── qrenderdoc.pyi
    └── renderdoc.pyi

From now on, although not perfect(it doesn't provide any type information of function return values), we have a basic auto-completion.

Generate_Renderdoc_Python_Stubs-2023-06-17-00-59-00

Info

For those who just want to take and go, there are two files generated by me from RenderDoc v1.26 available: https://gist.github.com/BlurryLight/5718987e699515ba480ba823c113ca83