3DS, Collada import through blender in SDK

What shall I say… Its in svn now :slight_smile:

With blender bundled and some python script magic I was able to teach the SDK to use blender as an importer, so now the Model Import Tool and the right-click “Convert to j3o” option also work for 3DS and Collada DAE files :slight_smile: This also means you can now batch-convert multiple 3ds or dae files by copying them to the assets folder and selecting multiple files (@jagwire)

For those who are interested in how it works and how it can be extended, have a look at the commit. Blender is called with the --background and --python-script command line options to convert the files, the scripts are in the commit as well. The model is first imported in blender and then saved as a blend file, finally imported via @Kaelthas great importer. All this happens in the background without blender even popping up. The python script does not much more than call the importer command and then save the file.

I actually found a template in blender (blender text editor) showing basically everything I needed to know, might also be interesting for people who want to do other blender-related tools like a model “game-readiness-checker”, the things described on our blender wiki page as automated processes in the SDK and stuff like that :wink:

Using this we can support all kinds of files basically, we do have to add support for each format separately but its very easy to do so from now on if theres a blender importer :smiley: Note that still doesn’t get rid of the issue of “not game-ready model” :wink:

Known issues:

  • The model import tool might fail when textures are not referenced externally by blender (and thus not reported or recognized by the importer), the model will show successfully in the preview but not successfully be converted to j3o in the project. Copy the model and textures to the assets folder to avoid this issue and convert the model. Most probably the model will not successfully display the textures anyway (they are not in slots that jme supports, still they keep the model from loading when they are not there in blender).

Cheers,
Normen

10 Likes

This. :slight_smile:

1 Like

:-o this is huge Normen.

1 Like

Awesome :)! Good job as usual

1 Like

Super Puper!

1 Like

Great !

1 Like

This is great stuff! Well played.

1 Like

I now made it possible to also set blender import key options when importing a model format supported through blender:

3 Likes

@normen , don’t you think to add LWO support? Lightwave users will be happy. :slight_smile:
But you will need to switch the addon on. It’s off by default.

In python to switch the addon on you will need to do this:
[java]
bpy.ops.wm.addon_enable(module=“io_import_scene_lwo”)
[/java]

You will need to do this every time when blender runs.

1 Like

When blender is downloaded from the web and put in the installer, I can’t go to that menu in between… I guess somebody has to prepare presets and blender configuration hints so we know how to manipulate the settings files etc :wink:

@normen said: When blender is downloaded from the web and put in the installer, I can't go to that menu in between.. I guess somebody has to prepare presets and blender configuration hints so we know how to manipulate the settings files etc ;)

You can enable the script in your python:
[java]bpy.ops.wm.addon_enable(module=”io_import_scene_lwo”)[/java]
Voila, and you can run the lwo importer as obj importer… :slight_smile:

1 Like

Btw, I see other interesting importers there like MakeHuman etc… I guess some of these need some post-processing but some would definitely be interesting for jME. We can extend the BlendKey to have extended options for scripts as well btw :slight_smile:

Ok, I talkedt to Normen and he said that JME already has LWo importer. But anyway i did the LWO importer through the blender.
If you want to use it somewhen you are welcome.

Taken from here: http://code.google.com/p/jmonkeyengine/source/browse/#svn%2Ftrunk%2Fsdk%2Fjme3-blender%2Fsrc%2Fcom%2Fjme3%2Fgde%2Fblender%2Fscripts

import_lwo.py:
[java]

This script invokes blender to import and save external model formats as

.blend files to be processed further.

Example usage for this importer:

blender --background --factory-startup --python $HOME/import_3ds.py – \

–i="/tmp/hello.lwo" \

–o="/tmp/hello.blend" \

See blender --help for details.

import bpy

Imports a file using importer

def import_file(file_path):
# Enable LightWave addon
bpy.ops.wm.addon_enable(module=“io_import_scene_lwo”)

# Import the model
bpy.ops.import_scene.lwo(filepath = file_path)

Clear existing objects.

def clear_scene():
scene = bpy.context.scene
scene.camera = None
for obj in scene.objects:
scene.objects.unlink(obj)

Save current scene as .blend file

def save_file(save_path):
# Check if output file exists already
try:
f = open(save_path, ‘w’)
f.close()
ok = True
except:
print(“Cannot save to path %r” % save_path)

    import traceback
    traceback.print_exc()

# Save .blend file
if ok:
    bpy.ops.wm.save_as_mainfile(filepath=save_path)

def main():
import sys # to get command line args
import argparse # to parse options for us and print a nice help message

# get the args passed to blender after "--", all of which are ignored by
# blender so scripts may receive their own arguments
argv = sys.argv

if "--" not in argv:
    argv = []  # as if no args are passed
else:
    argv = argv[argv.index("--") + 1:]  # get all args after "--"

# When --help or no args are given, print this help
usage_text = \
"Run blender in background mode with this script:"
"  blender --background --factory-startup --python " + __file__ + " -- [options]"

parser = argparse.ArgumentParser(description=usage_text)

# Possible types are: string, int, long, choice, float and complex.
parser.add_argument("-i", "--input", dest="file_path", metavar='FILE',
        help="Import the specified file")
parser.add_argument("-o", "--output", dest="save_path", metavar='FILE',
        help="Save the generated file to the specified path")

args = parser.parse_args(argv)  # In this example we wont use the args

if not argv:
    parser.print_help()
    return

# Run the conversion
clear_scene()
import_file(args.file_path)
save_file(args.save_path)

print("batch job finished, exiting")

if name == “main”:
main()

[/java]

1 Like