# 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.

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).

import bpy

if __name__ == "__main__":
for obj in bpy.data.objects:
obj.asset_mark()