Skip to content

Commit 2de9914

Browse files
committed
add poc blender preview
1 parent 469a9c7 commit 2de9914

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

preview.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import subprocess
2+
from abc import ABC
3+
4+
import typing
5+
from enum import Enum
6+
7+
from preview_generator.preview.generic_preview import ImagePreviewBuilder
8+
from preview_generator.utils import MimetypeMapping, ImgDims
9+
10+
class BlenderEngine(str, Enum):
11+
WORKBENCH_ENGINE = "BLENDER_WORKBENCH" # The fastest, don't render much (no texture, etc,…)
12+
EEVE_ENGINE = "BLENDER_EEVEE" # fastest real render, videogame-like render.
13+
CYCLE_ENGINE = "CYCLES" # Raytracing engine, very slow !
14+
15+
class ImagePreviewBuilderBlender(ImagePreviewBuilder):
16+
weight: int = 150
17+
engine: BlenderEngine = BlenderEngine.WORKBENCH_ENGINE
18+
thread_number: int = 4
19+
20+
@classmethod
21+
def get_label(cls) -> str:
22+
return "Blender Preview Builder"
23+
24+
@classmethod
25+
def get_supported_mimetypes(cls) -> typing.List[str]:
26+
return ['application/x-blender']
27+
28+
@classmethod
29+
def get_mimetypes_mapping(cls) -> typing.List[MimetypeMapping]:
30+
return [MimetypeMapping("application/x-blender", ".blend")]
31+
32+
33+
@classmethod
34+
def check_dependencies(cls) -> None:
35+
#TODO - check bpy exist.
36+
pass
37+
38+
def build_jpeg_preview(
39+
self,
40+
file_path: str,
41+
preview_name: str,
42+
cache_path: str,
43+
page_id: int,
44+
extension: str = ".jpeg",
45+
size: ImgDims = None,
46+
mimetype: str = "",
47+
) -> None:
48+
if not size:
49+
size = self.default_size
50+
preview_file_path = "{path}".format(
51+
path=cache_path + preview_name, extension=extension, page_id="####"
52+
)
53+
# TODO : use bpy instead to:
54+
# - set the render size
55+
# - get the frame number
56+
# - give proper name to preview
57+
# I didn't do it now, as bpy doesn't run on my debian unstable (hard to troubleshoot).
58+
command = ["blender", "-b", file_path, "-o", preview_file_path, "-E", self.engine, "-t", str(self.thread_number), "-f", str(page_id+1), "-F", "JPEG"]
59+
subprocess.check_call(
60+
command,
61+
stdout=subprocess.DEVNULL,
62+
stderr=subprocess.STDOUT,
63+
)

0 commit comments

Comments
 (0)