Datasets:
Download code/robohome/src/engine/source/packages/runtime/blender_resident.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/source/packages/runtime/blender_resident.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/engine/source/packages/runtime/blender_resident.py
-
curl -L -o blender_resident.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/source/packages/runtime/blender_resident.py
3.43 kB
| from __future__ import annotations | |
| import json | |
| import socket | |
| import uuid | |
| from pathlib import Path | |
| from typing import Any, Literal | |
| from pydantic import BaseModel, ConfigDict, Field | |
| class BlenderWorkerResponse(BaseModel): | |
| model_config = ConfigDict(extra="forbid") | |
| protocol_version: Literal["1.0"] = "1.0" | |
| job_id: str | |
| status: Literal["passed", "failed", "shutting_down"] | |
| jobs_completed: int = Field(ge=0) | |
| elapsed_s: float = Field(ge=0.0) | |
| report: str | None = None | |
| error: str | None = None | |
| def _endpoint(value: str) -> tuple[str, int]: | |
| host, separator, raw_port = value.rpartition(":") | |
| if not separator or not host: | |
| raise ValueError("Blender resident endpoint must be host:port") | |
| port = int(raw_port) | |
| if not 1 <= port <= 65535: | |
| raise ValueError("Blender resident endpoint port is invalid") | |
| return host, port | |
| def _request(endpoint: str, payload: dict[str, Any], *, timeout_s: float) -> dict[str, Any]: | |
| host, port = _endpoint(endpoint) | |
| encoded = (json.dumps(payload, separators=(",", ":")) + "\n").encode() | |
| with socket.create_connection((host, port), timeout=timeout_s) as connection: | |
| connection.settimeout(timeout_s) | |
| connection.sendall(encoded) | |
| chunks: list[bytes] = [] | |
| total = 0 | |
| while True: | |
| chunk = connection.recv(65536) | |
| if not chunk: | |
| break | |
| chunks.append(chunk) | |
| total += len(chunk) | |
| if total > 4 * 1024 * 1024: | |
| raise RuntimeError("Blender resident response exceeded 4 MiB") | |
| if b"\n" in chunk: | |
| break | |
| raw = b"".join(chunks).split(b"\n", 1)[0] | |
| if not raw: | |
| raise RuntimeError("Blender resident worker returned an empty response") | |
| value = json.loads(raw) | |
| if not isinstance(value, dict): | |
| raise RuntimeError("Blender resident worker returned a non-object response") | |
| return value | |
| def submit_blender_job( | |
| endpoint: str, | |
| *, | |
| manifest: Path, | |
| output: Path, | |
| mode: Literal["compose", "render-only"] = "compose", | |
| timeout_s: float = 3600.0, | |
| ) -> BlenderWorkerResponse: | |
| job_id = uuid.uuid4().hex | |
| response = BlenderWorkerResponse.model_validate( | |
| _request( | |
| endpoint, | |
| { | |
| "protocol_version": "1.0", | |
| "op": "compose", | |
| "job_id": job_id, | |
| "manifest": str(manifest.resolve()), | |
| "output": str(output.resolve()), | |
| "mode": mode, | |
| }, | |
| timeout_s=timeout_s, | |
| ) | |
| ) | |
| if response.job_id != job_id: | |
| raise RuntimeError("Blender resident response job ID differs") | |
| if response.status != "passed": | |
| raise RuntimeError(f"Blender resident job failed: {response.error}") | |
| return response | |
| def shutdown_blender_worker( | |
| endpoint: str, *, timeout_s: float = 30.0 | |
| ) -> BlenderWorkerResponse: | |
| job_id = uuid.uuid4().hex | |
| response = BlenderWorkerResponse.model_validate( | |
| _request( | |
| endpoint, | |
| { | |
| "protocol_version": "1.0", | |
| "op": "shutdown", | |
| "job_id": job_id, | |
| }, | |
| timeout_s=timeout_s, | |
| ) | |
| ) | |
| if response.job_id != job_id or response.status != "shutting_down": | |
| raise RuntimeError("Blender resident worker did not acknowledge shutdown") | |
| return response | |