Moving blender render files to be accessed via pkg_resources

Previously the .blend and .py files had to be in the same directory
mediagoblin/celery launched from.  This is now fixed so it pulls them
out of the package proper.
This commit is contained in:
Christopher Allan Webber
2012-12-01 20:16:15 -06:00
parent 8bef424f24
commit 39c340f280
3 changed files with 28 additions and 11 deletions

View File

@@ -0,0 +1,84 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import bpy, json, os
try:
CONFIG = json.loads(os.environ["RENDER_SETUP"])
MODEL_EXT = CONFIG["model_ext"]
MODEL_PATH = CONFIG["model_path"]
CAMERA_COORD = CONFIG["camera_coord"]
CAMERA_FOCUS = CONFIG["camera_focus"]
CAMERA_CLIP = CONFIG["camera_clip"]
CAMERA_TYPE = CONFIG["projection"]
CAMERA_ORTHO = CONFIG["greatest"] * 1.5
RENDER_WIDTH = CONFIG["width"]
RENDER_HEIGHT = CONFIG["height"]
RENDER_FILE = CONFIG["out_file"]
except KeyError:
print("Failed to load RENDER_SETUP environment variable.")
exit(1)
# add and setup camera
bpy.ops.object.camera_add(view_align=False, enter_editmode=False,
location = CAMERA_COORD)
camera_ob = bpy.data.objects[0]
camera = bpy.data.cameras[0]
camera.clip_end = CAMERA_CLIP
camera.ortho_scale = CAMERA_ORTHO
camera.type = CAMERA_TYPE
# add an empty for focusing the camera
bpy.ops.object.add(location=CAMERA_FOCUS)
target = bpy.data.objects[1]
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.track_set(type="TRACKTO")
bpy.ops.object.select_all(action="DESELECT")
if MODEL_EXT == 'stl':
# import an stl model
bpy.ops.import_mesh.stl(filepath=MODEL_PATH)
elif MODEL_EXT == 'obj':
# import an obj model
bpy.ops.import_scene.obj(
filepath=MODEL_PATH,
use_smooth_groups=False,
use_image_search=False,
axis_forward="Y",
axis_up="Z")
# rotate the imported objects with meshes in the scene
if CAMERA_TYPE == "PERSP":
for obj in bpy.data.objects[2:]:
obj.rotation_euler[2]=-.3
# attempt to render
scene = bpy.data.scenes.values()[0]
scene.camera = camera_ob
scene.render.filepath = RENDER_FILE
scene.render.resolution_x = RENDER_WIDTH
scene.render.resolution_y = RENDER_HEIGHT
scene.render.resolution_percentage = 100
bpy.ops.render.render(write_still=True)

View File

@@ -18,6 +18,7 @@ import os
import json
import logging
import subprocess
import pkg_resources
from mediagoblin import mg_globals as mgg
from mediagoblin.processing import create_pub_filepath, \
@@ -29,6 +30,17 @@ from mediagoblin.media_types.stl import model_loader
_log = logging.getLogger(__name__)
SUPPORTED_FILETYPES = ['stl', 'obj']
BLEND_FILE = pkg_resources.resource_filename(
'mediagoblin.media_types.stl',
os.path.join(
'assets',
'blender_render.blend'))
BLEND_SCRIPT = pkg_resources.resource_filename(
'mediagoblin.media_types.stl',
os.path.join(
'assets',
'blender_render.py'))
def sniff_handler(media_file, **kw):
if kw.get('media') is not None:
@@ -55,7 +67,12 @@ def blender_render(config):
arg_string = "blender -b blender_render.blend -F "
arg_string +="JPEG -P blender_render.py"
env = {"RENDER_SETUP" : json.dumps(config), "DISPLAY":":0"}
subprocess.call(arg_string.split(" "), env=env)
subprocess.call(
["blender",
"-b", BLEND_FILE,
"-F", "JPEG",
"-P", BLEND_SCRIPT],
env=env)
def process_stl(entry):
@@ -93,16 +110,16 @@ def process_stl(entry):
path = create_pub_filepath(entry, name_builder.fill(name))
render_file = mgg.public_store.get_file(path, "wb")
shot = {
"model_path" : queued_filename,
"model_ext" : ext,
"camera_coord" : camera,
"camera_focus" : model.average,
"camera_clip" : greatest*10,
"greatest" : greatest,
"projection" : project,
"width" : width,
"height" : height,
"out_file" : render_file.name,
"model_path": queued_filename,
"model_ext": ext,
"camera_coord": camera,
"camera_focus": model.average,
"camera_clip": greatest*10,
"greatest": greatest,
"projection": project,
"width": width,
"height": height,
"out_file": render_file.name,
}
render_file.close()
blender_render(shot)