A python wrapper for DearImGUI and popular extensions
Project description
PyImGui
DearImGui wrapper for python made with PyBind11
Read below for adjustments made to the standard APIs. Otherwise, all documentation from the original libraries remains 100% valid. Check out the examples folder for some concrete code.
Install
Install the latest version with pip
pip install py-imgui-redux
Modules:
imgui
- Core DearImGUI
imgui.implot
- ImPlot library
imgui.imnodes
- ImNodes library
imgui.glfw
- GLFW Bindings
Backends:
This module only uses the GFLW+OpenGL3 backend. imgui.glfw
provides full access to GLFW's API, see below for it's adjustments
API Adjustments
I am writing this library with the primary goal of keeping the original Dear ImGui functional API as intact as possible. This is because:
- I want to keep all C++ examples and documentation as relevant as possible since I am lazy and don't want to rewrite everything.
- I have a love-hate relationship with snake-case.
However, there are some minor compromises that have to be made in order to make this happen, primarily in the case of pointers and lists.
Pointers
Take for instance the function:
bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, /* other args... */);
- This function returns true if the state changed
v_current_min
andv_current_max
are pointers to state, and will be read and updated if a change is made
Typical C++ usage
int min = 0;
int max = 5;
// Code ...
if(imgui::DragIntRange2("Label", &min, &max))
{
// Code that happens if a change was made
}
Python, however, will not let you pass an integer by reference normally, let alone across the C API. Therefore, the py-imgui-redux method of accomplishing this:
min_val = imgui.IntRef(0)
max_val = imgui.IntRef(5)
# Code ...
if imgui.DragIntRange2("Label", min_val, max_val):
# Code that happens if a change was made
pass
These are thin wrappers around a single value.
imgui.IntRef
imgui.FloatRef
imgui.BoolRef
# The value can be accessed like so
myNum = imgui.IntRef(25)
myNum.val += 2
Lists
Take for instance the function
bool DragInt3(const char* label, int v[3], /* args ... */);
A standard python list is stored sequentially in memory, but the raw values themselves are wrapped in a python object. Therefore, we cannot easily iterate over just the ints/floats, let alone get a pointer to give to ImGui. PyBind11 will happily take a python list and turn it into a vector for us, but in doing so requires making a copy of the list (not ideal for large lists)
This is solved in one of two ways.
Method 1: py-imgui-redux Wrappers
vals = imgui.IntList([0, 5, 10])
if imgui.DragInt3("Label", vals):
# updating code
pass
These are thin wrappers around a C++ vector. They have standard python list access functions and iteration capabilities.
imgui.IntList
imgui.FloatList
imgui.DoubleList
x = imgui.IntList()
x.append(25)
x.append(36)
print(len(x))
for val in x:
print(x)
x[0] = 12
See their docs for more information and all functions.
Functions that mutate the data, such as vanilla ImGui widgets will use this method.
Method 2: Numpy Arrays
import numpy as np
xs = np.array([0, 5, 10])
ys = np.array([0, 5, 10])
# Code...
implot.PlotScatter("Scatter", xs, ys, len(xs))
The implot submodule uses these, as they prevent the need to copy potentially large arrays, and implot functions will not need to change the data as it reads it. Numpy is also easier to use for data manipulations as is typical with plotting.
Thirdly, references to strings are handled similarily to lists (it's actually a subclass of the List wrappers).
Take for instance the function
bool InputText(const char* label, char* buf, size_t buf_size, /* args ... */);
Which takes a pointer to the IO buffer, and also and argument for its size.
In Python:
myStr = imgui.StrRef("This is a string", maxSize=20)
# Code ...
if imgui.InputText("Label", myStr):
# code if the text changes
pass
Notice that you don't need to pass the size, this is baked into the StrRef.
Note: maxSize
automatically takes into account string terminators, i.e. maxSize=20
means
your string can hold 20 chars.
To change the maxSize:
myStr.resize(25)
Changing the size lower will drop any extra chars.
To get your string back
# make a copy
x = str(myStr)
# or
x = myStr.copy()
# get a temporary/unsafe pointer
# useful for printing large strings without copying
# only use said pointer while the object exists
# lest ye summon the dreaded seg-fault
print(myStr.view())
Images
Loading images for rendering is simple
import imgui
texture = imgui.LoadTextureFile("myImage.jpg")
imgui.Image(texture, imgui.ImVec2(texture.width, texture.height))
# ...
# Eventually
glfw.UnloadTexture(texture)
# texture can no longer be used without a call to LoadTexture
Image file loading is handled via stb_image and supports various common file formats. Alternatively, if you wish to do some manual image processing, you can use PILLOW or OpenCV (or any other image processing library... probably)
Important Note: LoadTexture
and LoadTextureFile
can only be called after both imgui and glfw have been initialized otherwise openGL will segfault
OpenCV Example
import imgui
import cv2
image = cv2.imread("myImage.jpg", cv2.IMREAD_UNCHANGED)
# cv2.IMREAD_UNCHANGED is important for files with alpha
# Have to convert the colors first
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# If your image has alpha: cv2.COLOR_GBRA2RGBA
texture = imgui.LoadTexture(image.tobytes(),
image.shape[1],
image.shape[0],
image.shape[2])
PILLOW Example
import imgui
from PIL import Image
image = Image.open("myImage.jpg")
texture = imgui.LoadTexture(image.tobytes(),
image.size[0],
image.size[1],
len(image.getbands()))
GLFW API Adjustments
This wrapper aims to be as close to the original API as possible. Exceptions:
- Functions have lost the
glfw
prefix as this is already in the module name - Functions that returned pointers to arrays now return list-like objects
- Functions that took pointers to output variables as arguments now return tuples
Build Dependencies
Debian/apt
libx11-dev libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libgl-dev
Fedora/yum
libXrandr-devel libXinerama-devel libXcursor-devel libXi-devel mesa-libGL-devel
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file py_imgui_redux-3.2.1.tar.gz
.
File metadata
- Download URL: py_imgui_redux-3.2.1.tar.gz
- Upload date:
- Size: 3.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 539a40362559e55fa8c54249860ad1d3c0fc5b42cad71ebf9d295b62e8fdf093 |
|
MD5 | 16cec0ed68173a9786326e87d6fc6a9a |
|
BLAKE2b-256 | 353a560e967554f4fff1228bea648bb6199731479bfc7b6b18328526cc158d48 |
File details
Details for the file py_imgui_redux-3.2.1-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a404b9f87704e04d860c3c753b3766ef820a5da7eedd2dfa1392ca758ef99126 |
|
MD5 | 76037507014cb01cee58ef4a9a0b4a95 |
|
BLAKE2b-256 | 4c12792e05a9556a6fc1c88b63644093ce6cb90bb82c138b1865af0a7317f216 |
File details
Details for the file py_imgui_redux-3.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.12, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78b03140ee00ee4958196464844cadd420de1af6787eaf2ea06dd774b59f3557 |
|
MD5 | f52e48eb53a21317a52ea4d31cdd02ea |
|
BLAKE2b-256 | ea7acf391908b342c5e0c813ec52bf9306552458cc695d126b9fced09f2be647 |
File details
Details for the file py_imgui_redux-3.2.1-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72925641afbee12444a9735db45b455b2869194273f75c99a68312c5fcfcb465 |
|
MD5 | 853961cf6263e232b58ebbd41fc36627 |
|
BLAKE2b-256 | 9690c18ce3d733c236b9f381f5ee4cefbf8827670200dd656dfd744967cb64d2 |
File details
Details for the file py_imgui_redux-3.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.11, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 693a43f5806267c9ea5876952f95c129700b23be98ba450c7e8f91532aa0eee0 |
|
MD5 | 834890325eff5a9f8caacaebbbf69a3e |
|
BLAKE2b-256 | 2d3b4aa83ceda35d97d350046fef3dd16a358731c291ad1744ae014cea35a129 |
File details
Details for the file py_imgui_redux-3.2.1-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f57ff4dca54136574fd60a8f31598c6bd770cff8718f54ecd0fdb0a1e67e0b9 |
|
MD5 | bd4c5f3597235e412b2538ed77562226 |
|
BLAKE2b-256 | 35b0a2233de447dc1682066014d66e71f6d28e9d8f5980dcb2921d37347a5884 |
File details
Details for the file py_imgui_redux-3.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.10, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d70572ee6fffbefc5b7a5ecb26ef94b62449515c0361725e1c8aa37cecc6caa2 |
|
MD5 | 9541b03dca39420b1d0460efc03847be |
|
BLAKE2b-256 | 22a15d6aebb4b5aaa65d7df4f67e518ce448b412a3b62f8165e82de103c68aeb |
File details
Details for the file py_imgui_redux-3.2.1-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f63845eb3dd25aefd575ce85cd327084ad99b6e2fa4566ff709237a2dca7e8f |
|
MD5 | c198c952ee0d3a10f683fd7351da29d8 |
|
BLAKE2b-256 | ae684fd20dbd4e63347697791a91fdf014a7c2e27b0b8607222cd4b9b8cd655f |
File details
Details for the file py_imgui_redux-3.2.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.9, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81fc79d2083e58d933a3b432e81d9dc57b6eeab56e4bb609edd2c9315dc7e511 |
|
MD5 | c9880f947a7bb9c80c45b69d92dc3ae7 |
|
BLAKE2b-256 | 2e809b5ed699ad3dbd3e0c27aa7bcae4d7086c359a0d0d01c116ed17f617dd4d |
File details
Details for the file py_imgui_redux-3.2.1-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5371a01ad52683913036835c062569d689889deb0902f746b8caecf21040b44b |
|
MD5 | cdcfcfee2f9975c639bdd41c296eb5dd |
|
BLAKE2b-256 | 08ff1209ec004bde9f35badb3c4e5176caa0bfd1527b3dbde32e24d0a5557aa6 |
File details
Details for the file py_imgui_redux-3.2.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.8, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97f852a5ff3e2af8aa532b4c84667e40adc5ea24ea67010c84346e883830557b |
|
MD5 | 2bcd2541c5c047c335d883c3c0aa19de |
|
BLAKE2b-256 | b94d2934f51b5d56a93220a617ab1e99eb6b12afd82c9785d5efcff4a4700eaa |
File details
Details for the file py_imgui_redux-3.2.1-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5757766fdd459e1314adb57e1cd8b5060622bf8d4a3eb2d913893ff3de71abd8 |
|
MD5 | c129262e628242f104fcf60ed89b2c12 |
|
BLAKE2b-256 | 8375df7490fd686629bb1e184056beb8ee40456699f0b4a1de0964b5db2a9444 |
File details
Details for the file py_imgui_redux-3.2.1-cp37-cp37m-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: py_imgui_redux-3.2.1-cp37-cp37m-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.7m, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13db60f218efbac87eb1d88832d685172b74a5dadf21652bb5153ecb31a6f0bf |
|
MD5 | ab073136772f72b8e31e062d2a6853b7 |
|
BLAKE2b-256 | dfdca5eaabd0e41ac945492829f8c41045b47c2cebfba59e909bca9bc7eeb15a |