16
$\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$

3 Answers 3

18
$\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)
if not argv:
    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!"
$\endgroup$
1
  • $\begingroup$ Thanks for you answer, this is a complementary piece of information for Windows. You should put blender --python /path/to/auto_open.py -- %1 into a .bat file, either setting your Path to include blender install dir or putting it by hand (I find it useful if you have several blender install and make several open with script). Mind the %1. Then use the contextual menu open with to set the bat file as the opening program. $\endgroup$
    – StackHola
    Commented Feb 28, 2023 at 14:14
1
$\begingroup$

The above answer is absolutely wonderful I did however have to edit the script a bit to prevent errors from making it function as expected. According to the error, objects do not have an unlink function.

I substituted the clearscene() function to simply remove the default Cube as the light and camera are of no concern for my particular usage. Working in 2.82

#!/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():
    if "Cube" in bpy.data.meshes:
        mesh = bpy.data.meshes["Cube"]
        print("removing mesh", mesh)
        bpy.data.meshes.remove(mesh)

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!"

$\endgroup$
0
$\begingroup$

I'm not a programmer or anything like this. How should I to use these scripts in windows? When its done I can click a fbx file and open it directly from the double click on blender or should I click the script? Please, I need a for dumies guide.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .