Batch assign pre existing image files as asset previews

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. The asset marking is working, just need to assign the images as asset previews.

And that error is…

Error relating to this line ‘bpy.ops.ed.lib_id_generate_preview(filepath=dir)’. context is incorrect.

It was solved with the following code:

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"
        )
1 Like

@minato I am trying to use your script and nothing is happening. Could you please make some video on how to use this the correct way?