_deprecated_zs_ai_ddl.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import requests
  2. from pathlib import Path
  3. import json
  4. import base64
  5. DDL_SERVER = "http://69.230.251.234:8081/api"
  6. script_path_ai = (
  7. "D://Git//ap-canvas-creation-module//04_stable_diffusion//sd_comfy_api_v2.py"
  8. )
  9. # script_path = Path(__file__).resolve()
  10. script_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//"
  11. # scene_path = str(script_path.parent / "sample_scene" / "Canvas_Render_Scene.blend")
  12. scene_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//sample_scene//Canvas_Render_Scene.blend"
  13. # render_script_path = str(script_path.parent / "zs_ai_render_script.py")
  14. render_script_path = (
  15. "D://Git//ap-canvas-creation-module//03_blender//sd_blender//zs_ai_render_script.py"
  16. )
  17. post_job_script_path = "D:/Git/ap-canvas-creation-module/04_stable_diffusion/zs_ai_post_ai_render_script.py"
  18. def convert_object_to_base64_string(obj):
  19. return base64.b64encode(json.dumps(obj).encode("utf-8")).decode("utf-8")
  20. def send_3d_ddl_job(
  21. job_info={},
  22. plugin_info={},
  23. ):
  24. url = f"{DDL_SERVER}/jobs"
  25. response = requests.post(
  26. url,
  27. json={
  28. "JobInfo": job_info,
  29. "PluginInfo": plugin_info,
  30. "AuxFiles": [],
  31. "IdOnly": True,
  32. },
  33. )
  34. if response.status_code == 200:
  35. print("Data posted successfully.")
  36. return response.json()
  37. else:
  38. print(f"Failed to post data. Status code: {response.status_code}")
  39. return None
  40. def send_sd_comfy_ddl_job(
  41. job_info={},
  42. plugin_info={},
  43. ):
  44. url = f"{DDL_SERVER}/jobs"
  45. data = {
  46. "JobInfo": job_info,
  47. "PluginInfo": plugin_info,
  48. "AuxFiles": [],
  49. "IdOnly": True,
  50. }
  51. response = requests.post(url, json=data)
  52. if response.status_code == 200:
  53. print("Data posted successfully.")
  54. return response.json()
  55. else:
  56. print(f"Failed to post data. Status code: {response.status_code}")
  57. return None
  58. def get_scene_data():
  59. print("Loading Scene Data")
  60. # load scene data
  61. # to be replaced with actual data
  62. # open scene_info.json
  63. script_path = Path(__file__).resolve()
  64. scene_data_path = script_path.parent / "sample_scene" / "scene_info.json"
  65. with scene_data_path.open() as file:
  66. scene_data = json.load(file)
  67. print(scene_data)
  68. return scene_data
  69. def get_ai_scene_data():
  70. print("Loading AI Scene Data")
  71. # load scene data
  72. # to be replaced with actual data
  73. # open scene_info.json
  74. script_path = Path(__file__).resolve()
  75. scene_data_path = script_path.parent / "sample_scene" / "ai_scene_info.json"
  76. with scene_data_path.open() as file:
  77. ai_scene_data = json.load(file)
  78. print(ai_scene_data)
  79. return ai_scene_data
  80. def submit_3d_job(username, scene_data):
  81. # create 3d ddl job
  82. ddl_3d_job = send_3d_ddl_job(
  83. job_info={
  84. "Name": scene_data["project_id"],
  85. "UserName": username,
  86. "Frames": 1,
  87. "Priority": 49,
  88. "Plugin": "DevBlender",
  89. "ChunkSize": "1",
  90. "MachineName": "AI_PC",
  91. # "PostJobScript": "D://Git//ap-canvas-creation-module//04_stable_diffusion//sd_comfy_api.py",
  92. },
  93. plugin_info={
  94. "SceneFile": scene_path,
  95. "AdditionalArguments": f"-P {render_script_path}",
  96. "CustomArguments": json.dumps(scene_data),
  97. },
  98. )
  99. return ddl_3d_job
  100. # create ai ddl job
  101. def submit_ai_image_job(username, ai_scene_data, dependency_job_id=""):
  102. ddl_ai_job = send_sd_comfy_ddl_job(
  103. job_info={
  104. "Name": "AI_Test_Job_01",
  105. "UserName": username,
  106. "Plugin": "Python",
  107. "Priority": 49,
  108. "Name": "ComfyUI Job",
  109. "JobDependencies": dependency_job_id,
  110. "PostJobScript": post_job_script_path,
  111. },
  112. plugin_info={
  113. "ScriptFile": script_path_ai,
  114. "Version": "3.1",
  115. "Arguments": f"-- {convert_object_to_base64_string(ai_scene_data)}",
  116. },
  117. )
  118. return ddl_ai_job
  119. def submit_3d_and_ai_image_job(username, scene_data, ai_scene_data):
  120. ddl_3d_job = submit_3d_job(username, scene_data)
  121. ddl_ai_job = submit_ai_image_job(username, ai_scene_data, ddl_3d_job["_id"])
  122. return ddl_3d_job, ddl_ai_job
  123. def submit_test_job():
  124. scene_data = get_scene_data()
  125. ai_scene_data = get_ai_scene_data()
  126. # ddl_3d_job = submit_3d_job("test_user", scene_data)
  127. ddl_ai_job = submit_ai_image_job("test_user", ai_scene_data)
  128. # ddl_3d_job, ddl_ai_job = submit_3d_and_ai_image_job(
  129. # "test_user", scene_data, scene_data
  130. # )
  131. submit_test_job()