Wow. Kind of blended into the new year without noticing. But then 2009 hasn’t been a bad year. Let’s keep it up :)
In my series of articles about Blender Export scripts, I explored how we can use Blender scenes as game asset libraries. In this article, I cover basic facilities needed to export a scene to several files representing
game assets.
Interactive Session
Try this:
Blender.sys.makename(ext='.foo')
Then this:
Blender.sys.makename()
Using the above, you can avoid accidentally overriding the .blend file, making sure, the default export file name uses the correct extension.
Now let's try this:
data='foo' fname=Blender.sys.makename(ext='.exnfo') fname # prints 'PATH_TO_BLEND_FILE/BLEND_FILE_NAME.exnfo' file=open(fname,'w') file.write(data) file.close() Blender.sys.exists(fname) # prints '1' for true input=open(fname,'r') target=input.read() target # prints foo! input.close()
Use something like this to retain the path to export to across Blender sessions, saving
frustrating moments browsing to the same path over and over.
The Blender file chooser isn't terribly helpful to export to folders as it doesn't allow selecting a directory so easily. Worse, I haven't really found a way that a new directory can be created using a Blender script (being Noob-y?). Having said that, with a little assistance, we can still get a user to enter a directory path, and with the above, we make it easy to just reuse the same path.
To generate file names under a given path, use something like:
x=Blender.sys.makename() parent=Blender.sys.dirname(x) child=Blender.sys.basename(x) dirname=Blender.sys.join(parent,child) fname=Blender.sys.join(parent,'dummy.txt') fname # just print the result to be sure...
Note that x and dirname are... exactly the same thing. This is just to illustrate defining a default export folder (if we want to export a scene to several files) and creating a path to export to.
Finally, code to pop a file chooser and bind it to a callback will look like:
# the callback
def exportTo(fname)
# do something useful...
#pop the file chooser
Blender.Window.FileSelector(exportTo, 'Meshes in Exile','dummy.myExtension')
(not sure if this works in interactive mode, but that's what we need)
Putting it all together, we can create a script that:
- Retrieves a path to export to, from a well known location (as in, the same folder as the blender file). The export path is (sensibly) specific to each Blender scene.
- Suggests such previous export path, if available.
- Otherwise suggests a reasonable path for an export folder
- Generates filenames to export within such export folder.
Checkout previous articles for 'actually how to export the real data'.


Comments