zs_ai_ddl.py 4.5 KB

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