sd_comfy_api.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import json
  2. from urllib import request, parse
  3. import random
  4. import sys
  5. import ast
  6. import base64
  7. import uuid
  8. # This is the ComfyUI api prompt format.
  9. # If you want it for a specific workflow you can "enable dev mode options"
  10. # in the settings of the UI (gear beside the "Queue Size: ") this will enable
  11. # a button on the UI to save workflows in api format.
  12. # keep in mind ComfyUI is pre alpha software so this format will change a bit.
  13. # this is the one for the default workflow
  14. # load the prompt from a workflow_api_test.json file
  15. def convert_base64_string_to_object(base64_string):
  16. bytes = base64.b64decode(base64_string)
  17. string = bytes.decode("ascii")
  18. return json.loads(string)
  19. server_address = "127.0.0.1:8188"
  20. client_id = str(uuid.uuid4())
  21. with open(
  22. "D://Git//ap-canvas-creation-module//04_stable_diffusion//workflows//canvas_3d_to_img_standard_V1.json",
  23. "r",
  24. ) as f:
  25. prompt_text_json = f.read()
  26. def queue_prompt(prompt):
  27. p = {"prompt": prompt}
  28. data = json.dumps(p).encode("utf-8")
  29. req = request.Request("http://127.0.0.1:8188/prompt", data=data)
  30. request.urlopen(req)
  31. def find_node(json_obj, title):
  32. for key, value in json_obj.items():
  33. if isinstance(value, dict):
  34. if value.get("_meta", {}).get("title") == title:
  35. return value
  36. else:
  37. result = find_node(value, title)
  38. if result:
  39. return result
  40. return None
  41. def set_filename(json_obj, title, new_prefix):
  42. for key, value in json_obj.items():
  43. if isinstance(value, dict):
  44. if value.get("_meta", {}).get("title") == title:
  45. if "inputs" in value and "filename_prefix" in value["inputs"]:
  46. value["inputs"]["filename_prefix"] = new_prefix
  47. else:
  48. result = set_filename(value, title, new_prefix)
  49. if result:
  50. return result
  51. return None
  52. def main():
  53. # main code here
  54. argv = sys.argv
  55. try:
  56. argv = argv[argv.index("--") + 1 :]
  57. ai_scene_info = convert_base64_string_to_object(argv[0])
  58. print("loading scene data", ai_scene_info)
  59. except Exception as e:
  60. print("Error:", e)
  61. positive_text = ai_scene_info["ai_scene"]["settings"]["positive_prompt"]
  62. negative_text = ai_scene_info["ai_scene"]["settings"]["negative_prompt"]
  63. image_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//sample_scene//Renders//15a314a1-8ba1-4e0e-ad0c-f605b06f89f8//"
  64. image_base_path = image_path + "base0001.jpg"
  65. image_alpha_products_path = image_path + "alpha_products0001.jpg"
  66. # image_depth_path = image_path + "depth0001.png"
  67. prompt = json.loads(prompt_text_json)
  68. set_filename(prompt, "Save Image", "custom/basic_api_example")
  69. ksampler_main = find_node(prompt, "KSampler")
  70. ksampler_main["inputs"]["noise_seed"] = random.randint(0, 1000000)
  71. ksampler_main = find_node(prompt, "KSampler")
  72. ksampler_main["inputs"]["steps"] = 30
  73. prompt_positive = find_node(prompt, "positive_CLIPTextEncodeSDXL")
  74. prompt_positive["inputs"]["text_g"] = positive_text
  75. prompt_positive["inputs"]["text_l"] = positive_text
  76. prompt_negative = find_node(prompt, "negative_CLIPTextEncodeSDXL")
  77. prompt_negative["inputs"]["text_g"] = negative_text
  78. prompt_negative["inputs"]["text_l"] = negative_text
  79. image_base = find_node(prompt, "image_base")
  80. image_base["inputs"]["image"] = image_base_path
  81. image_base = find_node(prompt, "image_product_mask")
  82. image_base["inputs"]["image"] = image_alpha_products_path
  83. image_base = find_node(prompt, "image_depth")
  84. # image_base["inputs"]["image"] = image_depth_path
  85. queue_prompt(prompt)
  86. if __name__ == "__main__":
  87. main()