Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[node-editor]How to update the image of node2 on the second click #2458

Closed
monkeycc opened this issue Dec 30, 2024 · 3 comments
Closed

[node-editor]How to update the image of node2 on the second click #2458

monkeycc opened this issue Dec 30, 2024 · 3 comments

Comments

@monkeycc
Copy link

image

How to update the image of node2 when selecting an image for the second time

import dearpygui.dearpygui as dpg
import numpy as np
import cv2
import tkinter as tk
from tkinter import filedialog

dpg.create_context()

# callback runs when user attempts to connect attributes
def link_callback(sender, app_data,):
    # app_data -> (link_id1, link_id2)
    dpg.add_node_link(app_data[0], app_data[1], parent=sender)
    print(sender)
    print(app_data[0])
    print(app_data[1])

    dpg.set_value("texture_tag2",dpg.get_value("texture_tag"))

# callback runs when user attempts to disconnect attributes
def delink_callback(sender, app_data):
    # app_data -> link_id
    dpg.delete_item(app_data)

def select_image():
    # Create a Tk root window but don't display it
    root = tk.Tk()
    root.withdraw()

    # Make the window topmost
    root.call('wm', 'attributes', '.', '-topmost', True)
    
    # Create file selection dialog with image file filter
    file_path = filedialog.askopenfilename(
        title="Select Image File",
        filetypes=[("Image Files", "*.jpg;*.jpeg;*.png;*.gif;*.bmp")]
    )
    if file_path:
        print("Selected file:", file_path)
        image = cv2.imread(file_path)
        image = cv2.resize(image, (200, 200))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
        image = np.true_divide(image, 255.0)

        dpg.set_value("texture_tag", image)
    else:
        print("No file selected")  



texture_data = []
texture_data2 = []
width = 200
height = 200

# Generate all-black texture data
for i in range(0, width * height):
    texture_data.append(0)
    texture_data.append(0)
    texture_data.append(0)
    texture_data.append(1)

    texture_data2.append(0)
    texture_data2.append(0)
    texture_data2.append(0)
    texture_data2.append(1)

with dpg.texture_registry(show=False):
    dpg.add_dynamic_texture(width=200, 
                           height=200, 
                           default_value=texture_data, 
                           tag="texture_tag")
    
with dpg.texture_registry(show=False):
    dpg.add_dynamic_texture(width=200, 
                           height=200, 
                           default_value=texture_data2, 
                           tag="texture_tag2")

with dpg.window(label="Tutorial", width=800, height=400):

    with dpg.node_editor(callback=link_callback, delink_callback=delink_callback,minimap=True, minimap_location=dpg.mvNodeMiniMap_Location_BottomRight):


        with dpg.node(label="Node 1", pos=[0, 0]):
            with dpg.node_attribute(label="Node A1", attribute_type=dpg.mvNode_Attr_Static):
                dpg.add_button(
                    label='Select Image',
                    width=200,
                    callback=select_image,
                )
            with dpg.node_attribute(
                    attribute_type=dpg.mvNode_Attr_Output,
            ):
                dpg.add_image("texture_tag")

        with dpg.node(label="Node 2", pos=[400, 0]):
            with dpg.node_attribute(label="Node A2", attribute_type=dpg.mvNode_Attr_Input):
                dpg.add_text(default_value='Input BGR Image')
                dpg.add_image("texture_tag2")

dpg.create_viewport(title='Custom Title', width=900, height=500)
dpg.setup_dearpygui()

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
@monkeycc monkeycc changed the title How to update the image of node2 on the second click [node-editor]How to update the image of node2 on the second click Dec 30, 2024
@v-ein
Copy link
Contributor

v-ein commented Dec 31, 2024

Doesn't your example "just work"? What issues are you experiencing?

@v-ein
Copy link
Contributor

v-ein commented Dec 31, 2024

Scratch that... I see it now, you want to update "texture_tag2".

I'd say if your application should let the user build an arbitrary processing graph, you should have a function like recalculate() that re-calculates images according to the links currently in the graph. Then, you'd call it at the end of link_callback, delink_callback, and select_image. The function would take all input nodes (in your case, texture_tag), look at what links they have, and update every connected node.

@monkeycc
Copy link
Author

monkeycc commented Jan 2, 2025

thank you
There are too few main tutorials
I need to explore on my own

@monkeycc monkeycc closed this as completed Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants