5
$\begingroup$

I have a large collection of models that I'd like to batch mark as asset. I use a .bat file to open the .blend and then mark as asset and then save and quit. It works but the issue is the preview. The models I'm trying to mark as asset already have rendered preview images. I just want to assign the corresponding image preview to the asset. This is what I've got so far:

import bpy

for obj in bpy.data.objects:
    if obj.type == "MESH":
        obj.asset_mark()
        name = obj.name + ".jpg"
        dir = "C:\\path\\" + name
        bpy.ops.ed.lib_id_generate_preview(filepath=dir)
        
bpy.ops.wm.save_mainfile()

It gives an error of course.

$\endgroup$

1 Answer 1

6
$\begingroup$

You need to fix two things :

  • bpy.ops.ed.lib_id_generate_preview is used to generate an automatic preview from the file's contents. You want to use bpy.ops.ed.lib_id_load_custom_preview instead.

  • Since the operator is normally called from within the asset editor, you need to override it to use it in a script. Thankfully the override is pretty simple, it only needs to know about the object you want to fetch the preview for (called id).

    I did not follow your exact usecase but I think you can adapt it easily :

    import bpy
    
    
    if __name__ == "__main__":
        for obj in bpy.data.objects:
            obj.asset_mark()
            with bpy.context.temp_override(id=obj):
                bpy.ops.ed.lib_id_load_custom_preview(
                    filepath=f"C:/path/{obj.name}.png"
                )
    

For Blender 3.2 and below, use this instead :

bpy.ops.ed.lib_id_load_custom_preview(
    {"id": obj}, 
    filepath=f"C:/path/{obj.name}.png"
)

For the non-python savvy people, I'll plug the (free) add-on I'm developing to automate asset browser repetitive tasks :

https://github.com/Gorgious56/asset_browser_utilities/releases

Example of an operation you can do with it : Load previews from disk if their name corresponds to the asset names. It can search recursively in subfolders.

enter image description here

$\endgroup$
3
  • $\begingroup$ That actually works quite well, I've tested it. It saves a lot of manual work of assigning previews. Thanks. $\endgroup$
    – Kenan Clay
    Commented Dec 8, 2021 at 18:55
  • 3
    $\begingroup$ @KenanClay Glad it helped ! on separate but related note, if you mark assets by code they don't generate their own automatic preview, contrary to when you use the interface to do it. You can use the operator in your question to do it, but you'll have to find a way to wait a little bit so that the thread responsible for generating the preview finishes calculating it. (eg don't close a file just after generating the preview or it won't register) just a heads-up ! $\endgroup$
    – Gorgious
    Commented Dec 8, 2021 at 18:58
  • $\begingroup$ That worked like a charm! Thank you! $\endgroup$
    – satishgoda
    Commented May 15 at 22:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .