diff --git a/bpy_checkout.py b/bpy_checkout.py index 95ef307..c4c04c5 100644 --- a/bpy_checkout.py +++ b/bpy_checkout.py @@ -15,7 +15,7 @@ def main(): sys.exit(4) action = None for i in bpy.data.actions: - if (i.name.upper() == action_name): + if (str(i.name.upper()) == str(action_name)): action = i break if (action == None): diff --git a/bpy_render.py b/bpy_render.py index 3402d9c..d2da0f8 100644 --- a/bpy_render.py +++ b/bpy_render.py @@ -41,10 +41,8 @@ def file_rename(file_full_path): except Exception: return "" -def render_video(video_output = ""): - getcwd = os.path.dirname(os.path.abspath(__file__)) - base_path = os.path.join(getcwd, video_output) - bpy.context.scene.render.filepath = base_path + "_" +def render_video(upload_dir, sign_name): + bpy.context.scene.render.filepath = os.path.join(upload_dir, sign_name + "_") try: bpy.ops.render.render(animation = True, write_still = False, layer = "", scene = "") return str("%s%0.4i-%0.4i.mp4" % (bpy.context.scene.render.filepath, bpy.context.scene.frame_start, bpy.context.scene.frame_end)) @@ -55,13 +53,13 @@ def main(): if (len(sys.argv) == 8): json_obj = json.loads(sys.argv[7]) try: - video_output = json_obj["video_output"] - convert_to_webm = json_obj["convert_to_webm"] + upload_dir = json_obj["upload_dir"] + sign_name = json_obj["sign_name"] except: sys.exit(3) try: configure_output() - file_rename(render_video(video_output)) + file_rename(render_video(upload_dir, sign_name)) except: sys.exit(4) else: diff --git a/checkout.py b/checkout.py index 0a2cd1e..5738932 100644 --- a/checkout.py +++ b/checkout.py @@ -3,10 +3,11 @@ import json import os import subprocess +import pyutil -getcwd = os.path.dirname(os.path.abspath(__file__)) -bpy_script_action = os.path.join(getcwd, "bpy_checkout.py") -bpy_script_render = os.path.join(getcwd, "bpy_render.py") +getcwd=os.path.dirname(os.path.abspath(__file__)) +bpy_script_action=os.path.join(getcwd, "bpy_checkout.py") +bpy_script_render=os.path.join(getcwd, "bpy_render.py") def file_exists(file_path): if ((os.path.isfile(file_path) == 1) and (os.path.exists(file_path) == 1)): @@ -14,58 +15,81 @@ def file_exists(file_path): else: return False -def check_action(blend_file = "", action_name = "", action_fake_is_valid = True, min_frame_count = 10, hide_output = True): +def check_action(blend_file="", action_name="", action_fake_is_valid=True, min_frame_count=10, hide_output=True): if (file_exists(blend_file)): - if not (isinstance(blend_file, str) and isinstance(action_name, str)): - return 1 - if not (isinstance(action_fake_is_valid, bool) and isinstance(min_frame_count, int)): + if not ((isinstance(blend_file, str) and isinstance(action_name, str)) or + (isinstance(blend_file, unicode) and isinstance(action_name, unicode)) or + ((isinstance(action_fake_is_valid, bool) and isinstance(min_frame_count, int)))): + pyutil.log("Args to check_action no match types", 4) return 1 if (action_name == ""): - action_name = os.path.splitext(blend_file)[0] + action_name=os.path.splitext(blend_file)[0] try: - json_object = json.JSONEncoder().encode( + json_object=json.JSONEncoder().encode( { - "action_name": action_name.upper(), + "action_name": action_name, "action_fake_is_valid": action_fake_is_valid, "min_frame_count": min_frame_count } ) if (hide_output): - dev_null = open(os.devnull, 'w') - return subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object], stdout = dev_null, stderr = dev_null) + dev_null=open(os.devnull, 'w') + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object], stdout=dev_null, stderr=dev_null) else: - return subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object]) + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object]) + if (result_code == 4): + pyutil.log("JSON Key no match", 4) + elif (result_code == 5): + pyutil.log("Action in blend file no exists", 4) + elif (result_code == 6): + pyutil.log("Action is fake user", 4) + elif (result_code == 7): + pyutil.log("Timeline Frame Count is less than 10", 4) + elif (result_code == 8): + pyutil.log("Args count no match", 4) except: + pyutil.log("Subprocess interrupt", 4) return 2 + return result_code else: + pyutil.log("Blend file not exists", 4) return 3 -def render_video(blend_file = "", video_output = "", convert_to_webm = True, rm_original_video = True, hide_output = False): +def render_video(upload_dir="", blend_file="", sign_name="", convert_to_webm=True, rm_original_video=True, hide_output=False): if (file_exists(blend_file)): - if not (isinstance(blend_file, str) and isinstance(video_output, str)): + if not ((isinstance(upload_dir, str) and isinstance(blend_file, str)) or + (isinstance(upload_dir, unicode) and isinstance(blend_file, unicode)) or + (isinstance(convert_to_webm, bool) and isinstance(rm_original_video, bool) and isinstance(hide_output, bool))): + pyutil.log("Args to render_video no match types", 4) return 1 try: - json_object = json.JSONEncoder().encode( + json_object= json.JSONEncoder().encode( { - "video_output": video_output.upper(), - "convert_to_webm": convert_to_webm + "upload_dir": upload_dir, + "sign_name": sign_name } ) if (hide_output): - dev_null = open(os.devnull, 'w') - result_code = subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object], stdout = dev_null, stderr = dev_null) + dev_null=open(os.devnull, 'w') + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object], stdout=dev_null, stderr=dev_null) else: - result_code = subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object]) + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object]) if (convert_to_webm): - video_mp4 = video_output.upper() + ".mp4" - video_webm = video_output.upper() + ".webm" + video_mp4=os.path.join(upload_dir, sign_name + ".mp4") + video_webm=os.path.join(upload_dir, sign_name + ".webm") subprocess.call(["avconv", "-loglevel", "0", "-y", "-i", video_mp4, "-r", "24", "-vcodec", "libvpx", video_webm]) if (rm_original_video): subprocess.call(["rm", video_mp4]) + if (result_code == 4): + pyutil.log("Except in process", 4) + elif(result_code == 5): + pyutil.log("Args count no match", 4) except: + pyutil.log("Subprocess interrupt", 4) return 2 return result_code else: + pyutil.log("JSON Key no match", 4) return 3 """ diff --git a/corretor.py b/corretor.py index 361d80f..e0f22e9 100644 --- a/corretor.py +++ b/corretor.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from flask import request, make_response from werkzeug import secure_filename import pbclient @@ -44,6 +45,24 @@ class Corretor: project.allow_anonymous_contributors = False pbclient.update_project(project) + def finish_task(self): + upload_session_id = request.form['upload_session_id'] + sign_name = request.form['sign_name'] + upload_dir = os.path.join(self.config['UPLOAD_FOLDER'], upload_session_id) + blend_file = os.path.join(upload_dir, sign_name + ".blend") + checkout_result = checkout.render_video(upload_dir, blend_file, sign_name) + pyutil.log("render_video code: " + str(checkout_result)) + code = 0 + result_msg = "" + if(checkout_result == 0): + result_msg = "Video gerado com sucesso." + code = 200 + else: + result_msg = "Erro durante geração de vídeo." + code = 400 + pyutil.log(result_msg) + return make_response(result_msg, code) + def create_project(self): app_short_name = self.config['PYBOSSA_APP_SHORT_NAME'] project = self.__find_project(app_short_name) @@ -91,12 +110,13 @@ class Corretor: file.save(uploaded_file) renamed_file = os.path.join(upload_dir, secure_filename(sign_name + ".blend")) os.rename(uploaded_file, renamed_file) - check_result = checkout.check_action(str(renamed_file), str(sign_name)) - if (check_result == 0): + checkout_result = checkout.check_action(renamed_file, sign_name) + pyutil.log("checkout_result code: " + str(checkout_result)) + if (checkout_result == 0): result_msg = "File " + filename + " was uploaded." code = 200 else: - result_msg = "File " + filename + " has not expected structure of blend file. " + str(check_result) + result_msg = "File " + filename + " has not expected structure of blend file. " + str(checkout_result) code = 400 pyutil.log(result_msg) return make_response(result_msg, code) diff --git a/main.py b/main.py index d740054..8a7b610 100644 --- a/main.py +++ b/main.py @@ -42,8 +42,11 @@ def upload_file(): @app.route("/finish_task", methods=["POST"]) def finish_task(): - # TODO read - request.data['upload_session_id'] e request.data['sign_name'] - return + try: + return controller.finish_task() + except: + pyutil.print_stack_trace() + raise def read_settings(app): here = os.path.abspath(__file__) @@ -60,7 +63,7 @@ def setup_controller(): def run(): setup_controller() - app.run(port=app.config['SERVER_PORT']) + app.run(host=app.config['SERVER_HOST'], port=app.config['SERVER_PORT']) if __name__ == '__main__': try: diff --git a/view/template.html b/view/template.html index 6b6c22e..4ea447d 100755 --- a/view/template.html +++ b/view/template.html @@ -146,8 +146,6 @@ $("#finish-button").removeClass("disabled-button"); $("#finish-button").addClass("enabled-button"); $("#finish-button").off("click").on("click", function() { - // endpoint - /finish_task - // enviar mensagem via POST para renderizar video - upload//.blend enableLoading(); $.post("{{ server }}/finish_task", { @@ -159,8 +157,7 @@ disableLoading(); status = status_dict[$("#finish-button").text().trim()]; saveAnswer(task, deferred, status); - } - ); + }); }); } -- libgit2 0.21.2