View difference between Paste ID: i0bFgwJj and Q9dLtWEu
SHOW: | | - or go back to the newest paste.
1
# This script invokes blender to import and save external model formats as
2
# .blend files to be processed further.
3
#
4
# Example usage for this importer:
5
#  blender --background --factory-startup --python $HOME/import_3ds.py -- \
6
#          --i="/tmp/hello.3ds" \
7
#          --o="/tmp/hello.blend" \
8
#
9
# See blender --help for details.
10
11
import bpy
12
13
# Imports a file using importer
14
def import_file(file_path):
15
    # Import the model
16
    bpy.ops.wm.collada_import(filepath = file_path)
17
18
# Clear existing objects.
19
def clear_scene():
20
    scene = bpy.context.scene
21
    scene.camera = None
22
    for obj in scene.objects:
23
        scene.objects.unlink(obj)
24
25
# Save current scene as .blend file
26
def save_file(save_path):
27
    # Check if output file exists already
28
    try:
29
        f = open(save_path, 'w')
30
        f.close()
31
        ok = True
32
    except:
33
        print("Cannot save to path %r" % save_path)
34
35
        import traceback
36
        traceback.print_exc()
37
38
    # Save .blend file
39
    if ok:
40
        bpy.ops.wm.save_as_mainfile(filepath=save_path)
41
42
# DIFFERENT STUFF
43
def open_save_file(open_path):
44-
     bpy.ops.wm.open_as_mainfile(filepath=save_path)
44+
     bpy.ops.wm.open_mainfile(filepath=save_path)
45
     bpy.ops.wm.save_as_mainfile(filepath=save_path)
46
47
# END DIFFERENT STUFF
48
49
def main():
50
    import sys       # to get command line args
51
    import argparse  # to parse options for us and print a nice help message
52
53
    # get the args passed to blender after "--", all of which are ignored by
54
    # blender so scripts may receive their own arguments
55
    argv = sys.argv
56
57
    if "--" not in argv:
58
        argv = []  # as if no args are passed
59
    else:
60
        argv = argv[argv.index("--") + 1:]  # get all args after "--"
61
62
    # When --help or no args are given, print this help
63
    usage_text = \
64
    "Run blender in background mode with this script:"
65
    "  blender --background --factory-startup --python " + __file__ + " -- [options]"
66
67
    parser = argparse.ArgumentParser(description=usage_text)
68
69
    # Possible types are: string, int, long, choice, float and complex.
70
    parser.add_argument("-i", "--input", dest="file_path", metavar='FILE',
71
            help="Import the specified file")
72
    parser.add_argument("-o", "--output", dest="save_path", metavar='FILE',
73
            help="Save the generated file to the specified path")
74
75
    args = parser.parse_args(argv)  # In this example we wont use the args
76
77
    if not argv:
78
        parser.print_help()
79
        return
80
81
    # Run the conversion
82
    clear_scene()
83
    import_file(args.file_path)
84
    save_file(args.save_path)
85
    open_save_file(args.save_path)
86
87
    print("batch job finished, exiting")
88
89
90
if __name__ == "__main__":
91
    main()