0
$\begingroup$

I'm writing a script to export assets with custom previews. The script works perfectly for objects, but collections show no previews. The preview data appears to be missing in collection assets.

Blender Version: 4.3.2

Problem Description:

  • When exporting objects (e.g. Cube), the preview image is properly embedded
  • When exporting collections, the .preview data seems missing despite setting it through API

Object (Cube) Preview Data Exists in .blend File - Data API Browser View

Object Preview Data Exists

Collection Preview Data Missing in .blend File

Collection Preview Missing

Code Sample:

import array
from PIL import Image


import bpy
from bpy import types


preview_image_path = "D:/Temp/test_prview_img.png"
preview_resize = (128, 128)

collection_name = "Collection"
export_collection_path = "D:/Temp/test_collection_asset.blend"

object_name = "Cube"
export_object_path = "D:/Temp/test_cube_asset.blend"


collection = bpy.data.collections.get(collection_name)
object = collection.objects.get(object_name)


def write_asset(obj: types.Object, export_path):
    obj.asset_mark()

    preview = obj.preview_ensure()

    image = Image.open(preview_image_path)
    image = image.transpose(Image.FLIP_TOP_BOTTOM)
    image = image.convert("RGBA").convert("RGBa")
    image = image.resize(preview_resize)
    image_data = array.array("i", image.tobytes())

    preview.image_size = preview_resize
    preview.image_pixels = image_data

    data_blocks = set()
    data_blocks.add(obj)
    bpy.data.libraries.write(export_path, data_blocks, compress=True)

    obj.asset_clear()


write_asset(collection, export_collection_path)
write_asset(object, export_object_path)

Question:

Is there special handling required for collection asset previews? Why does the same code work for objects but not collections? Are there additional steps to persist collection preview data through bpy.data.libraries.write?

$\endgroup$

1 Answer 1

0
$\begingroup$

You need workaround:

import array
from PIL import Image
import bpy
from bpy import types

preview_image_path = "D:/Temp/test_preview_img.png"
preview_resize = (128, 128)

collection_name = "Collection"
export_collection_path = "D:/Temp/test_collection_asset.blend"

object_name = "Cube"
export_object_path = "D:/Temp/test_cube_asset.blend"

collection = bpy.data.collections.get(collection_name)
object = collection.objects.get(object_name)


def write_asset(obj: bpy.types.ID, export_path):
    obj.asset_mark()

    if isinstance(obj, bpy.types.Object):
        preview = obj.preview_ensure()

        image = Image.open(preview_image_path)
        image = image.transpose(Image.FLIP_TOP_BOTTOM)
        image = image.convert("RGBA").convert("RGBa")
        image = image.resize(preview_resize)
        image_data = array.array("i", image.tobytes())

        preview.image_size = preview_resize
        preview.image_pixels = image_data

    # WORKAROUND - Store preview image path for collections
    elif isinstance(obj, bpy.types.Collection):
        obj["preview_image_path"] = preview_image_path  # The blendre will auto generate preview

    data_blocks = {obj}
    bpy.data.libraries.write(export_path, data_blocks, compress=True)

    obj.asset_clear()


write_asset(collection, export_collection_path)
write_asset(object, export_object_path)
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.