From 97a071c6e9c74b03a13b08747641d179bde36a7f Mon Sep 17 00:00:00 2001 From: Jedd Morgan <45512892+JR-Morgan@users.noreply.github.com> Date: Thu, 12 Oct 2023 16:32:34 +0100 Subject: [PATCH] automate data --- Dockerfile | 2 +- main.py | 17 ++++++++++++++--- speckle_import.py | 47 +++++++++++++++++++++++------------------------ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/Dockerfile b/Dockerfile index 248e4f0..009e200 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.11-slim +FROM nytimes/blender:latest RUN pip install poetry diff --git a/main.py b/main.py index 9ba838a..abe4b79 100644 --- a/main.py +++ b/main.py @@ -16,15 +16,26 @@ def automate_function( automate_context: AutomationContext ) -> None: - account = automate_context.speckle_client.account - AccountStorage.SaveObject(account.id, JsonConvert.SerializeObject(account)); + client = automate_context.speckle_client + PROJECT_ID = automate_context.automation_run_data.project_id + VERSION_ID = automate_context.automation_run_data.version_id + OBJECT_ID = client.commit.get(PROJECT_ID, VERSION_ID).referencedObject + + DATA = f""" +TOKEN = '{client.account.token}' +SERVER_URL = '{client.account.serverInfo.url}' +PROJECT_ID = '{PROJECT_ID}' +OBJECT_ID = '{OBJECT_ID}' +""" + + Path.write_text(Path("./automate_data.py"), DATA) + run( [ 'blender', '"environment.blend"', '--background', '--python speckle_import.py', - '-f 10', ], capture_output=True, text=True, diff --git a/speckle_import.py b/speckle_import.py index fc6683a..43e3fd7 100644 --- a/speckle_import.py +++ b/speckle_import.py @@ -1,49 +1,48 @@ import bpy import os -from bpy_speckle.properties.scene import SpeckleSceneSettings +from bpy_speckle.convert.to_native import _deep_conversion +from specklepy.api import operations +from specklepy.transports.server import ServerTransport +from automate_data import * - - -def take_screenshot(): - bpy.ops.render.render(write_still = True) - print(f"We took a screenshot! saved at {bpy.context.scene.render.filepath}") - -def set_output_path(commitId, cameraName): - #Get Output file directory +def set_filename(fileName): + """Sets the output directory for rendered images""" filepath = bpy.data.filepath - filename = os.path.basename(filepath) directory = os.path.dirname(filepath) if not filepath: raise Exception("Blend file must be saved!") - bpy.context.scene.render.filepath = os.path.join(directory, "Screenshots", f"{commitId}.{cameraName}") + bpy.context.scene.render.filepath = os.path.join(directory, "Screenshots", fileName) +print(f"Starting Receive...") +remote_transport = ServerTransport(STREAMID, token=TOKEN, url=SERVER_URL) +commit_object = operations.receive(OBJECTID, remote_transport) -speckle: SpeckleSceneSettings = bpy.context.scene.speckle +converted_objects = {} +_deep_conversion(commit_object, converted_objects, True) -print("hello speckle screenshot tool!") -bpy.ops.speckle.users_load() -bpy.ops.speckle.receive_stream_objects() -print(f"We received the objects!") +all_cameras = [o for o in bpy.context.scene.objects if o.type == 'CAMERA'] +total = len(all_cameras) +print(f"Starting rendering cameras... 0/{total}") - - -for ob in bpy.context.scene.objects: - if ob.type != 'CAMERA': - continue +for (i, ob) in enumerate(all_cameras): + #Set as active render cam bpy.context.scene.camera = ob - commit = speckle.get_active_user().get_active_stream().get_active_branch().get_active_commit() - set_output_path(commit.id, ob.name) - take_screenshot() + #Render camera to output directory + set_filename(f"{ob.name}") + bpy.ops.render.render(write_still = True) + print(f"Render {i}/{total} complete! saved at {bpy.context.scene.render.filepath}") + +print(f"All Done!")