13
$\begingroup$

Is there a way to make Blender auto import files that are not .blend when you open them?

For example if you open a .fbx file by double clicking on it, and you have Blender set as default application, then import it to a new .blend.

Is it possible to make Blender import this .fbx file just like you would do it manually by using File > Import > FBX?

By default, Blender just opens up and loads default new scene when you open anything that is not .blend file.

$\endgroup$
15
$\begingroup$

No, Blender doesn't support this. Mainly because there are many options you may want to configure while importing a file.

However if its vital for your work-flow, writing a script to do this is not such a difficult task.

eg:

blender --python /path/to/auto_open.py --

Contents of auto_open.py

import os
from sys import argv
argv = argv[argv.index("--") + 1:]

# format specific options... change as you like
args_fbx = dict(
    # global_scale=1.0,
    )

args_obj = dict(
    # use_image_search=False,
    )

args_3ds = dict(
    # constrain_size=0.0,
    )

import bpy
for f in argv:
    ext = os.path.splitext(f)[1].lower()

    if ext == ".fbx":
        bpy.ops.import_scene.fbx(filepath=f, **args_fbx)
    elif ext == ".obj":
        bpy.ops.import_scene.obj(filepath=f, **args_obj)
    elif ext == ".3ds":
        bpy.ops.import_scene.autodesk_3ds(filepath=f, **args_3ds)
    else:
        print("Extension %r is not known!" % ext)
else:
    print("No files passed")

File associations typically add the file to the end of the command to form something like this:

blender --python /path/to/auto_open.py -- /home/me/my_model.fbx

Linux (Setup Script)

Setting file associations can be done via this script (to save you having to do it manually), tested on Linux with Gnome.

Run this once bash blender_auto_import.sh

#!/bin/bash
echo "Setting up Blender file extensions..."
DESKTOP_FILE="$HOME/.local/share/applications/blender_auto_import.desktop"
MIME_DIR="$HOME/.local/share/mime/packages"
PYTHON_SCRIPT="$HOME/.config/blender/blender_auto_open.py"
EXT_LIST="fbx obj 3ds"

mkdir -p $(dirname "$DESKTOP_FILE")
mkdir -p $(dirname "$PYTHON_SCRIPT")

tee $DESKTOP_FILE > /dev/null <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=Blender Auto Import
Exec=blender --python $PYTHON_SCRIPT -- %F
MimeType=application/x-blender_auto_import;
Icon=blender
Terminal=false
Type=Application
Categories=Graphics;3DGraphics;
EOF
echo "  written: '$DESKTOP_FILE'"

# setup file associations
for EXT in $EXT_LIST; do
    XML_FILEPATH="$MIME_DIR/x-blender_auto_import_$EXT.xml"
    tee $XML_FILEPATH > /dev/null <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/x-blender_auto_import_$EXT">
        <comment>Blender $EXT file</comment>
        <glob pattern="*.$EXT"/>
    </mime-type>
</mime-info>
EOF
    echo "  written: '$XML_FILEPATH'"
done

# update desktop files
update-desktop-database $(dirname "$DESKTOP_FILE")
update-mime-database $(dirname "$MIME_DIR")

for EXT in $EXT_LIST; do
    xdg-mime default ${DESKTOP_FILE##*/} "application/x-blender_auto_import_$EXT"
done

tee $PYTHON_SCRIPT > /dev/null <<EOF
import bpy


# format specific options... change as you like
args_fbx = dict(
    # global_scale=1.0,
    )

args_obj = dict(
    # use_image_search=False,
    )

args_3ds = dict(
    # constrain_size=0.0,
    )

def clear_scene():
    for scene in bpy.data.scenes:
        for obj in scene.objects[:]:
            scene.objects.unlink(obj)


def main():
    import os

    from sys import argv
    argv = argv[argv.index("--") + 1:]

    for f in argv:
        ext = os.path.splitext(f)[1].lower()

        if ext == ".fbx":
            bpy.ops.import_scene.fbx(filepath=f, **args_fbx)
        elif ext == ".obj":
            bpy.ops.import_scene.obj(filepath=f, **args_obj)
        elif ext == ".3ds":
            bpy.ops.import_scene.autodesk_3ds(filepath=f, **args_3ds)
        else:
            print("Extension %r is not known!" % ext)
    else:
        print("No files passed!")

if __name__ == "__main__":
    clear_scene()
    main()
EOF
echo "  written: '$PYTHON_SCRIPT'"

echo "Done!"
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.