Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

June 03, 2012

Scripted import of materials from a .blend

Part 1 - The setup

This is the first step towards a material library loader for cycles node view. In the following scenario imagine you have a folder inside /scripts called addons_contrib/io_material_loader. io_material_loader will contain a .blend called practicality.blend. That blend will contain a material called 'vibrant'. The import material function takes a known name of a material and a known name of a blend and looks in that blend for the material. It performs no error checking in this scenario, mainly because eventually the function will only be called with materials that I(or the script) know are present.

There are some drawbacks to this approach as it relies on ops, (insert clarification later). But for now it will suffice to act as example.

Part 2 - Reading the material content of a .blend

This will allow the script to populate a menu with the materials present in a blend file. It will rely on the methods used by ideasman_42 in this gist : here. My modification step which is viewable here takes a large chunk of that script and prints the names of blends located inside addons_contrib/io_material_loader. The next step below will make more sense now. It should print filenames and material names contained therein.

Part 3 - ditching part 1

Now we have the basic code soup required to get the job done. After having spent the best part of a day trying to pass a property along with layout.operator_menu_enum( ).some_property = some_value, which seems like an utterly trivial thing to do, it appears that the API doesn't provide this functionality. I died a little inside as ideasman_42 confirmed that it probably wasn't a feature of the API. If you would like to see an example of how I had hoped to implement the menu ( example script, displays new menu in nodeview ). Perhaps there is still a neat solution, but i've exhausted my desire to find a solution to the first idea.

Now the decision is where to add the menu then, in nodeview properties panel? Uch... if i must, albeit just to have a working prototype - so i can use it. This leaves me to settle on the uglier flat list, because doing an eval for creating menus is not an idea i like to think about. This is my current (lame) solution, so i can focus on combining parts 1 and 2. clicky

What follows is the combination of all parts:

Part 4 - create the addon boilerplate

What if i want to be able to use this as a full addon, by enabling it permanently in the addons menu? Well, it's not very difficult - but it probably doesn't make much sense to you if you haven't done it a few times. So this is what must be done, this zip contains a folder addons_contrib which you should dump (along with its contents) directly into the /scripts folder. To get a deeper appreciation of the content, take your time to read through the .py files. Note: to use the library feature, you need to have a few .blend files located inside addons_contrib/io_material_loader, because that's where the script expects to find them. Then you will find a new Materials menu on the Node View header.


  1. I don't know if it's possible to write an script that:
    create a window where you can import .blend archives and(this is the difficult part) the user can select the differents objects of the .blend archive.

    I've a necklace and I want that when I import it the different pieces of the necklace as different objects. I need to manage that pieces in the script in a separated way.

    do you know if its possible?


  2. I saw your post on B1enderAr1ists but I do not really understand the question. So I will ask you a few questions perhaps we can clear this up.

    Exporting from blender should not be a problem. The question is what kind of program (does it have a name) do you want to import into? Tell me more about that program.

  3. Sorry I don't have a very good English.
    I want to do a program in Python where the user could choose a necklace and modify the colors.
    For this I want to import into a Python'window this necklace but I don't know haw to manage the diferents parts of my necklace. I have to manage the parts the user select, and I don't know how to do that.
    Do yo understand?

  4. I think you should look at the game engine, or look at exporting to three.js so you can make it a web based program (javascript). But if you really want to do what you are thinking of you should make real plans or find someone who speaks your native tongue to help you define the problem and work towards a solution. -- I can not help you with this vague idea.

  5. Well,

    I've another question. Hope you understand me and help me.

    How I can write a script in blender, that creates a "canvas" or a "window" in which I can show a .blend(my necklace).
    I want the user to be able to select the diferents components of the necklace, that is being shown in the canvas.


  6. Sorry Daniela, You should look into the Blender Game Engine and the Blender Player, there are active subforums on BlenderArtists.org with people more capable to give you suggestions. But I strongly advise you to draw a mockup in photoshop/gimp so you can show people what you would like to do. Take your time to write a better description.

  7. gracias hermano este script es muy util para un programa que estoy creando,para linux.


Please use Blender.StackExchange.com for python scripting questions unrelated to this post.