Эх сурвалжийг харах

update comfy and scene info

Sergiu 9 сар өмнө
parent
commit
0e4ab036a3

BIN
03_blender/sd_blender/sample_scene/Canvas_Render_Scene.blend


+ 72 - 16
03_blender/sd_blender/sample_scene/scene_info.json

@@ -1,5 +1,6 @@
 {
   "scene": {
+    "project_id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
     "objects": [
       {
         "name": "LNG Serum",
@@ -8,14 +9,25 @@
         "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
         "properties": {
           "transform": {
-            "position": [0, 0, 0],
-            "rotation": [9.0, 26.1, -15.7],
-            "scale": [1, 1, 1]
+            "position": [
+              0,
+              0,
+              0
+            ],
+            "rotation": [
+              9.0,
+              26.1,
+              -15.7
+            ],
+            "scale": [
+              1,
+              1,
+              1
+            ]
           },
           "visible": true
         }
       },
-
       {
         "name": "Sphere",
         "type": "group",
@@ -23,9 +35,21 @@
         "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89h3",
         "properties": {
           "transform": {
-            "position": [-0.041122, -0.036135, 0.155559],
-            "rotation": [0, 0, 0],
-            "scale": [0.74098, 0.74098, 0.74098]
+            "position": [
+              -0.041122,
+              -0.036135,
+              0.155559
+            ],
+            "rotation": [
+              0,
+              0,
+              0
+            ],
+            "scale": [
+              0.74098,
+              0.74098,
+              0.74098
+            ]
           },
           "visible": true,
           "color": {
@@ -42,9 +66,21 @@
         "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89h3",
         "properties": {
           "transform": {
-            "position": [0.067047, 0.088912, -0.023188],
-            "rotation": [0, 0, 0],
-            "scale": [0.4103, 0.4103, 0.4103]
+            "position": [
+              0.067047,
+              0.088912,
+              -0.023188
+            ],
+            "rotation": [
+              0,
+              0,
+              0
+            ],
+            "scale": [
+              0.4103,
+              0.4103,
+              0.4103
+            ]
           },
           "visible": true,
           "color": {
@@ -61,9 +97,21 @@
         "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89h3",
         "properties": {
           "transform": {
-            "position": [-0.041122, -0.132029, 0.078844],
-            "rotation": [0, 0, 0],
-            "scale": [0.16679, 0.16679, 0.16679]
+            "position": [
+              -0.041122,
+              -0.132029,
+              0.078844
+            ],
+            "rotation": [
+              0,
+              0,
+              0
+            ],
+            "scale": [
+              0.16679,
+              0.16679,
+              0.16679
+            ]
           },
           "visible": true,
           "color": {
@@ -80,8 +128,16 @@
         "type": "camera",
         "properties": {
           "transform": {
-            "position": [0.432918, -0.165131, -0.080359],
-            "rotation": [105.303, 0.786264, 69.833]
+            "position": [
+              0.432918,
+              -0.165131,
+              -0.080359
+            ],
+            "rotation": [
+              105.303,
+              0.786264,
+              69.833
+            ]
           },
           "lens": {
             "type": "perspective",
@@ -113,4 +169,4 @@
       }
     }
   }
-}
+}

+ 108 - 27
03_blender/sd_blender/zs_ai_ddl.py

@@ -1,19 +1,31 @@
 import requests
 from pathlib import Path
 import json
+import base64
 
 DDL_SERVER = "http://69.230.251.234:8081/api"
 
+script_path_ai = (
+    "D://Git//ap-canvas-creation-module//04_stable_diffusion//sd_comfy_api.py"
+)
 
-def send_ddl_job(
-    job_info={
-        "Plugin": "DevBlender",
-        "Frames": "0",
-        "Priority": 49,
-        "Name": "name",
-        "Group": "general",
-    },
-    plugin_info={"SceneFile": "", "AdditionalArguments": "", "CustomArguments": ""},
+# script_path = Path(__file__).resolve()
+script_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//"
+# scene_path = str(script_path.parent / "sample_scene" / "Canvas_Render_Scene.blend")
+scene_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//sample_scene//Canvas_Render_Scene.blend"
+# render_script_path = str(script_path.parent / "zs_ai_render_script.py")
+render_script_path = (
+    "D://Git//ap-canvas-creation-module//03_blender//sd_blender//zs_ai_render_script.py"
+)
+
+
+def convert_object_to_base64_string(obj):
+    return base64.b64encode(json.dumps(obj).encode("utf-8")).decode("utf-8")
+
+
+def send_3d_ddl_job(
+    job_info={},
+    plugin_info={},
 ):
     url = f"{DDL_SERVER}/jobs"
     response = requests.post(
@@ -25,10 +37,36 @@ def send_ddl_job(
             "IdOnly": True,
         },
     )
-    return response.json()
+    if response.status_code == 200:
+        print("Data posted successfully.")
+        return response.json()
+    else:
+        print(f"Failed to post data. Status code: {response.status_code}")
+        return None
 
 
-def load_scene_data():
+def send_sd_comfy_ddl_job(
+    job_info={},
+    plugin_info={},
+):
+    url = f"{DDL_SERVER}/jobs"
+    data = {
+        "JobInfo": job_info,
+        "PluginInfo": plugin_info,
+        "AuxFiles": [],
+        "IdOnly": True,
+    }
+    response = requests.post(url, json=data)
+
+    if response.status_code == 200:
+        print("Data posted successfully.")
+        return response.json()
+    else:
+        print(f"Failed to post data. Status code: {response.status_code}")
+        return None
+
+
+def get_scene_data():
     print("Loading Scene Data")
     # load scene data
 
@@ -42,39 +80,82 @@ def load_scene_data():
     return scene_data
 
 
-def send_test_render():
+def get_ai_scene_data():
+    print("Loading AI Scene Data")
+    # load scene data
 
+    # to be replaced with actual data
+    # open scene_info.json
     script_path = Path(__file__).resolve()
-    scene_path = str(script_path.parent / "sample_scene" / "Canvas_Render_Scene.blend")
+    scene_data_path = script_path.parent / "sample_scene" / "ai_scene_info.json"
+    with scene_data_path.open() as file:
+        ai_scene_data = json.load(file)
+        print(ai_scene_data)
+    return ai_scene_data
 
-    render_script_path = str(script_path.parent / "zs_ai_render_script.py")
 
-    # load scene data and convert it to base64 string
+def submit_3d_job(username, scene_data):
 
-    scene_data = load_scene_data()
-    scene_data_string = json.dumps(scene_data)
-    # scene_data_bytes = scene_data_string.encode("ascii")
-    # scene_data_base64 = scene_data_bytes.decode("ascii")
+    # create 3d ddl job
 
-    ddl_web = send_ddl_job(
+    ddl_3d_job = send_3d_ddl_job(
         job_info={
-            "Name": "AI_Test_Job_01",
-            "UserName": "AI_PC",
-            "Frames": 0,
+            "Name": scene_data["scene"]["project_id"],
+            "UserName": username,
+            "Frames": 1,
             "Priority": 49,
             "Plugin": "DevBlender",
             "ChunkSize": "1",
             "MachineName": "AI_PC",
-            "PostJobScript": "D:/Git/ap-canvas-creation-module/05_deadline/sd_comfy_deadline.py",
+            # "PostJobScript": "D://Git//ap-canvas-creation-module//04_stable_diffusion//sd_comfy_api.py",
         },
         plugin_info={
             "SceneFile": scene_path,
             "AdditionalArguments": f"-P {render_script_path}",
-            "CustomArguments": scene_data_string,
+            "CustomArguments": json.dumps(scene_data),
         },
     )
 
-    print(ddl_web)
+    return ddl_3d_job
+
+    # create ai ddl job
+
+
+def submit_ai_image_job(username, ai_scene_data, dependency_job_id=""):
+    ddl_ai_job = send_sd_comfy_ddl_job(
+        job_info={
+            "Name": "AI_Test_Job_01",
+            "UserName": username,
+            "Plugin": "Python",
+            "Priority": 49,
+            "Name": "ComfyUI Job",
+            "JobDependencies": dependency_job_id,
+            # "PostJobScript": "C:/WORK/2022.DDL_Script/postJobTest.py",
+        },
+        plugin_info={
+            "ScriptFile": script_path_ai,
+            "Version": "3.1",
+            "Arguments": f"-- {convert_object_to_base64_string(ai_scene_data)}",
+        },
+    )
+
+    return ddl_ai_job
+
+
+def submit_3d_and_ai_image_job(username, scene_data, ai_scene_data):
+    ddl_3d_job = submit_3d_job(username, scene_data)
+    ddl_ai_job = submit_ai_image_job(username, ai_scene_data, ddl_3d_job["_id"])
+    return ddl_3d_job, ddl_ai_job
+
+
+def submit_test_job():
+    scene_data = get_scene_data()
+    ai_scene_data = get_ai_scene_data()
+    # ddl_3d_job = submit_3d_job("test_user", scene_data)
+    # ddl_ai_job = submit_ai_image_job("test_user", ai_scene_data)
+    ddl_3d_job, ddl_ai_job = submit_3d_and_ai_image_job(
+        "test_user", scene_data, scene_data
+    )
 
 
-send_test_render()
+submit_test_job()

+ 1 - 0
03_blender/sd_blender/zs_ai_render_script.py

@@ -7,6 +7,7 @@ import base64
 
 argv = sys.argv
 try:
+
     argv = argv[argv.index("--") + 1 :]
 
     # scene_info_base64 = argv[0]

+ 55 - 23
04_stable_diffusion/sd_comfy_api.py

@@ -1,6 +1,9 @@
 import json
 from urllib import request, parse
 import random
+import sys
+import ast
+import base64
 
 # This is the ComfyUI api prompt format.
 
@@ -13,6 +16,15 @@ import random
 # this is the one for the default workflow
 
 # load the prompt from a workflow_api_test.json file
+
+
+def convert_base64_string_to_object(base64_string):
+    bytes = base64.b64decode(base64_string)
+    string = bytes.decode("ascii")
+
+    return json.loads(string)
+
+
 with open(
     "D://Git//ap-canvas-creation-module//04_stable_diffusion//workflows//canvas_3d_to_img_standard.json",
     "r",
@@ -52,37 +64,57 @@ def set_filename(json_obj, title, new_prefix):
     return None
 
 
-positive_text = "cosmetics bottle at sunset, beautiful product advertising, luxury"
-negative_text = "ugly, bad"
+def main():
+    # main code here
+
+    argv = sys.argv
+
+    try:
+
+        argv = argv[argv.index("--") + 1 :]
+
+        ai_scene_info = convert_base64_string_to_object(argv[0])
+
+        print("loading scene data", ai_scene_info)
+
+    except Exception as e:
+        print("Error:", e)
+
+    positive_text = ai_scene_info["ai_scene"]["settings"]["positive_prompt"]
+    negative_text = ai_scene_info["ai_scene"]["settings"]["negative_prompt"]
+
+    image_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//sample_scene//Renders//TestProject//"
+
+    image_base_path = image_path + "base0001.png"
+    image_alpha_products_path = image_path + "alpha_products0001.png"
+    image_depth_path = image_path + "depth0001.png"
 
-image_path = "D://Git//ap-canvas-creation-module//03_blender//sd_blender//sample_scene//Renders//TestProject//"
+    prompt = json.loads(prompt_text_json)
+    set_filename(prompt, "Save Image", "custom/basic_api_example")
 
-image_base_path = image_path + "base0001.png"
-image_alpha_products_path = image_path + "alpha_products0001.png"
-image_depth_path = image_path + "depth0001.png"
+    ksampler_main = find_node(prompt, "KSampler")
+    ksampler_main["inputs"]["noise_seed"] = random.randint(0, 1000000)
 
-prompt = json.loads(prompt_text_json)
-set_filename(prompt, "Save Image", "custom/basic_api_example")
+    prompt_positive = find_node(prompt, "positive_CLIPTextEncodeSDXL")
+    prompt_positive["inputs"]["text_g"] = positive_text
 
-ksampler_main = find_node(prompt, "KSampler")
-ksampler_main["inputs"]["noise_seed"] = random.randint(0, 1000000)
+    prompt_positive["inputs"]["text_l"] = positive_text
 
-prompt_positive = find_node(prompt, "positive_CLIPTextEncodeSDXL")
-prompt_positive["inputs"]["text_g"] = positive_text
+    prompt_negative = find_node(prompt, "negative_CLIPTextEncodeSDXL")
+    prompt_negative["inputs"]["text_g"] = negative_text
+    prompt_negative["inputs"]["text_l"] = negative_text
 
-prompt_positive["inputs"]["text_l"] = positive_text
+    image_base = find_node(prompt, "image_base")
+    image_base["inputs"]["image"] = image_base_path
 
-prompt_negative = find_node(prompt, "negative_CLIPTextEncodeSDXL")
-prompt_negative["inputs"]["text_g"] = negative_text
-prompt_negative["inputs"]["text_l"] = negative_text
+    image_base = find_node(prompt, "image_product_mask")
+    image_base["inputs"]["image"] = image_alpha_products_path
 
-image_base = find_node(prompt, "image_base")
-image_base["inputs"]["image"] = image_base_path
+    image_base = find_node(prompt, "image_depth")
+    image_base["inputs"]["image"] = image_depth_path
 
-image_base = find_node(prompt, "image_product_mask")
-image_base["inputs"]["image"] = image_alpha_products_path
+    queue_prompt(prompt)
 
-image_base = find_node(prompt, "image_depth")
-image_base["inputs"]["image"] = image_depth_path
 
-queue_prompt(prompt)
+if __name__ == "__main__":
+    main()