← Blender.Today
  • 3
  • Published 12 Sep 2019

How to Append a Material from another .blend file using Python script

I wan’t to append a Material from another blend file using Python script. Can anybody help me doing this ?

officialranimationstudios
12 Sep 2019

13 Comments

Victoria_deBlois

Try this, by replacing the name of the material (MATERIAL_NAME) and the path to the blend file containing the material:

import bpy
bpy.ops.wm.append(filename="MATERIAL_NAME", directory="/PATH/TO/BLENDFILE/NAME.blend\\Material\\")
2

* @Victoria De Blois * in the help I remember they are talking about relative or absolute path. I don't know the difference

Victoria_deBlois

* @zgorg2004 * In this example above the path used is absolute, in the sense that it represents the exact location of the blend file on the disk starting from the root directory (if, for example, the NAME.blend file is located in the Documents folder, then the path needed would be of the form "/home/USERNAME/Documents/NAME.blend" or "C:\Users\Documents\NAME.blend" depending on the operating system, to which you append the suffix \\Material\\).

2
officialranimationstudios Author

* @Victoria De Blois * What If I wanted to add relative path then how to do that. Please Help me out

Victoria_deBlois

* @officialranimationstudios * From what I've tried, appending a material using this command works fine with absolute paths but throws an error of the material not being a library whenever replacing the absolute path with the corresponding relative one.

2

* @Victoria De Blois * thks. like official... what I don't understand is what's a relative path. or maybe this is something in the same folder or under folder?

1
officialranimationstudios Author

* @zgorg2004 * you are somewhat correct Relative path is exactly the same folder in which the blend file is saved or it is there so wherever you place the blend file the relative path is the same folder. Now what i don't Know is to how to create a script that creates a relative path means wherever i place my blend file the script will be in the same folder and should successfully work suppose i gave my blend file to you and you saved the file in different directory then if the paths are absolute then it is not going to work and if if is relative then the new path will be the directory you placed the file in.

Victoria_deBlois

* @officialranimationstudios * In general, blender supports relative paths starting with "//". You can take a look at the API documentation regarding path utilities, which has many interesting and potentially useful path functions.
docs.blender.org/api/current/bpy.path.html Also, when dealing with a large shared project involving multiple users, it is helpful having a predefined scheme of file-handling conventions, to maintain cross-user consistency (for example, following specific naming conventions and folder structure).

2

* @Victoria De Blois * ok I will see this. I'm doing an addon to do a special switch mode pie menu. this is to go in a mode with only the active object, centered and no rotation. so I used float properties to record location x, y , z and same about rotation. but I need to hide other objects when I enter the new mode and unhide them getting back to object mode. not very difficult using ops. but if some object are already hidden when I unhide I lose this state. so I need a possibility to do a list of hidden objects at first (not so easy) and then record this. but I don't know what to use this time. I saw enum property?{attachment SPE_PIEmp4}

scott

All I know at the moment is you'll need to use bpy.ops.wm.append() , but I don't know the exact way you need to use it. A friend wrote github.com/amirpavlo/YAAM which uses the function, so that may be a guide to work off of.

2
agungandre687

Try this, i found this code from b3d.interplanety.org/en/how-to-append-an-object-from-another-blend-file-to-the-scene-using-the-blender-python-api/ and I've improved it a little bit:

import bpy
import os

file_path =  os.path.abspath('Test Blend.blend')  # blend file name
inner_path = 'Material'   # type 
material_name = 'Material_Name' # name

bpy.ops.wm.append(
    filepath=os.path.join(file_path, inner_path, material_name),
    directory=os.path.join(file_path, inner_path),
    filename=material_name
    )
1
Josephbburg

Hi, if you can't get help here, try Blender Stack Exchange, too! I don't know how to do this off of the top of my head, but I know it's possible (many addons to come with Blend files containing the data they use).

Try taking a look at this addon: (your program files folder) \Blender Foundation\Blender\2.80\scripts\addons\materials_library_vx Materials Library VX uses blend files to store its materials and templates. I hope you can figure it out from there. I don't have time to take a look myself right at the moment, but maybe this will help you.

1
Cephas

Hi! I'm trying to write an add-on that I can use to import and export nodegroups from but I can't seem to be able to append from my .blend file in C:\Peter\Nodes.blend with

import bpy
bpy.ops.wm.append(filename="RandomValues", directory="C:\\Peter\\Nodes.blend\\NodeTree\\")

without it throwing a "not a Library" error. What am I doing wrong?

I have very little experience coding and this is my very first forum post every so sorry if I'm doing something wrong.