Skip to main content

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:

  1. I want to keep all C++ examples and documentation as relevant as possible since I am lazy and don't want to rewrite everything.
  2. 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... */);
  1. This function returns true if the state changed
  2. v_current_min and v_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_imgui_redux-3.3.0.tar.gz (3.8 MB view details)

Uploaded Source

Built Distributions

py_imgui_redux-3.3.0-cp312-cp312-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.12 Windows x86-64

py_imgui_redux-3.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

py_imgui_redux-3.3.0-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11 Windows x86-64

py_imgui_redux-3.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

py_imgui_redux-3.3.0-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

py_imgui_redux-3.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

py_imgui_redux-3.3.0-cp39-cp39-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

py_imgui_redux-3.3.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

py_imgui_redux-3.3.0-cp38-cp38-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

py_imgui_redux-3.3.0-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

py_imgui_redux-3.3.0-cp37-cp37m-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.7m Windows x86-64

py_imgui_redux-3.3.0-cp37-cp37m-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

File details

Details for the file py_imgui_redux-3.3.0.tar.gz.

File metadata

  • Download URL: py_imgui_redux-3.3.0.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.6

File hashes

Hashes for py_imgui_redux-3.3.0.tar.gz
Algorithm Hash digest
SHA256 326793bcb6216afa8bca6caad8a43f771a201200f7d3ba08319c3023f796a061
MD5 c82ddce949824ab66e029dad5227bce6
BLAKE2b-256 90ab747dba10eea7242240a43526fe2f22fea56d459aa770e7d3d3fbb7b00261

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d49d671a2c9d3e3989063a19fb7390d4032bea9b1ade5d5467de6f84a5fe0ee2
MD5 560dd56677dfea3a3408448a011e9526
BLAKE2b-256 8ac72e184bca7ac2c497f26cec01305739d7919cfe610fa803e61ae27c57b6fc

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 530c58d99014a6461d8b1abc551c428f973ca5133105087469d3758027c21821
MD5 9c95367b20c39ec3ed63dfdcd1dd4552
BLAKE2b-256 2eaf651857fde02cd9de5de70a535442e44734cfc5df9bed589d18c780b861db

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 43ad5f4351e14b1f78cb81b5102737b1f91c747411d1579130fe91abdf8829b8
MD5 bdb6657db64fb5ee015b3f1b3ef5cccb
BLAKE2b-256 e5f7f15ce44fdb7af766c6b3acdbf7bbb2fa0a0431ad7c1dfd3ab3ba69c1eb72

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 49a218d4f270ac6717d70116cfebacd61b542738f85a1041d6537f459327ed43
MD5 0f5afd6e4577e9b76f23c1ecfc8340d2
BLAKE2b-256 ac3fc64a9f697366a306f6b7d5a9bd07308d7be4dae6db586bcfcb479f540f85

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e8db06e04cbc910a04dc30d45a580641d02e2904697067eaff0cc6d6ba8e6d56
MD5 2bade3edb1fc28c0c9f317f609a2b4b4
BLAKE2b-256 7dc0b383cb3d1662d09fde9072215f8048e2f2c3b59ed5bf781021d8d348db7f

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 72fadb2e8bc8186d33de9e3c7781113c073e69492f5e4f3e5cbc6533f683ac2f
MD5 fea56bc7284508c77cba1e0b5a5c5536
BLAKE2b-256 b6e557e839d291f92f20690475cd102e354fd325dd71aad0e9be694ad153565c

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 538e0e2781ee1894e0615efa9693f88c72091bbc398b889f132c491adf820905
MD5 5bd13fb294c1364089495b0457d91f5e
BLAKE2b-256 f3d50e962a8a1340fca56af8996c54e6b83b24816790aaddfe64282e9c0d0d08

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ab8d202cd95742eaeb82153db31c61eece3cb88375a5262277a0351999cf1494
MD5 0f9338dd9ca809e4a974171b9d4ae3f7
BLAKE2b-256 87a1f294e3883bba331a11595d1f0e7334a6cb091fead06bc147a02e5faf0681

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 50a1ba2f1e76e5366746101f0f507572b4f75ebf7751f2b4c5548f1e6fcc9dfe
MD5 157fd410560c67f95e862c68b63dda96
BLAKE2b-256 a899e30b8678cf5d4201f0b49e801acdc197e0f4afc4a62b1c9509a7bcb61180

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 89849d30fe184514048dd84ec41b32d5b067678ac20b6c0237f85d2895c690fc
MD5 dd7a051fff9b44c7130b2dce3c5e7bed
BLAKE2b-256 569aeee7edd4214f0f744f3cc232f78f630fe853d5200bd5b28ea0d42b4827ca

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 2d0861e3eb3e7622a5286fd953edba51d6765ce651c58c1b336f5ecfa0f1d162
MD5 333c4b70f154b71f6f2893f8fce90a2c
BLAKE2b-256 bf4ef9076769577a13367dffc2cac8957beadeabc0e31235372a4955d7e8ddf6

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.3.0-cp37-cp37m-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.3.0-cp37-cp37m-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d06fbb047bf7bba5a9485cddad79781453f4273a18fa4d839883b908d25639bb
MD5 74f58cfd95159ba243fa6f5c3727d2ac
BLAKE2b-256 fd47a2b213536320560884e3f9f7dcf3983bc9e822183057d7e8af82bf054960

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