move _run_in_next_server_event to nodes.py to avoid misunderstand
This commit is contained in:
+23
-4
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
import asyncio
|
||||
import time
|
||||
from nodes import LoadImage
|
||||
from .apis import cache_images
|
||||
|
||||
@@ -9,6 +9,17 @@ def define_comfyui_nodes(sdppp):
|
||||
return 'Photoshop is not connected'
|
||||
return True
|
||||
|
||||
def call_async_func_in_sdppp_thread(fn):
|
||||
handle = {
|
||||
'done': False,
|
||||
'result': None
|
||||
}
|
||||
sdppp.onNextTick(fn, handle)
|
||||
while not handle['done']:
|
||||
pass
|
||||
return handle['result']
|
||||
|
||||
|
||||
class GetImageFromPhotoshopLayerNode:
|
||||
@classmethod
|
||||
def VALIDATE_INPUTS(s, layer):
|
||||
@@ -22,7 +33,9 @@ def define_comfyui_nodes(sdppp):
|
||||
photoshopInstance = sdppp.get_ps_instance()
|
||||
id = photoshopInstance.layer_name_to_id(layer)
|
||||
bounds_id = photoshopInstance.layer_name_to_id(use_layer_bounds, id)
|
||||
is_changed, history_state_id = photoshopInstance.check_layer_bounds_combo_changed(id, bounds_id)
|
||||
async def do_get_history():
|
||||
return await photoshopInstance.check_layer_bounds_combo_changed(id, bounds_id)
|
||||
is_changed, history_state_id = call_async_func_in_sdppp_thread(do_get_history)
|
||||
if is_changed and history_state_id is None:
|
||||
return np.random.rand()
|
||||
photoshopInstance.update_comfyui_last_value(id, bounds_id, history_state_id)
|
||||
@@ -56,7 +69,11 @@ def define_comfyui_nodes(sdppp):
|
||||
|
||||
id = photoshopInstance.layer_name_to_id(layer)
|
||||
bounds_id = photoshopInstance.layer_name_to_id(use_layer_bounds, id)
|
||||
image_id, layer_opacity = photoshopInstance.get_image_from_remote(layer_id=id, bounds_id=bounds_id)
|
||||
|
||||
async def do_get_image():
|
||||
return await photoshopInstance.get_image(layer_id=id, bounds_id=bounds_id)
|
||||
image_id, layer_opacity = call_async_func_in_sdppp_thread(do_get_image)
|
||||
|
||||
loadImage = LoadImage()
|
||||
(output_image, output_mask) = loadImage.load_image(image_id)
|
||||
return (output_image, output_mask, layer_opacity / 100)
|
||||
@@ -93,7 +110,9 @@ def define_comfyui_nodes(sdppp):
|
||||
|
||||
ret = cache_images(images)
|
||||
layer_id = photoshopInstance.layer_name_to_id(layer)
|
||||
photoshopInstance.send_images_to_remote(image_ids=ret, layer_id=layer_id)
|
||||
async def do_send():
|
||||
return await photoshopInstance.send_images(image_ids=ret, layer_id=layer_id)
|
||||
call_async_func_in_sdppp_thread(do_send)
|
||||
|
||||
return (None,)
|
||||
|
||||
|
||||
+17
-45
@@ -19,16 +19,6 @@ class PhotoshopInstance:
|
||||
self.layers = []
|
||||
self.reset_change_tracker()
|
||||
|
||||
def _run_in_next_server_event(self, fn):
|
||||
handle = {
|
||||
'done': False,
|
||||
'result': None
|
||||
}
|
||||
self.sdppp.onNextTick(fn, handle)
|
||||
while not handle['done']:
|
||||
pass
|
||||
return handle['result']
|
||||
|
||||
def get_layers(self):
|
||||
return list(map(lambda layer: f"{layer['name']} (id:{layer['id']})", self.layers))
|
||||
|
||||
@@ -62,32 +52,22 @@ class PhotoshopInstance:
|
||||
id = int(layer_name_and_id_split.pop().strip()[:-1])
|
||||
return id
|
||||
|
||||
# for nodes call
|
||||
def get_image_from_remote(self, layer_id, bounds_id=False):
|
||||
async def _get_image(sio):
|
||||
return await sio.call('get_image', data={'layer_id': layer_id, 'use_layer_bounds': bounds_id}, to=self.sid)
|
||||
result = self._run_in_next_server_event(_get_image)
|
||||
async def get_image(self, layer_id, bounds_id=False):
|
||||
result = await self.sdppp.sio.call('get_image', data={'layer_id': layer_id, 'use_layer_bounds': bounds_id}, to=self.sid)
|
||||
if not result:
|
||||
return None, None
|
||||
history_state_id = self._update_layer_bounds_history_state_id(layer_id, bounds_id)
|
||||
self._update_history_state_id_after_internal_change(history_state_id)
|
||||
history_state_id = await self._update_layer_bounds_history_state_id(layer_id, bounds_id)
|
||||
await self._update_history_state_id_after_internal_change(history_state_id)
|
||||
|
||||
return result['upload_name'], result['layer_opacity']
|
||||
|
||||
# for nodes call
|
||||
def send_images_to_remote(self, image_ids, layer_id=""):
|
||||
async def _send_images(sio):
|
||||
return await sio.call('send_images', data={'image_ids': image_ids, 'layer_id': layer_id}, to=self.sid)
|
||||
result = self._run_in_next_server_event(_send_images)
|
||||
self._update_history_state_id_after_internal_change()
|
||||
async def send_images(self, image_ids, layer_id=""):
|
||||
result = await self.sdppp.sio.call('send_images', data={'image_ids': image_ids, 'layer_id': layer_id}, to=self.sid)
|
||||
await self._update_history_state_id_after_internal_change()
|
||||
return result
|
||||
|
||||
# for nodes call
|
||||
def get_active_history_state_id_from_remote(self):
|
||||
async def get_active_history_state_id_from_remote(sio):
|
||||
res = await sio.call('get_active_history_state_id', data={}, to=self.sid)
|
||||
return res
|
||||
result = self._run_in_next_server_event(get_active_history_state_id_from_remote)
|
||||
async def get_active_history_state_id(self):
|
||||
result = await self.sdppp.sio.call('get_active_history_state_id', data={}, to=self.sid)
|
||||
if result is None:
|
||||
return 0
|
||||
id = result.get('history_state_id', None)
|
||||
@@ -95,52 +75,44 @@ class PhotoshopInstance:
|
||||
|
||||
|
||||
# ===
|
||||
# for api calls and initialize
|
||||
def reset_change_tracker(self):
|
||||
self.get_img_state_id = 0
|
||||
self.change_tracker = {}
|
||||
self.comfyui_last_value_tracker = {}
|
||||
|
||||
# for apis calls
|
||||
async def is_ps_history_changed(self, sio):
|
||||
result = await sio.call('get_active_history_state_id', data={}, to=self.sid)
|
||||
if result is None:
|
||||
return False
|
||||
current_id = result.get('history_state_id', None)
|
||||
async def is_ps_history_changed(self):
|
||||
current_id = await self.get_active_history_state_id()
|
||||
return self.get_img_state_id != current_id and self.get_img_state_id < current_id
|
||||
|
||||
# for nodes calls
|
||||
def check_layer_bounds_combo_changed(self, layer, use_layer_bounds):
|
||||
async def check_layer_bounds_combo_changed(self, layer, use_layer_bounds):
|
||||
layer_bounds_combo = f"{layer}{use_layer_bounds}"
|
||||
layer_bounds_history_state_id = self.change_tracker.get(layer_bounds_combo, None)
|
||||
if layer_bounds_history_state_id is None:
|
||||
return True
|
||||
latest_state_id = self.get_active_history_state_id_from_remote()
|
||||
latest_state_id = await self.get_active_history_state_id()
|
||||
if latest_state_id > layer_bounds_history_state_id:
|
||||
return True, latest_state_id
|
||||
#didn't change, use last value
|
||||
comfyui_last_value = self.comfyui_last_value_tracker.get(layer_bounds_combo, None) or layer_bounds_history_state_id
|
||||
return False, comfyui_last_value
|
||||
|
||||
# for nodes
|
||||
# have to track this value because comfyui determines if the value is changed by comparing it with the last value.
|
||||
# can't use the real history id because sending images changes the history id, and comfyui will think the value is changed when it's not
|
||||
def update_comfyui_last_value(self, layer, use_layer_bounds, value):
|
||||
layer_bounds_combo = f"{layer}{use_layer_bounds}"
|
||||
self.comfyui_last_value_tracker[layer_bounds_combo] = value
|
||||
|
||||
# for nodes call
|
||||
# need to update history id after internal operation otherwise it might cause infinite change loop
|
||||
def _update_history_state_id_after_internal_change(self, history_state_id=None):
|
||||
async def _update_history_state_id_after_internal_change(self, history_state_id=None):
|
||||
if history_state_id is None: # need to get new id after operation, it causes history change
|
||||
history_state_id = self.get_active_history_state_id_from_remote()
|
||||
history_state_id = await self.get_active_history_state_id()
|
||||
if self.get_img_state_id is not None: # udpate all the change trackers with the new history state id
|
||||
self.change_tracker = {k: history_state_id for k, v in self.change_tracker.items() if v == self.get_img_state_id}
|
||||
self.get_img_state_id = history_state_id # it gets into a loop if get img state is not updated
|
||||
|
||||
# for nodes call
|
||||
def _update_layer_bounds_history_state_id(self, layer, use_layer_bounds):
|
||||
async def _update_layer_bounds_history_state_id(self, layer, use_layer_bounds):
|
||||
layer_bounds_combo = f"{layer}{use_layer_bounds}"
|
||||
latest_state_id = self.get_active_history_state_id_from_remote()
|
||||
latest_state_id = await self.get_active_history_state_id()
|
||||
self.change_tracker[layer_bounds_combo] = latest_state_id
|
||||
return latest_state_id
|
||||
+6
-6
@@ -30,7 +30,7 @@ class SDPPP:
|
||||
return self.photoshop_instances[sid]
|
||||
|
||||
def onNextTick(self, fn, handle):
|
||||
self.onNextTickQueue.append((fn, handle))
|
||||
self.onNextTickQueue.append((fn, handle))
|
||||
|
||||
def check_state_true(self, sid):
|
||||
return self.state.get(sid, False)
|
||||
@@ -64,14 +64,14 @@ class SDPPP:
|
||||
break
|
||||
while len(self.onNextTickQueue) > 0:
|
||||
item = self.onNextTickQueue.pop(0)
|
||||
item[1]['result'] = await item[0](self.sio)
|
||||
item[1]['result'] = await item[0]()
|
||||
item[1]['done'] = True
|
||||
await asyncio.sleep(0.5)
|
||||
self.loop.create_task(selfEventLoop())
|
||||
|
||||
# only emit by photoshop instance
|
||||
@sio.event
|
||||
def disconnect(sid):
|
||||
async def disconnect(sid):
|
||||
self.state[sid] = False
|
||||
if sid in self.photoshop_instances:
|
||||
self.photoshop_instances.pop(sid, None)
|
||||
@@ -101,12 +101,12 @@ class SDPPP:
|
||||
instance = self.get_ps_instance()
|
||||
if instance is None:
|
||||
return
|
||||
if not await instance.is_ps_history_changed(sio):
|
||||
if not await instance.is_ps_history_changed():
|
||||
return
|
||||
sio.emit('trigger_graph_change', to=sid)
|
||||
await sio.emit('trigger_graph_change', to=sid)
|
||||
|
||||
@sio.event
|
||||
def reset_changes(sid):
|
||||
async def reset_changes(sid):
|
||||
instance = self.get_ps_instance()
|
||||
if instance is None:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user