sd_comfy_api.py 3.5 KB

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