| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU Affero General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU Affero General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Affero General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | |
| 18 | import bpy, json, os |
| 19 | |
| 20 | |
| 21 | try: |
| 22 | CONFIG = json.loads(os.environ["RENDER_SETUP"]) |
| 23 | MODEL_EXT = CONFIG["model_ext"] |
| 24 | MODEL_PATH = CONFIG["model_path"] |
| 25 | CAMERA_COORD = CONFIG["camera_coord"] |
| 26 | CAMERA_FOCUS = CONFIG["camera_focus"] |
| 27 | CAMERA_CLIP = CONFIG["camera_clip"] |
| 28 | CAMERA_TYPE = CONFIG["projection"] |
| 29 | CAMERA_ORTHO = CONFIG["greatest"] * 1.5 |
| 30 | RENDER_WIDTH = CONFIG["width"] |
| 31 | RENDER_HEIGHT = CONFIG["height"] |
| 32 | RENDER_FILE = CONFIG["out_file"] |
| 33 | except KeyError: |
| 34 | print("Failed to load RENDER_SETUP environment variable.") |
| 35 | exit(1) |
| 36 | |
| 37 | |
| 38 | # add and setup camera |
| 39 | bpy.ops.object.camera_add(view_align=False, enter_editmode=False, |
| 40 | location = CAMERA_COORD) |
| 41 | camera_ob = bpy.data.objects[0] |
| 42 | camera = bpy.data.cameras[0] |
| 43 | camera.clip_end = CAMERA_CLIP |
| 44 | camera.ortho_scale = CAMERA_ORTHO |
| 45 | camera.type = CAMERA_TYPE |
| 46 | |
| 47 | |
| 48 | |
| 49 | # add an empty for focusing the camera |
| 50 | bpy.ops.object.add(location=CAMERA_FOCUS) |
| 51 | target = bpy.data.objects[1] |
| 52 | bpy.ops.object.select_all(action="SELECT") |
| 53 | bpy.ops.object.track_set(type="TRACKTO") |
| 54 | bpy.ops.object.select_all(action="DESELECT") |
| 55 | |
| 56 | |
| 57 | if MODEL_EXT == 'stl': |
| 58 | # import an stl model |
| 59 | bpy.ops.import_mesh.stl(filepath=MODEL_PATH) |
| 60 | |
| 61 | elif MODEL_EXT == 'obj': |
| 62 | # import an obj model |
| 63 | bpy.ops.import_scene.obj( |
| 64 | filepath=MODEL_PATH, |
| 65 | use_smooth_groups=False, |
| 66 | use_image_search=False, |
| 67 | axis_forward="Y", |
| 68 | axis_up="Z") |
| 69 | |
| 70 | |
| 71 | # rotate the imported objects with meshes in the scene |
| 72 | if CAMERA_TYPE == "PERSP": |
| 73 | for obj in bpy.data.objects[2:]: |
| 74 | obj.rotation_euler[2]=-.3 |
| 75 | |
| 76 | |
| 77 | # attempt to render |
| 78 | scene = bpy.data.scenes.values()[0] |
| 79 | scene.camera = camera_ob |
| 80 | scene.render.filepath = RENDER_FILE |
| 81 | scene.render.resolution_x = RENDER_WIDTH |
| 82 | scene.render.resolution_y = RENDER_HEIGHT |
| 83 | scene.render.resolution_percentage = 100 |
| 84 | bpy.ops.render.render(write_still=True) |