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.