0
$\begingroup$

Trying to append a material from one blend file to another but not having any luck. I've tried two methods so far.

Method 1:

bpy.ops.wm.append(
    filepath="material.blend",
    directory="/Users/me/Desktop/Materials/material.blend\\Material\\",
    filename="Material01")

In the console this returns {'FINISHED'} but there is no new material appended to my file.

Method 2:

# path to the blend
filepath = "/Users/me/Desktop/Materials/material.blend"


# name of object(s) to append or link
mat_name = "material01"

# append, set to true to keep the link to the original file
link = False

with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
    data_to.materials = [name for name in data_from.materials if name.startswith(mat_name)]

#link object to current scene
for mat in data_to.materials:
    if mat is not None:
       bpy.context.collection.materials.link(mat)

I read that this method is more appropriate, but again, nothing happens. Method 2 was originally written for objects in the example I saw, so is there maybe something I'm missing for materials?

Interestingly, after running either of these, when I go to manually append (file>append) my file explorer opens on the material I'm trying to append. So it is getting Blender to look at the right material, but not appending it.

Any help greatly appreciated!

CK

$\endgroup$
1

1 Answer 1

0
$\begingroup$

The below seems to work - it appends all materials from the .blend file. Not sure why the original code isn't working but for my purposes the below is fine as each file only contains one material anyway

with bpy.data.libraries.load(filepath, link=False) as (data_from, data_to):
    data_to.materials = data_from.materials
$\endgroup$
1
  • $\begingroup$ I guess the code in question is not working because of the if-statement. You are asking for a material, that startswith "material01" in its name which is most likely not present in the other blend file. $\endgroup$
    – brockmann
    Sep 28, 2021 at 9:40

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