Skip to content

Commit

Permalink
fix datablock loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Pullusb committed Sep 20, 2023
1 parent 60e0caf commit 25395df
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions snippets/bpy/append_link_datablock_from_blend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
## generic load datablock from blend
def load_datablocks(self, src, names, type, link=True):
with bpy.data.libraries.load(str(src), link=link) as (data_from, data_to):
setattr(data_to, type, names)
## Generic load datablock from blend (append/link)

def load_datablocks(src, type, names, link=True):
'''append/link datablock from a blend file
ex: load_datablocks('path/to/file.blend', 'node_groups', ['ng_A', 'ng_B'])
args:
src (str or Path): path to source blend file
type (str): datablock type (types found under bpy.data)
names (list or str): single name or list of names to append/link
link (bool): link if True else Append datablock
'''

return getattr(data_to, type)
if isinstance(names, str):
names = [names]
with bpy.data.libraries.load(str(src), link=link) as (data_from, data_to):
setattr(data_to, type, [item for item in getattr(data_from, type) if item in names])
return getattr(data_to, type)

0 comments on commit 25395df

Please sign in to comment.