background_job.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # interfering 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. import bpy
  20. def example_function(text, save_path, render_path):
  21. # Clear existing objects.
  22. bpy.ops.wm.read_factory_settings(use_empty=True)
  23. scene = bpy.context.scene
  24. txt_data = bpy.data.curves.new(name="MyText", type='FONT')
  25. # Text Object
  26. txt_ob = bpy.data.objects.new(name="MyText", object_data=txt_data)
  27. scene.collection.objects.link(txt_ob) # add the data to the scene as an object
  28. txt_data.body = text # the body text to the command line arg given
  29. txt_data.align_x = 'CENTER' # center text
  30. # Camera
  31. cam_data = bpy.data.cameras.new("MyCam")
  32. cam_ob = bpy.data.objects.new(name="MyCam", object_data=cam_data)
  33. scene.collection.objects.link(cam_ob) # instance the camera object in the scene
  34. scene.camera = cam_ob # set the active camera
  35. cam_ob.location = 0.0, 0.0, 10.0
  36. # Light
  37. light_data = bpy.data.lights.new("MyLight", 'POINT')
  38. light_ob = bpy.data.objects.new(name="MyLight", object_data=light_data)
  39. scene.collection.objects.link(light_ob)
  40. light_ob.location = 2.0, 2.0, 5.0
  41. bpy.context.view_layer.update()
  42. if save_path:
  43. bpy.ops.wm.save_as_mainfile(filepath=save_path)
  44. if render_path:
  45. render = scene.render
  46. render.use_file_extension = True
  47. render.filepath = render_path
  48. bpy.ops.render.render(write_still=True)
  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. # get the args passed to blender after "--", all of which are ignored by
  53. # blender so scripts may receive their own arguments
  54. argv = sys.argv
  55. if "--" not in argv:
  56. argv = [] # as if no args are passed
  57. else:
  58. argv = argv[argv.index("--") + 1:] # get all args after "--"
  59. # When --help or no args are given, print this help
  60. usage_text = (
  61. "Run blender in background mode with this script:"
  62. " blender --background --python " + __file__ + " -- [options]"
  63. )
  64. parser = argparse.ArgumentParser(description=usage_text)
  65. # Example utility, add some text and renders or saves it (with options)
  66. # Possible types are: string, int, long, choice, float and complex.
  67. parser.add_argument(
  68. "-t", "--text", dest="text", type=str, required=True,
  69. help="This text will be used to render an image",
  70. )
  71. parser.add_argument(
  72. "-s", "--save", dest="save_path", metavar='FILE',
  73. help="Save the generated file to the specified path",
  74. )
  75. parser.add_argument(
  76. "-r", "--render", dest="render_path", metavar='FILE',
  77. help="Render an image to the specified path",
  78. )
  79. args = parser.parse_args(argv) # In this example we won't use the args
  80. if not argv:
  81. parser.print_help()
  82. return
  83. if not args.text:
  84. print("Error: --text=\"some string\" argument not given, aborting.")
  85. parser.print_help()
  86. return
  87. # Run the example function
  88. example_function(args.text, args.save_path, args.render_path)
  89. print("batch job finished, exiting")
  90. if __name__ == "__main__":
  91. main()