1
$\begingroup$

What I usually do is assign all my materials to a Cube and append it, and the materials just come with it.

The problem is that bpy.ops.wp.append() require a filename, and I have no way that I know to create a for loop that would cycle through all the mats in the file and append them. Usually when I want to import all files in a folder I use glob.glob() to create a list but that obviously wouldn't work in this case. Is there an equivalent to glob.glob() for a blend file?

Appending the cube has it's limit since I need to remember to assign my materials to it, and when you are working with hundreds of mats it becomes problematic.

$\endgroup$

1 Answer 1

3
$\begingroup$

Example using BlendDataLibraries based on How can I import BI materials from another blender file with python?. The script scans the given directory for blend files and appends all materials:

import bpy
import os

blend_files = []
for root, dirs, files in os.walk("/home/<USER>/..."):
    for file in files:
        if file.endswith(".blend"):
             blend_files.append(os.path.join(root, file))

# Append all materials from all blends within the folder
for filepath in blend_files:
    with bpy.data.libraries.load(filepath, link=False) as (data_from, data_to):
        data_to.materials = data_from.materials 

A little bit of housekeeping might be nice in this case:

with bpy.data.libraries.load(filepath, link=False) as (data_from, data_to):
    data_to.materials = [m for m in data_from.materials if m.startswith("MATLIB_")]
$\endgroup$
1
  • $\begingroup$ Thank you I think I can work with that! $\endgroup$
    – globglob
    Dec 17, 2019 at 13:28

You must log in to answer this question.

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