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...

Showing posts with label import. Show all posts
Showing posts with label import. Show all posts

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.

March 20, 2012

importing .obj

The separator used here is '\\', but this varies from OS to OS. Python has a module called os to take some of the headache away. The above code allows you to do this stuff manually, research and experiment with the os module, it's great to understand how it works.

You could write a plugin using the directory selector built into Blender, so that you can navigate to a directory and obtain the string full_path_to_directory that way, but that's a different topic.

Blog reader pmpfx wondered how this syntax was glued together, so I think the above should clarify the path issues. The following snippet uses the os module to list the content of a directory. It will gather all files that are .obj and import them. No error checking is done, the path has to be valid. Conveniently the os module provides ways to check if a path is valid, read the docs. Here's a description of how to sanitize a blend

August 12, 2011

Scripted Keyframe animation from imported mocha file

This will provide some insight into the pleasantly trivial, but still involved, nature of scripting keyframes for a spline object. Currently i only do basic 'POLY' form of spline. read the code here :

github.com/zeffii/AE--afterfx--Mocha-shape-import

May 14, 2011

Blender 2.5 Python Reading CSV

Here is a sequence of examples that show how to read csv (into blender)

naive

this first gist will also print the newline, at the end of each csv line, this can be annoying. While this version drops the newline. If you are seeing the data in the csv for the first time then it is useful to set a break at 10 lines to get a comprehension of the data. here enumerate is used to track the element index without setting up a counter first.

splits the incoming line easily


Python also has a csv module, and we like a bit of convenience if we can handle it. Here is the documentation. That might be more useful to you. You can still use the enumerate method on file

Here's another use of csv, and writing json

Most notably is the skipping of the first line using __next__().

old text

The reading and parsing has nothing to do with blender specifically, I include it because i'm going to plot it using blender. The file i was reading has over 500,000 lines, and element 2,3 and 4 of each line describes a coordinate.

Doing stuff with the read dataset

While the method of reading data is pretty general, the implementation of the code after (or during the read of) the data can be very specialized. Here is a rather lengthy example of one of the first longer bits of code I ever wrote. The splitting of data on commas happens in the initFunction.

the old code is here