|
| 1 | +""" |
| 2 | +CLI Script to save out the previews of all assets: |
| 3 | +
|
| 4 | +Argument Order: |
| 5 | + 0. blender executable |
| 6 | + 1. background arg |
| 7 | + 2. factory startup arg: str |
| 8 | + 3. blend file to open: str |
| 9 | + 4. run python script arg |
| 10 | + 5. script to run |
| 11 | + 6. output directory: str |
| 12 | + 7. asset name: str |
| 13 | + 8. asset type: str |
| 14 | + 9. do whole file: bool |
| 15 | +""" |
| 16 | + |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +import bpy |
| 22 | +import numpy as np |
| 23 | +import OpenImageIO.OpenImageIO as oiio |
| 24 | +from OpenImageIO import ROI, ImageBuf, ImageSpec |
| 25 | + |
| 26 | + |
| 27 | +DIRECTORY = Path(sys.argv[6]) |
| 28 | +ASSET_NAME = sys.argv[7] |
| 29 | +ASSET_TYPE = sys.argv[8] |
| 30 | +DO_WHOLE_FILE = sys.argv[9] == "True" |
| 31 | + |
| 32 | +print(f"{DIRECTORY=}") |
| 33 | +print(f"{ASSET_NAME=}") |
| 34 | +print(f"{ASSET_TYPE=}") |
| 35 | +print(f"{DO_WHOLE_FILE=}") |
| 36 | + |
| 37 | + |
| 38 | +def set_compression( |
| 39 | + spec: ImageSpec, extension: str, quality: int = 100, quality_other: Any = None |
| 40 | +) -> None: |
| 41 | + if extension == ".bmp": |
| 42 | + spec.attribute("compression", quality_other) # "rle4" or "rle8" |
| 43 | + elif extension in {".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi"}: |
| 44 | + spec.attribute("Compression", f"jpeg:{quality}") |
| 45 | + elif extension == ".exr": |
| 46 | + # quality_other= "none", "rle", "zips", "zip", "piz", "pxr24", "b44", "b44a", "dwaa", "dwab" |
| 47 | + spec.attribute("compression", quality_other) |
| 48 | + elif extension == ".png": |
| 49 | + # quality needs to be between 0 and 9 |
| 50 | + quality = quality / 100 * 9 |
| 51 | + spec.attribute("png:compressionLevel", quality) |
| 52 | + elif extension in {".tif", ".tiff", ".tx", ".env", ".sm", ".vsm"}: |
| 53 | + # quality_other= "none", "lzw", "zip", "ccittrle", "jpeg", "backbits" |
| 54 | + spec.attribute("compression", quality_other) |
| 55 | + spec.attribute("tiff:compression", quality) |
| 56 | + |
| 57 | + |
| 58 | +def numpy_to_image( |
| 59 | + buf: np.ndarray, |
| 60 | + save_path: Path | str = None, |
| 61 | + quality: int = 100, |
| 62 | + quality_other: Any = None, |
| 63 | +) -> ImageBuf: |
| 64 | + """ |
| 65 | + Convert a numpy array to an image. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + buf : np.ndarray |
| 70 | + The numpy array to convert. |
| 71 | + path_path : Path or str |
| 72 | + The path to the image file to write. If not path is supplied the image will not be saved. |
| 73 | + quality : int, optional |
| 74 | + The quality of the image to save. Only applies if `save_path` is provided. |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + ImageBuf |
| 79 | + The image. |
| 80 | +
|
| 81 | + """ |
| 82 | + image = ImageBuf(ImageSpec(buf.shape[1], buf.shape[0], buf.shape[2], oiio.FLOAT)) |
| 83 | + image.set_pixels(ROI(0, buf.shape[1], 0, buf.shape[0]), buf) |
| 84 | + if save_path is not None: |
| 85 | + set_compression( |
| 86 | + image.spec(), |
| 87 | + Path(save_path).suffix, |
| 88 | + quality=quality, |
| 89 | + quality_other=quality_other, |
| 90 | + ) |
| 91 | + image.write(str(save_path)) |
| 92 | + return image |
| 93 | + |
| 94 | + |
| 95 | +def save_out_preview(item: bpy.types.ID): |
| 96 | + if item.preview: |
| 97 | + pixels = np.array(item.preview.image_pixels_float) |
| 98 | + |
| 99 | + d3 = 4 |
| 100 | + if len(pixels) == item.preview.image_size[0] * item.preview.image_size[1] * 3: |
| 101 | + d3 = 3 |
| 102 | + pixels.resize(list(item.preview.image_size) + [d3]) |
| 103 | + fp = ( |
| 104 | + DIRECTORY |
| 105 | + / f"{Path(bpy.data.filepath).stem}_{item.__class__.__name__.lower()}_{item.name}_preview.webp" |
| 106 | + ) |
| 107 | + print(f"Saving preview for {item.name} to {fp}") |
| 108 | + numpy_to_image( |
| 109 | + pixels[::-1], |
| 110 | + save_path=fp, |
| 111 | + ) |
| 112 | + |
| 113 | + else: |
| 114 | + print(f"No preview found for {item.name}") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + DIRECTORY.mkdir(parents=True, exist_ok=True) |
| 119 | + |
| 120 | + if DO_WHOLE_FILE: |
| 121 | + for item in bpy.data.actions: |
| 122 | + if item.asset_data: |
| 123 | + save_out_preview(item) |
| 124 | + |
| 125 | + for item in bpy.data.collections: |
| 126 | + if item.asset_data: |
| 127 | + save_out_preview(item) |
| 128 | + |
| 129 | + for item in bpy.data.materials: |
| 130 | + if item.asset_data: |
| 131 | + save_out_preview(item) |
| 132 | + |
| 133 | + for item in bpy.data.node_groups: |
| 134 | + if item.asset_data: |
| 135 | + save_out_preview(item) |
| 136 | + |
| 137 | + for item in bpy.data.objects: |
| 138 | + if item.asset_data: |
| 139 | + save_out_preview(item) |
| 140 | + |
| 141 | + for item in bpy.data.worlds: |
| 142 | + if item.asset_data: |
| 143 | + save_out_preview(item) |
| 144 | + else: |
| 145 | + item = getattr(bpy.data, ASSET_TYPE).get(ASSET_NAME) |
| 146 | + print(f"Getting item: Name:{ASSET_NAME}, Type:{ASSET_TYPE} | item: {item}") |
| 147 | + if item: |
| 148 | + save_out_preview(item) |
| 149 | + else: |
| 150 | + print(f"Item {ASSET_NAME} not found in {ASSET_TYPE}") |
0 commit comments