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.4.1.tar.gz (3.9 MB view details)

Uploaded Source

Built Distributions

py_imgui_redux-3.4.1-cp313-cp313-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.13Windows x86-64

py_imgui_redux-3.4.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.3 MB view details)

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

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

Uploaded CPython 3.12Windows x86-64

py_imgui_redux-3.4.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.3 MB view details)

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

py_imgui_redux-3.4.1-cp311-cp311-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.11Windows x86-64

py_imgui_redux-3.4.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.3 MB view details)

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

py_imgui_redux-3.4.1-cp310-cp310-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.10Windows x86-64

py_imgui_redux-3.4.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.3 MB view details)

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

py_imgui_redux-3.4.1-cp39-cp39-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.9Windows x86-64

py_imgui_redux-3.4.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.3 MB view details)

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

py_imgui_redux-3.4.1-cp38-cp38-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.8Windows x86-64

py_imgui_redux-3.4.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.3 MB view details)

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

File details

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

File metadata

  • Download URL: py_imgui_redux-3.4.1.tar.gz
  • Upload date:
  • Size: 3.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for py_imgui_redux-3.4.1.tar.gz
Algorithm Hash digest
SHA256 c35d38092f3b69c6901cd4246e1120a5a5c5d551e465be61d03c30d076e9d9be
MD5 d32ebdbde3383c533f6a5e7831df2444
BLAKE2b-256 72dba49775bf8113a4c254c172f39e6827fd40b38061ad07b8b40c4434242dc8

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.4.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b255a2af0f074b928b2829140c347c291c16e2256f45e7733b77cf15eef2d9f7
MD5 132d360048041da07f2d22623e43ef34
BLAKE2b-256 5cb62d2a700cdb4d96dde74a8bc0abffb4de0172d89bab2921496f0f2e46f30c

See more details on using hashes here.

File details

Details for the file py_imgui_redux-3.4.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 828c59bac6ee619e4abb8138c3dc0d37df7acabdb752a08c0223466d229cf296
MD5 e8d5d91569e0da600fec3c7e98fdc7f3
BLAKE2b-256 4e49ace81ca543f7d4f6c2846bd05a0b3892ff8faa8a6c8ba5c61c9ea030d227

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3d238b124cf3e3db9faf3d8e5a1db5a9e265d6a6d0b6b7d3b71a14acff8c1c27
MD5 e44b98a4a0a67bbf72f8a184305ab650
BLAKE2b-256 b92a69b7f71a8d7e9ab35f767b909a08788290083260cb8054a763ee61df7b5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4edd058172497308ec1a37e9de5aab59e5f78b75883a71a9431af9af0325e61a
MD5 5d7af4b3dc46db8473f7c98f8f9d20c0
BLAKE2b-256 238d226816b282c2b48768eca59eefb31080e193e496175b5ff93d8a7a78a0dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8f17cea08bd1ee039ea4cba78b2f918355dbad5ba0bd6b71f96ecc34bb0db7e4
MD5 80cb85d2870622f0f824ab1e76024ab1
BLAKE2b-256 75b68f8b204ccb4479618e5af7bb16a487e9827387e02fd0f10efbd15dee8c0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1117900be5ab9cbbc6d53c8ca0dfa054effdbdb05d7107646d213d1be46f5f54
MD5 16e2abcdbbb94e0d56e74205c0458a3f
BLAKE2b-256 6bf79a2386f867b0ee001819c7552328af94b78eccdd2caeaf7f537d8b4ea659

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c7f264f793da89e6a21515c720b3a14b0de8b5a6965d7865cfe4ab636ba18d48
MD5 f2220090ab710b5795ded5d705e4891e
BLAKE2b-256 70a997c7bbb03eb003d285f47334986a54b32748c93d0a6595b48556220cfae9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 de3a08b57a98bdedf7dd323d3961dc2750aa20be1d55915e1421890568f52daf
MD5 31e53478bd97c7da599ae0379d191f9d
BLAKE2b-256 49955d9156d7ae346469ae9b8e1c83fc2b0cf0953b300466c782a6b3fab99ef3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8582ca8a9128e980de6dd809e8744c3389d87df390a7d31bf5187e34a9d3600f
MD5 b2aeb51a2d4b6283168d7e2b89149882
BLAKE2b-256 01db197b991175824619cd3d55d74ea611eb88cb7711a58feb1bda787a9568fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 443031ff87bdc4b7e5dd7ef0b1a18bb400ceeccfba155d23fd709bd0ef0409ff
MD5 974565f862ec2b042583e92c166d60a9
BLAKE2b-256 5c7284edd5e092717ee5a99aa2079abcd0943aed31a613f61852498a12760ef3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 17636303c10ca0b377a30c90dd12ca40b930378fb3966700d9493f0aa7881037
MD5 cd7bf8df69a8e545effa327ad3c40981
BLAKE2b-256 ed89eb9b444852f3e6be64496eaeccec794c65a5aabe45a222b86707fa94cf1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_imgui_redux-3.4.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bc20ab01f632abc378a31b75607bce01808028d87629edb94dd38565bb027e63
MD5 68af934bf5aae97f0e94a1d57661756f
BLAKE2b-256 27397ca432bcc507a80ef8066107eb4af17dd71e97c6724d34aa55f2beeaf62d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page