pthom / imgui_bundle

Your fast track to powerful GUIs: Dear ImGui Bundle, an extensive toolkit for Python and C++ with immediate mode efficiency.
https://pthom.github.io/imgui_bundle/
MIT License
700 stars 72 forks source link

[imgui_node_editor] Can't set value for StyleVar.node_padding #204

Closed bgribble closed 7 months ago

bgribble commented 7 months ago

Attempting to set any value for StyleVar.node_padding throws an error from a null pointer assert.

This may be because the enum value for node_padding is 0?

[   2.979    gui]   File "/home/grib/devel/mfp/.virtual/lib/python3.11/site-packages/mfp/gui/imgui/processor_element.py", line 30, in render
[   2.979    gui]     nedit.push_style_var(nedit.StyleVar.node_padding, 1)
[   2.979    gui] RuntimeError: IM_ASSERT( var != nullptr )   ---   imgui_node_editor.cpp:5631
hugle commented 7 months ago

I use get_style() and has no problem, but you will need to set x, y, z, w one by one in python as no API binding for python for direct assignment:

padding_list = [1.0,1.0,1.0,1.0]
style = ed.get_style()
style.node_padding.x = padding_list[0]
style.node_padding.y = padding_list[1]
style.node_padding.z = padding_list[2]
style.node_padding.w = padding_list[3]
pthom commented 7 months ago

Hello,

node_padding is a ImVec4, thus you should use

    ed.push_style_var(ed.StyleVar.node_padding, ImVec4(1, 1, 1, 1))

or

    style = ed.get_style()
    style.node_padding = ImVec4(8, 8, 8, 8)
hugle commented 7 months ago

Thanks @pthom , if ImVec4 is prefered. I would recommend update some of the imgui API (example below) from List[float] to ImVec4. Otherwise conversion between them is required, that would be redundant:

https://github.com/pthom/imgui_bundle/blob/f9d61bfe10ff2d13b0e08bee9eaf18639088d10b/bindings/imgui_bundle/imgui/__init__.pyi#L1429-L1438

pthom commented 7 months ago

@hugle: This example is different. Here you are trying to edit a list of float. In the ImGui code, the declaration is as below:

bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags)

It is true that in some cases you can see C++ code like this

ImGui::DragFloat4(const char* label, &someImVec4Value)

However, this uses the fact that the memory layout of ImVec4 and float[4] happens to be the same in C and C++. There is no such thing in Python, and IMHO this is for the better