2011-04-10 15:24:05 +00:00
|
|
|
# This script is an example of how you can run blender from the command line
|
|
|
|
# (in background mode with no interface) to automate tasks, in this example it
|
|
|
|
# creates a text object, camera and light, then renders and/or saves it.
|
|
|
|
# This example also shows how you can parse command line options to scripts.
|
2011-01-26 07:54:27 +00:00
|
|
|
#
|
2010-12-11 11:14:30 +00:00
|
|
|
# Example usage for this test.
|
2011-04-10 15:24:05 +00:00
|
|
|
# blender --background --factory-startup --python $HOME/background_job.py -- \
|
|
|
|
# --text="Hello World" \
|
|
|
|
# --render="/tmp/hello" \
|
|
|
|
# --save="/tmp/hello.blend"
|
2011-01-26 07:54:27 +00:00
|
|
|
#
|
|
|
|
# Notice:
|
2011-04-10 15:24:05 +00:00
|
|
|
# '--factory-startup' is used to avoid the user default settings from
|
|
|
|
# interfearing with automated scene generation.
|
|
|
|
#
|
2011-01-26 07:54:27 +00:00
|
|
|
# '--' causes blender to ignore all following arguments so python can use them.
|
|
|
|
#
|
|
|
|
# See blender --help for details.
|
2010-12-11 11:14:30 +00:00
|
|
|
|
|
|
|
import bpy
|
|
|
|
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2011-04-10 15:24:05 +00:00
|
|
|
def example_function(text, save_path, render_path):
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2010-12-11 11:14:30 +00:00
|
|
|
scene = bpy.context.scene
|
|
|
|
|
|
|
|
# Clear existing objects.
|
|
|
|
scene.camera = None
|
|
|
|
for obj in scene.objects:
|
|
|
|
scene.objects.unlink(obj)
|
|
|
|
|
2010-12-16 05:02:15 +00:00
|
|
|
txt_data = bpy.data.curves.new(name="MyText", type='FONT')
|
2010-12-11 11:14:30 +00:00
|
|
|
|
|
|
|
# Text Object
|
|
|
|
txt_ob = bpy.data.objects.new(name="MyText", object_data=txt_data)
|
2011-04-10 15:24:05 +00:00
|
|
|
scene.objects.link(txt_ob) # add the data to the scene as an object
|
|
|
|
txt_data.body = text # the body text to the command line arg given
|
|
|
|
txt_data.align = 'CENTER' # center text
|
2010-12-11 11:14:30 +00:00
|
|
|
|
|
|
|
# Camera
|
2011-04-10 15:24:05 +00:00
|
|
|
cam_data = bpy.data.cameras.new("MyCam")
|
2010-12-11 11:14:30 +00:00
|
|
|
cam_ob = bpy.data.objects.new(name="MyCam", object_data=cam_data)
|
2011-04-10 15:24:05 +00:00
|
|
|
scene.objects.link(cam_ob) # instance the camera object in the scene
|
|
|
|
scene.camera = cam_ob # set the active camera
|
2011-01-26 07:54:27 +00:00
|
|
|
cam_ob.location = 0.0, 0.0, 10.0
|
2010-12-11 11:14:30 +00:00
|
|
|
|
|
|
|
# Lamp
|
2011-01-11 02:30:01 +00:00
|
|
|
lamp_data = bpy.data.lamps.new("MyLamp", 'POINT')
|
2010-12-11 11:14:30 +00:00
|
|
|
lamp_ob = bpy.data.objects.new(name="MyCam", object_data=lamp_data)
|
|
|
|
scene.objects.link(lamp_ob)
|
|
|
|
lamp_ob.location = 2.0, 2.0, 5.0
|
|
|
|
|
|
|
|
if save_path:
|
|
|
|
try:
|
|
|
|
f = open(save_path, 'w')
|
|
|
|
f.close()
|
|
|
|
ok = True
|
|
|
|
except:
|
|
|
|
print("Cannot save to path %r" % save_path)
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
if ok:
|
|
|
|
bpy.ops.wm.save_as_mainfile(filepath=save_path)
|
|
|
|
|
|
|
|
if render_path:
|
|
|
|
render = scene.render
|
|
|
|
render.use_file_extension = True
|
|
|
|
render.filepath = render_path
|
|
|
|
bpy.ops.render.render(write_still=True)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2011-04-10 15:24:05 +00:00
|
|
|
import sys # to get command line args
|
|
|
|
import argparse # to parse options for us and print a nice help message
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2011-04-10 15:24:05 +00:00
|
|
|
# get the args passed to blender after "--", all of which are ignored by
|
|
|
|
# blender so scripts may receive their own arguments
|
2010-12-11 11:14:30 +00:00
|
|
|
argv = sys.argv
|
|
|
|
|
|
|
|
if "--" not in argv:
|
2011-01-26 07:54:27 +00:00
|
|
|
argv = [] # as if no args are passed
|
|
|
|
else:
|
|
|
|
argv = argv[argv.index("--") + 1:] # get all args after "--"
|
|
|
|
|
2010-12-11 11:14:30 +00:00
|
|
|
# When --help or no args are given, print this help
|
2011-04-10 15:24:05 +00:00
|
|
|
usage_text = \
|
|
|
|
"Run blender in background mode with this script:"
|
|
|
|
" blender --background --python " + __file__ + " -- [options]"
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2011-03-26 03:42:59 +00:00
|
|
|
parser = argparse.ArgumentParser(description=usage_text)
|
2010-12-11 11:14:30 +00:00
|
|
|
|
2011-04-10 15:24:05 +00:00
|
|
|
# Example utility, add some text and renders or saves it (with options)
|
2010-12-11 11:14:30 +00:00
|
|
|
# Possible types are: string, int, long, choice, float and complex.
|
2011-04-10 15:24:05 +00:00
|
|
|
parser.add_argument("-t", "--text", dest="text", type=str, required=True,
|
|
|
|
help="This text will be used to render an image")
|
2010-12-11 11:14:30 +00:00
|
|
|
|
2011-04-10 15:24:05 +00:00
|
|
|
parser.add_argument("-s", "--save", dest="save_path", metavar='FILE',
|
|
|
|
help="Save the generated file to the specified path")
|
|
|
|
parser.add_argument("-r", "--render", dest="render_path", metavar='FILE',
|
|
|
|
help="Render an image to the specified path")
|
2010-12-11 11:14:30 +00:00
|
|
|
|
2011-04-10 15:24:05 +00:00
|
|
|
args = parser.parse_args(argv) # In this example we wont use the args
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2010-12-11 11:14:30 +00:00
|
|
|
if not argv:
|
|
|
|
parser.print_help()
|
|
|
|
return
|
|
|
|
|
2011-04-10 15:24:05 +00:00
|
|
|
if not args.text:
|
2010-12-11 11:14:30 +00:00
|
|
|
print("Error: --text=\"some string\" argument not given, aborting.")
|
|
|
|
parser.print_help()
|
|
|
|
return
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2010-12-11 11:14:30 +00:00
|
|
|
# Run the example function
|
2011-04-10 15:24:05 +00:00
|
|
|
example_function(args.text, args.save_path, args.render_path)
|
2010-12-11 11:14:30 +00:00
|
|
|
|
|
|
|
print("batch job finished, exiting")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|