-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode_pulse.gd
More file actions
129 lines (90 loc) · 3.46 KB
/
Node_pulse.gd
File metadata and controls
129 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
extends Node
@onready var g_progress_bar_node = $"../CanvasLayer/VBoxContainer/ProgressBar"
@onready var g_sdimage_progress_node = $"../CanvasLayer/VBoxContainer/HSplitContainer/TextureRect_sdimageProgress"
@onready var g_node_error_noserver = $"../CanvasLayer/Window_ERROR_noserver"
@onready var g_node_config = $"../Node_pulse_config"
func base64_to_jpg(the_base64:String, the_image_path:String):
var raw_data = Marshalls.base64_to_raw(the_base64)
## xx save .jpg as binary
var f = FileAccess.open(the_image_path, FileAccess.WRITE)
for byte in raw_data:
f.store_8(byte)
f.close()
## xx end :: save .jpg as binary
func imagefile_to_texture(the_image_path:String):
var image_loaded = Image.load_from_file(the_image_path)
var texture = ImageTexture.create_from_image(image_loaded)
return texture
var g_busy = false
func SD_send_GET_request(url, payload, callback_func):
$HTTPRequest.request_completed.connect(callback_func)
var send_request = $HTTPRequest.request(url,[],HTTPClient.METHOD_GET, JSON.stringify(payload)) # what do we want to connect to
if send_request != OK:
g_busy = false
print("ERROR sending request")
print("SERVER NOT STARTED")
g_node_error_noserver.show()
else:
g_node_error_noserver.hide()
func get_api_url_progress():
return "http://127.0.0.1:7860/sdapi/v1/progress"
func do_timer():
SD_send_GET_request(get_api_url_progress(), {}, do_timer_response)
print("do timer")
var g_last_64 = ""
func do_timer_response(result, response_code, headers, body):
# get the progress and update the progress bar
var r = JSON.parse_string(body.get_string_from_utf8())
if r != null and r.has("current_image"):
if r["current_image"] == null:
print("idle")
g_progress_bar_node.value = 100
return false
# print(r)
if r == null:
print("Server not started.")
return
# print(r)
if r.has("current_image"):
var b64_image = r["current_image"]
if g_last_64 != b64_image: # prevent abusive unnecessary writes
# mem based
var raw_data = Marshalls.base64_to_raw(b64_image)
var jpg_image = Image.new()
# dont use webp
# show our progress image
g_sdimage_progress_node.show()
print("adjusting for filetype:", g_node_config.g_filetype)
# png
if g_node_config.g_filetype == "png":
jpg_image.load_png_from_buffer(raw_data)
else: # jpg
jpg_image.load_jpg_from_buffer(raw_data)
var textureb = ImageTexture.create_from_image(jpg_image)
g_sdimage_progress_node.texture = textureb
# file based
#base64_to_jpg(b64_image, "./temp/temp_progress.jpg")
#var tex = imagefile_to_texture("./temp/temp_progress.jpg")
#g_sdimage_progress_node.texture = tex
#g_last_64 = b64_image
#print("updating image progress")
## resize g_sdimage_progress_node to fit the image
#var image_size = tex.get_size()
#print("progress image size:")
#print(image_size)
print("time taken:", r["progress"])
print("time remaining:", r["eta_relative"])
print("sampling step:", r["state"]["sampling_step"])
print("sampling steps:", r["state"]["sampling_steps"])
var cur_progress = float(r["state"]["sampling_step"]) / float(r["state"]["sampling_steps"])
cur_progress = cur_progress * 100
g_progress_bar_node.value = cur_progress
pass
func _on_timer_timeout():
do_timer()
pass # Replace with function body.
func _on_ready():
g_sdimage_progress_node.hide()
SD_send_GET_request(get_api_url_progress(), {}, do_timer_response)
print("do timer:start")
pass # Replace with function body.