zs_ai_post_3d_render_script.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from __future__ import (
  2. print_function,
  3. ) # Ensure print() function is available in Python 2
  4. import sys
  5. import json
  6. import requests
  7. import base64
  8. from Deadline.Scripting import * # type: ignore # Import Deadline script utilities
  9. api_path = "https://canvas-api-test.anvil.app/_/api"
  10. image_path = (
  11. "D:/Git/ap-canvas-creation-module/03_blender/sd_blender/sample_scene/Renders/"
  12. )
  13. def update_3d_image_task_status(row_id, new_status):
  14. # Define the URL for the API endpoint
  15. url = "{}/creation-module/3d-image/update-status".format(api_path)
  16. # Create a JSON payload
  17. payload = {"row_id": row_id, "new_status": new_status}
  18. # Make a POST request to the API endpoint with the JSON payload
  19. response = requests.post(url, json=payload)
  20. # Handle the response
  21. if response.status_code == 200:
  22. print("Status update was successful")
  23. return response.json()
  24. else:
  25. print("Status update failed")
  26. print("Status code:", response.status_code)
  27. print("Response:", response.text)
  28. return None
  29. def get_3d_image_task(row_id):
  30. # Define the URL for the API endpoint
  31. url = "{}/creation-module/3d-image/{}".format(api_path, row_id)
  32. print("Constructed URL:", url) # Print the URL for debugging
  33. # Make a GET request to the API endpoint
  34. response = requests.get(url)
  35. # Handle the response
  36. if response.status_code == 200:
  37. print("Request was successful")
  38. return response.json()
  39. else:
  40. print("Request failed")
  41. print("Status code:", response.status_code)
  42. print("Response:", response.text)
  43. return None
  44. def find_image_and_convert_to_base64(image_path):
  45. with open(image_path, "rb") as image_file:
  46. image_data = image_file.read()
  47. image_base64 = base64.b64encode(image_data).decode("utf-8")
  48. return image_base64
  49. def upload_image_to_anvil(row_id, image_base64):
  50. url = "{}/creation-module/3d-image/upload-preview".format(api_path)
  51. payload = {"row_id": row_id, "image_base64": image_base64}
  52. response = requests.post(url, json=payload)
  53. if response.status_code == 200:
  54. print("Image uploaded successfully")
  55. update_3d_image_task_status(row_id=row_id, new_status=3)
  56. return response.json()
  57. else:
  58. print("Image upload failed")
  59. print("Status code:", response.status_code)
  60. print("Response:", response.text)
  61. return None
  62. def __main__(*args):
  63. deadlinePlugin = args[0]
  64. job = deadlinePlugin.GetJob()
  65. row_id = job.JobExtraInfo0
  66. response = get_3d_image_task(row_id)
  67. data = json.loads(response["data"])
  68. project_id = data["project_id"]
  69. image_base64 = find_image_and_convert_to_base64(
  70. image_path + "{}/base0001.jpg".format(project_id)
  71. )
  72. upload_image_to_anvil(row_id, image_base64)
  73. if __name__ == "__main__":
  74. __main__(sys.argv)