2
$\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$
2
$\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()
            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$
2
  • $\begingroup$ That actually works quite well, I've tested it. It saves a lot of manual work of assigning previews. Thanks. $\endgroup$
    – Kenan Clay
    Dec 8 '21 at 18:55
  • 1
    $\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
    Dec 8 '21 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.