~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/templates/background_job.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This script is an example of how you can run blender from the command line
2
 
# (in background mode with no interface) to automate tasks, in this example it
3
 
# creates a text object, camera and light, then renders and/or saves it.
4
 
# This example also shows how you can parse command line options to scripts.
5
 
#
6
 
# Example usage for this test.
7
 
#  blender --background --factory-startup --python $HOME/background_job.py -- \
8
 
#          --text="Hello World" \
9
 
#          --render="/tmp/hello" \
10
 
#          --save="/tmp/hello.blend"
11
 
#
12
 
# Notice:
13
 
# '--factory-startup' is used to avoid the user default settings from
14
 
#                     interfearing with automated scene generation.
15
 
#
16
 
# '--' causes blender to ignore all following arguments so python can use them.
17
 
#
18
 
# See blender --help for details.
19
 
 
20
 
import bpy
21
 
 
22
 
 
23
 
def example_function(text, save_path, render_path):
24
 
 
25
 
    scene = bpy.context.scene
26
 
 
27
 
    # Clear existing objects.
28
 
    scene.camera = None
29
 
    for obj in scene.objects:
30
 
        scene.objects.unlink(obj)
31
 
 
32
 
    txt_data = bpy.data.curves.new(name="MyText", type='FONT')
33
 
 
34
 
    # Text Object
35
 
    txt_ob = bpy.data.objects.new(name="MyText", object_data=txt_data)
36
 
    scene.objects.link(txt_ob)  # add the data to the scene as an object
37
 
    txt_data.body = text        # the body text to the command line arg given
38
 
    txt_data.align = 'CENTER'   # center text
39
 
 
40
 
    # Camera
41
 
    cam_data = bpy.data.cameras.new("MyCam")
42
 
    cam_ob = bpy.data.objects.new(name="MyCam", object_data=cam_data)
43
 
    scene.objects.link(cam_ob)  # instance the camera object in the scene
44
 
    scene.camera = cam_ob       # set the active camera
45
 
    cam_ob.location = 0.0, 0.0, 10.0
46
 
 
47
 
    # Lamp
48
 
    lamp_data = bpy.data.lamps.new("MyLamp", 'POINT')
49
 
    lamp_ob = bpy.data.objects.new(name="MyCam", object_data=lamp_data)
50
 
    scene.objects.link(lamp_ob)
51
 
    lamp_ob.location = 2.0, 2.0, 5.0
52
 
 
53
 
    if save_path:
54
 
        try:
55
 
            f = open(save_path, 'w')
56
 
            f.close()
57
 
            ok = True
58
 
        except:
59
 
            print("Cannot save to path %r" % save_path)
60
 
 
61
 
            import traceback
62
 
            traceback.print_exc()
63
 
 
64
 
        if ok:
65
 
            bpy.ops.wm.save_as_mainfile(filepath=save_path)
66
 
 
67
 
    if render_path:
68
 
        render = scene.render
69
 
        render.use_file_extension = True
70
 
        render.filepath = render_path
71
 
        bpy.ops.render.render(write_still=True)
72
 
 
73
 
 
74
 
def main():
75
 
    import sys       # to get command line args
76
 
    import argparse  # to parse options for us and print a nice help message
77
 
 
78
 
    # get the args passed to blender after "--", all of which are ignored by
79
 
    # blender so scripts may receive their own arguments
80
 
    argv = sys.argv
81
 
 
82
 
    if "--" not in argv:
83
 
        argv = []  # as if no args are passed
84
 
    else:
85
 
        argv = argv[argv.index("--") + 1:]  # get all args after "--"
86
 
 
87
 
    # When --help or no args are given, print this help
88
 
    usage_text = \
89
 
    "Run blender in background mode with this script:"
90
 
    "  blender --background --python " + __file__ + " -- [options]"
91
 
 
92
 
    parser = argparse.ArgumentParser(description=usage_text)
93
 
 
94
 
    # Example utility, add some text and renders or saves it (with options)
95
 
    # Possible types are: string, int, long, choice, float and complex.
96
 
    parser.add_argument("-t", "--text", dest="text", type=str, required=True,
97
 
            help="This text will be used to render an image")
98
 
 
99
 
    parser.add_argument("-s", "--save", dest="save_path", metavar='FILE',
100
 
            help="Save the generated file to the specified path")
101
 
    parser.add_argument("-r", "--render", dest="render_path", metavar='FILE',
102
 
            help="Render an image to the specified path")
103
 
 
104
 
    args = parser.parse_args(argv)  # In this example we wont use the args
105
 
 
106
 
    if not argv:
107
 
        parser.print_help()
108
 
        return
109
 
 
110
 
    if not args.text:
111
 
        print("Error: --text=\"some string\" argument not given, aborting.")
112
 
        parser.print_help()
113
 
        return
114
 
 
115
 
    # Run the example function
116
 
    example_function(args.text, args.save_path, args.render_path)
117
 
 
118
 
    print("batch job finished, exiting")
119
 
 
120
 
 
121
 
if __name__ == "__main__":
122
 
    main()