2
$\begingroup$

How can I create a new asset library, like what bpy.ops.preferences.asset_library_add() does, but also get the new library stored in a variable?

The operator just returns {'FINISHED'}. Normally there's a separate function like D.materials.new() which returns the created datablock, but I can't find any similar .new() function for asset libraries which are stored as part of the preferences in C.preferences.filepaths.asset_libraries.

Feasibly I could store a list of asset libraries, create a new one, and then examine the libraries to see which is not present in the old list, but this seems like a silly workaround.

Edit: I guess it's maybe intentional that this can't be done easily, as you also have to handle saving the user's preferences.

$\endgroup$
5
  • $\begingroup$ OPS are designed to be used in the interface, not in the Python. I would suggest watching my minused answer here blender.stackexchange.com/questions/267405/… why it is better not to use OPS. $\endgroup$
    – Crantisz
    Jun 27, 2022 at 13:21
  • $\begingroup$ Exactly, that's why I'm asking :) $\endgroup$
    – Greg Zaal
    Jun 27, 2022 at 13:25
  • $\begingroup$ Bpy.ops are meant to be used in Python: there are some parts of the API, like some baking, that can only be accessed through bpy.ops. If they weren’t, then there would be no Python interface to them. At this time the asset library interface is in flux and a lot has simply not been implemented yet. $\endgroup$ Jun 27, 2022 at 13:49
  • $\begingroup$ Yeah at first glance I'm inclined to believe that your "workaround" is the solution. At least for now, looking through the docs, it seems like they're working on adding to the API for asset libraries $\endgroup$
    – Jakemoyo
    Jun 27, 2022 at 14:46
  • $\begingroup$ follow up on bpy.ops: The Blender Python fine manual Gotchas section says "Blender’s operators are tools for users to access, that can be accessed with Python too which is very useful." bpy.ops should be avoided in the case where 1) performance is critical and 2) there exists a low level alternative. If either of these is not true, than by all means use bpy.ops but be aware of gotchas. $\endgroup$ Jun 27, 2022 at 14:52

1 Answer 1

3
$\begingroup$

Asset libraries are a property collection and so there is no low level add method. However, bpy.ops.preferences.asset_library_add() follows the common practice of adding the new library to the end of the asset library list, so you don't have to compare two versions of the list and can use this idiom:

bpy.ops.preferences.asset_library_add()
new_library = bpy.context.preferences.filepaths.asset_libraries[-1]

to retrieve a reference to the just added library. This, of course, only works up until the next time you perform an operation that modifies the library list.

$\endgroup$
1

You must log in to answer this question.

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