8
$\begingroup$

I have around 100 separate .blend files that have one material each, is there a way to merge the materials from all of these blend files into one?

I am trying to do this to add the materials into the asset browser without having to merge them and add each material one by one, so I can easier manage my asset browser.

$\endgroup$

2 Answers 2

9
$\begingroup$

I'll assume that your files are placed in the same directory—or a couple of folders deep in the same directory. If so, in the file browser that pops up when you click File > Append, you can switch the Recursion level of your display to one of three levels to see all append-able data blocks present in the .blend files from that folder:

enter image description here

You probably don't want to see all data blocks, so you might wanna filter out the rest and only leave Shading on:

enter image description here

Now you should be able to select multiple materials from different files at once to append.

$\endgroup$
3
  • $\begingroup$ Thanks a lot, by any chance is it possible to mark all materials in a blend file as asset? $\endgroup$ Commented Mar 14, 2022 at 23:01
  • $\begingroup$ Not in front my computer atm so I can't check but if you switch the Outliner to the "Blender File" mode you should see a list of all data blocks in the file: docs.blender.org/manual/en/latest/_images/… You might be able to select multiple materials at once and mark as asset there? $\endgroup$
    – Kuboå
    Commented Mar 14, 2022 at 23:24
  • $\begingroup$ this worked.... $\endgroup$ Commented Mar 15, 2022 at 1:00
5
$\begingroup$

EDIT: to read all materials, you don't want to use bpy.ops.wm.append but rather you want to use library management code. Updated the answer to reflect this.

There is no automatic way to do this. The manual way is to open a new blend file and append the materials from each of the 100 other files to that file and then save it.

You could write a Python script to do that. If, for instance, all of the blend files were in the same directory, the script could iterate through each file, appending every material from that file using bpy.data.libraries.load Here's an example of doing that modified from the matlib manager addon

with bpy.data.libraries.load(blend) as (data_from,data_to):
    data_to.materials = data_from.materials

Note that this is equivalent to linking the material, but now you have a list of names. If you want to append instead, you would then go through the list and call bpy.ops.wm.append for each material.

Here is the starting point for code to accomplish this. It's only a starting point because it doesn't handle the error cases, like not being able to open a given blend file and it doesn't do any filtering.

import bpy
from pathlib import Path

blendfilespath = Path(PATH_TO_MATERIALS_DIRECTORY)

for file in blendfilespath.glob("*.blend") :
    file_path = Path(file)
    inner_path = "Materials"
    with bpy.data.libraries.load(str(file_path)) as (data_from, data_to):
        data_to.materials = data_from.materials
    for linked_material in data_to.materials:
        object_name = linked_material.name
        bpy.ops.wm.append(
        filepath=str(file_path / inner_path / object_name),
        directory=str(file_path / inner_path),
        filename=object_name
    )
$\endgroup$
3
  • $\begingroup$ blender.stackexchange.com/questions/245555/… any good? $\endgroup$
    – John Eason
    Commented Mar 14, 2022 at 22:31
  • $\begingroup$ @JohnEason That's good if you want to tag a lot of things in existing files as assets for the asset browser. The problem here seems to be how to append a lot of content from many other files to one file. $\endgroup$ Commented Mar 15, 2022 at 13:38
  • $\begingroup$ Ah ok. I remember it being discussed at the time @Gorgious posted his code, but must admit I'd forgotten it didn't include appending them into one file. $\endgroup$
    – John Eason
    Commented Mar 15, 2022 at 14:13

You must log in to answer this question.

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