Filter Out Unwanted Files – Blender 2.6 API

Many of you are familiar with my work on the LDR Importer for Blender, how I adopted it am now the maintainer of the script, and how there are now two other developers working on it, not counting contributors.

In v1.1, the script receive some notable updates by me, rioforce, and the other developers. One of the changes made is the ability to filter out any non-LDraw models from the file browser. This has been on my wish list for a while, but I have not been able to find a simple implementation or documentation on how to do this.

After examining a few scripts, I finally found a simple implementation of it. How simple? A mere five lines. Because this is a useful but hard-to-find-a-good-example feature, I figured I would document it here. This may also work for

We will be filtering out all but .ldr, and .dat files for this example, because that is what the filter in the script does. In addition, I will give you a small snippet of code from the script itself to give a better idea of where you need to add the lines.

In the main operation class of your script, somewhere below the bl_ variables but before def draw(self) or def execute(self), add lines 14-20 to your script. I will explain the syntax afterwards.

Now the syntax is very simple, but the variable names are very specific (AKA they need to be named they way they are). filename_ext is a string containing the default file extension, in this case .ldr (note the period). filter_glob is the StringProperty function from the Blender-only bpy.props module. The default parameter is a string that looks almost like an array. It is semicolon separated, lists all extensions you want to show, and includes the wildcard symbol (*). As you can see, it’s easier to show the syntax than for me to explain it.

As far as I know, this should also work with the upcoming Blender 2.7 release. If it doesn’t… you’ll find out. I plan on having LDR Importer support 2.7 and 2.6 for as long as possible. If it does not work, then we are in trouble. 😛

Well, that’s the end of this tutorial! Short but sweet, tiny but powerful. 🙂


2 thoughts on “Filter Out Unwanted Files – Blender 2.6 API

  1. This was useful to me.
    It seems like the import helper isn’t used anywhere in this script, is it?
    So this line is unnecessary in this particular case:
    from bpy_extras.io_utils import ImportHelper

    1. Actually, the import is used in the class LDRImporterOps arguments. It’s a required argument for all import scripts, just like ExportHelper is for export scripts. Blender itself uses it for its purposes. Just because it is not used in the class does not always mean it is unused. 😉

      I’m glad the code helped you! 😀

Triangular Reactions

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s