Asset broswer won't show the previews when asset was marked in a background blender #93893

Closed
opened 2021-12-09 11:42:22 +01:00 by yonghao lv · 15 comments

System Information
Operating system: Windows-10-10.0.22483-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 2080 SUPER/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 472.47

Blender Version
Broken: version: 3.0.0, branch: master, commit date: 2021-12-02 18:35, hash: f1cca30557
Worked: (newest version of Blender that worked as expected)

Short description of error
1639046242(1).png
Only generate previews when open blender and set them in outliner/python
use bpy.ops.wm.previews_batch_generate() for these files will not work(Error below).

ERROR: bpy_struct: item.attr = val: enum "" not found in ('BEST', 'GOOD', 'REALTIME')
*NOT* Saving D:\ASSET\Plane.blend, because some error(s) happened while deleting temp render data...

Exact steps for others to reproduce the error
running this script to mark every object in a blend file as asset

import argparse
import bpy
import sys


def main(args):
    print("Script args: ", args)

    if len(args) > 0:
        parser = argparse.ArgumentParser()
        parser.add_argument('blend')
        parser.add_argument('--pack', action='append')
        args = parser.parse_args(args)

        blend = args.blend

        bpy.ops.wm.open_mainfile(filepath=blend)

        for o in bpy.data.objects:
            bpy.context.scene.collection.objects.link(o)
            o.asset_mark()
            o.asset_generate_preview()

        # pack
        bpy.ops.file.pack_all()
        try:
            bpy.ops.file.pack_libraries()
        except:
            pass

        bpy.context.view_layer.update()
        bpy.context.preferences.filepaths.save_version = 0  # No backup blends needed
        bpy.ops.wm.save_as_mainfile(filepath=blend, compress=True)


if __name__ == "__main__":
    if "--" not in sys.argv:
        argv = []  # as if no args are passed
    else:
        argv = sys.argv[sys.argv.index("--") + 1:]  # get all args after "--"
    main(argv)

**System Information** Operating system: Windows-10-10.0.22483-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 2080 SUPER/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 472.47 **Blender Version** Broken: version: 3.0.0, branch: master, commit date: 2021-12-02 18:35, hash: `f1cca30557` Worked: (newest version of Blender that worked as expected) **Short description of error** ![1639046242(1).png](https://archive.blender.org/developer/F12719041/1639046242_1_.png) Only generate previews when open blender and set them in outliner/python use `bpy.ops.wm.previews_batch_generate()` for these files will not work(Error below). ``` ERROR: bpy_struct: item.attr = val: enum "" not found in ('BEST', 'GOOD', 'REALTIME') *NOT* Saving D:\ASSET\Plane.blend, because some error(s) happened while deleting temp render data... ``` **Exact steps for others to reproduce the error** running this script to mark every object in a blend file as asset ```Py import argparse import bpy import sys def main(args): print("Script args: ", args) if len(args) > 0: parser = argparse.ArgumentParser() parser.add_argument('blend') parser.add_argument('--pack', action='append') args = parser.parse_args(args) blend = args.blend bpy.ops.wm.open_mainfile(filepath=blend) for o in bpy.data.objects: bpy.context.scene.collection.objects.link(o) o.asset_mark() o.asset_generate_preview() # pack bpy.ops.file.pack_all() try: bpy.ops.file.pack_libraries() except: pass bpy.context.view_layer.update() bpy.context.preferences.filepaths.save_version = 0 # No backup blends needed bpy.ops.wm.save_as_mainfile(filepath=blend, compress=True) if __name__ == "__main__": if "--" not in sys.argv: argv = [] # as if no args are passed else: argv = sys.argv[sys.argv.index("--") + 1:] # get all args after "--" main(argv) ```
Author

Added subscriber: @1029910278

Added subscriber: @1029910278

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

Added subscriber: @JulianEisel

Added subscriber: @JulianEisel
Member

Didn't investigate yet, but my suspicion is that the file is saved before the previews are done generating. This would also be possible to happen for regular GUI use, if the user saves the file fast enough after generating previews. There's no nice solution here I think - either we wait with saving the file until the preview jobs are done, or we accept that the previews are not finished properly. I think waiting is better, even if that can be annoying. For scripts we could make the waiting optional.

Didn't investigate yet, but my suspicion is that the file is saved before the previews are done generating. This would also be possible to happen for regular GUI use, if the user saves the file fast enough after generating previews. There's no nice solution here I think - either we wait with saving the file until the preview jobs are done, or we accept that the previews are not finished properly. I think waiting is better, even if that can be annoying. For scripts we *could* make the waiting optional.
Author

Actually I use a wait to execute script. I am not sure whether saving file will break generating previews.

import subprocess
import os
import bpy

def execute(cmd):
    return subprocess.Popen(cmd, universal_newlines=True)


def execute_blender(args):
    args.insert(0, bpy.app.binary_path)
    print(" ".join(args))
    return execute(args)


def post_process_blend_file(asset,scripts_file_name = 'script_export_blend.py'):
    args = [
        "--background",
        "--factory-startup",
        "--python",
        os.path.join(os.path.dirname(__file__), scripts_file_name),
        "--",
        asset,
    ]

    execute_blender(args).wait()  # Wait for completion.

Actually I use a wait to execute script. I am not sure whether saving file will break generating previews. ```Py import subprocess import os import bpy def execute(cmd): return subprocess.Popen(cmd, universal_newlines=True) def execute_blender(args): args.insert(0, bpy.app.binary_path) print(" ".join(args)) return execute(args) def post_process_blend_file(asset,scripts_file_name = 'script_export_blend.py'): args = [ "--background", "--factory-startup", "--python", os.path.join(os.path.dirname(__file__), scripts_file_name), "--", asset, ] execute_blender(args).wait() # Wait for completion. ```

Added subscriber: @dr.sybren

Added subscriber: @dr.sybren

Actually I use a wait to execute script.

This makes the current process wait for the sub-process. It doesn't make the sub-process wait with saving until the preview generation is done. In other words: something waits, but not for the right thing.

The waiting should happen between the calls to o.asset_generate_preview() and bpy.ops.wm.save_as_mainfile().

Also, never do this:

      try:
            # code here
      except:
          pass

This hides any errors that may occur, regardless of whether they're expected or not.

> Actually I use a wait to execute script. This makes the current process wait for the sub-process. It doesn't make the sub-process wait with saving until the preview generation is done. In other words: *something* waits, but not for the right thing. The waiting should happen between the calls to `o.asset_generate_preview()` and `bpy.ops.wm.save_as_mainfile()`. Also, never do this: ```Py try: # code here except: pass ``` This hides any errors that may occur, regardless of whether they're expected or not.

Added subscriber: @hughetop

Added subscriber: @hughetop

I'm running into this issue as well when trying to create library assets programmatically. Looping over selected objects like:

file_path = get_asset_path(obj)
obj.asset_mark()
obj.asset_generate_preview()
# set catalog, tags, description, author
bpy.data.libraries.write(file_path, {obj})

The assets correctly export and the library updates, but none have previews because they are generated in a background thread and aren't ready by the time the bpy.data.libraries.write function is called. Since there's no way to force it to wait for the preview to finish (even obj.preview_ensure() just returns a blank image), is there a way to tell if the preview actually exists? I'm trying to do something like:

while True:
    if preview_finished(obj):
        break
    time.sleep(0.1)

but I have no idea how to tell if the preview is actually finished generating. It seems to immediately create a blank image so obj.preview.image_pixels is never empty.
So is there any way right now to wait for the preview to finish generating, or programmatically determine if the preview is not blank?

Edit: found on a forum post, here's what my preview_finished function is doing

import numpy as np
def preview_finished(obj):
    arr = np.zeros((obj.preview.image_size[0] * obj.preview.image_size[1]) * 4, dtype=np.float32)
    obj.preview.image_pixels_float.foreach_get(arr)
    if np.all((arr == 0)):
        return False
    return True

It's hacky but I'm hoping there will be a more elegant way to do this in the future.

I'm running into this issue as well when trying to create library assets programmatically. Looping over selected objects like: ```Py file_path = get_asset_path(obj) obj.asset_mark() obj.asset_generate_preview() # set catalog, tags, description, author bpy.data.libraries.write(file_path, {obj}) ``` The assets correctly export and the library updates, but none have previews because they are generated in a background thread and aren't ready by the time the `bpy.data.libraries.write` function is called. Since there's no way to force it to wait for the preview to finish (even `obj.preview_ensure()` just returns a blank image), is there a way to tell if the preview actually exists? I'm trying to do something like: ```Py while True: if preview_finished(obj): break time.sleep(0.1) ``` but I have no idea how to tell if the preview is actually finished generating. It seems to immediately create a blank image so `obj.preview.image_pixels` is never empty. So is there any way right now to wait for the preview to finish generating, or programmatically determine if the preview is not blank? Edit: found on a forum post, here's what my `preview_finished` function is doing ```Py import numpy as np def preview_finished(obj): arr = np.zeros((obj.preview.image_size[0] * obj.preview.image_size[1]) * 4, dtype=np.float32) obj.preview.image_pixels_float.foreach_get(arr) if np.all((arr == 0)): return False return True ``` It's hacky but I'm hoping there will be a more elegant way to do this in the future.
Member

Added subscriber: @DanielGrauer

Added subscriber: @DanielGrauer
Member

Is this solved by 2fba27e6d8 (since now this is not run as a job when in background)?

@DanielGrauer @hughetop @1029910278 : could you re-check this in a fresh 3.6 build from https://builder.blender.org/download/daily/ ?

Is this solved by 2fba27e6d8876025b1e98d1a6e35d5fac6749a27 (since now this is not run as a job when in background)? @DanielGrauer @hughetop @1029910278 : could you re-check this in a fresh 3.6 build from https://builder.blender.org/download/daily/ ?
Philipp Oeser added
Status
Needs Information from User
and removed
Status
Confirmed
labels 2023-04-21 10:07:24 +02:00
Member

This seems to work for me, all the previews show properly with 3.6 builds when i generate them in the background.

This seems to work for me, all the previews show properly with 3.6 builds when i generate them in the background.
Member

This seems to work for me, all the previews show properly with 3.6 builds when i generate them in the background.

good to hear, will wait a bit for others to confirm (in case of no reply, will close in a bit...)

> This seems to work for me, all the previews show properly with 3.6 builds when i generate them in the background. good to hear, will wait a bit for others to confirm (in case of no reply, will close in a bit...)

Seems to work with the blender-3.3.6-stable+v33.948f8298b982-windows.amd64-release build. Sorry for the duplicate account, I switched companies (and therefore email addresses).

Seems to work with the blender-3.3.6-stable+v33.948f8298b982-windows.amd64-release build. Sorry for the duplicate account, I switched companies (and therefore email addresses).
Member

Seems to work with the blender-3.3.6-stable+v33.948f8298b982-windows.amd64-release build. Sorry for the duplicate account, I switched companies (and therefore email addresses).

Good to hear it is working for you as well, so I think we can close now.

> Seems to work with the blender-3.3.6-stable+v33.948f8298b982-windows.amd64-release build. Sorry for the duplicate account, I switched companies (and therefore email addresses). Good to hear it is working for you as well, so I think we can close now.
Blender Bot added
Status
Archived
and removed
Status
Needs Information from User
labels 2023-04-27 14:12:35 +02:00
Philipp Oeser added
Status
Resolved
and removed
Status
Archived
labels 2023-04-27 14:12:44 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
8 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#93893
No description provided.