I think this is what the button is doing under the hood. Then you can go ahead and unlink (curious about how you are doing it).
For some reason, the outliner doesn’t immediately update after running this in the python console. But, if you switch to View Layer (or anything else) and switch back to Orphan data, the outliner seems to have updated.
Maybe, we’ll have to redraw the interface manually after the fact which I am not familiar with.
ah it seems use_fake_user = False does the trick, I’ll double check again to see if it’s work or not. Another related question to this is, How to access all data that reside in orphan data section? Like how to differentiate it with other data that currently in use?
Be very careful when accessing datablocks by name. The name does not have to be unique. It is only unique within a single blend file, but with linking you can have “material ‘MAT’ from file X.blend” and “material ‘MAT’ from file Y.blend” linked into the same blend file.
You could shorten this to:
for mat in bpy.data.materials:
if mat.users == mat.use_fake_user:
Oh, I never thought about that. Thanks for the heads up @sybren.
Yeah! I remember you were explaining this in one of your Scripting for Artist videos. From then I always tried to simplify things, but, sometimes it’s hard for me to realize that I am actually doing it. I think I ll have to mess around with more coding exercises to absorb this idea. Thanks for pointing out!
It’s not a dictionary, it’s an object that implements the __getitem__() function. You can pass a string, in which case it’ll do a lookup by name. You can also pass it a string and a library, and it’ll do a lookup based on that.
lib = bpy.data.libraries["library_name"]
material = bpy.data.materials["material_name", lib]
That will give you the material from that particular library.