Commit e89d1e6b347fe31af0f81b9e802e767d508b7fc1
1 parent
9d2e6410
Exists in
master
Finaliza integração do módulo Checkout
Showing
6 changed files
with
83 additions
and
41 deletions
Show diff stats
bpy_checkout.py
bpy_render.py
... | ... | @@ -41,10 +41,8 @@ def file_rename(file_full_path): |
41 | 41 | except Exception: |
42 | 42 | return "" |
43 | 43 | |
44 | -def render_video(video_output = ""): | |
45 | - getcwd = os.path.dirname(os.path.abspath(__file__)) | |
46 | - base_path = os.path.join(getcwd, video_output) | |
47 | - bpy.context.scene.render.filepath = base_path + "_" | |
44 | +def render_video(upload_dir, sign_name): | |
45 | + bpy.context.scene.render.filepath = os.path.join(upload_dir, sign_name + "_") | |
48 | 46 | try: |
49 | 47 | bpy.ops.render.render(animation = True, write_still = False, layer = "", scene = "") |
50 | 48 | 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(): |
55 | 53 | if (len(sys.argv) == 8): |
56 | 54 | json_obj = json.loads(sys.argv[7]) |
57 | 55 | try: |
58 | - video_output = json_obj["video_output"] | |
59 | - convert_to_webm = json_obj["convert_to_webm"] | |
56 | + upload_dir = json_obj["upload_dir"] | |
57 | + sign_name = json_obj["sign_name"] | |
60 | 58 | except: |
61 | 59 | sys.exit(3) |
62 | 60 | try: |
63 | 61 | configure_output() |
64 | - file_rename(render_video(video_output)) | |
62 | + file_rename(render_video(upload_dir, sign_name)) | |
65 | 63 | except: |
66 | 64 | sys.exit(4) |
67 | 65 | else: | ... | ... |
checkout.py
... | ... | @@ -3,10 +3,11 @@ |
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | import subprocess |
6 | +import pyutil | |
6 | 7 | |
7 | -getcwd = os.path.dirname(os.path.abspath(__file__)) | |
8 | -bpy_script_action = os.path.join(getcwd, "bpy_checkout.py") | |
9 | -bpy_script_render = os.path.join(getcwd, "bpy_render.py") | |
8 | +getcwd=os.path.dirname(os.path.abspath(__file__)) | |
9 | +bpy_script_action=os.path.join(getcwd, "bpy_checkout.py") | |
10 | +bpy_script_render=os.path.join(getcwd, "bpy_render.py") | |
10 | 11 | |
11 | 12 | def file_exists(file_path): |
12 | 13 | if ((os.path.isfile(file_path) == 1) and (os.path.exists(file_path) == 1)): |
... | ... | @@ -14,58 +15,81 @@ def file_exists(file_path): |
14 | 15 | else: |
15 | 16 | return False |
16 | 17 | |
17 | -def check_action(blend_file = "", action_name = "", action_fake_is_valid = True, min_frame_count = 10, hide_output = True): | |
18 | +def check_action(blend_file="", action_name="", action_fake_is_valid=True, min_frame_count=10, hide_output=True): | |
18 | 19 | if (file_exists(blend_file)): |
19 | - if not (isinstance(blend_file, str) and isinstance(action_name, str)): | |
20 | - return 1 | |
21 | - if not (isinstance(action_fake_is_valid, bool) and isinstance(min_frame_count, int)): | |
20 | + if not ((isinstance(blend_file, str) and isinstance(action_name, str)) or | |
21 | + (isinstance(blend_file, unicode) and isinstance(action_name, unicode)) or | |
22 | + ((isinstance(action_fake_is_valid, bool) and isinstance(min_frame_count, int)))): | |
23 | + pyutil.log("Args to check_action no match types", 4) | |
22 | 24 | return 1 |
23 | 25 | if (action_name == ""): |
24 | - action_name = os.path.splitext(blend_file)[0] | |
26 | + action_name=os.path.splitext(blend_file)[0] | |
25 | 27 | try: |
26 | - json_object = json.JSONEncoder().encode( | |
28 | + json_object=json.JSONEncoder().encode( | |
27 | 29 | { |
28 | - "action_name": action_name.upper(), | |
30 | + "action_name": action_name, | |
29 | 31 | "action_fake_is_valid": action_fake_is_valid, |
30 | 32 | "min_frame_count": min_frame_count |
31 | 33 | } |
32 | 34 | ) |
33 | 35 | if (hide_output): |
34 | - dev_null = open(os.devnull, 'w') | |
35 | - return subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object], stdout = dev_null, stderr = dev_null) | |
36 | + dev_null=open(os.devnull, 'w') | |
37 | + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object], stdout=dev_null, stderr=dev_null) | |
36 | 38 | else: |
37 | - return subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object]) | |
39 | + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_action, "--", json_object]) | |
40 | + if (result_code == 4): | |
41 | + pyutil.log("JSON Key no match", 4) | |
42 | + elif (result_code == 5): | |
43 | + pyutil.log("Action in blend file no exists", 4) | |
44 | + elif (result_code == 6): | |
45 | + pyutil.log("Action is fake user", 4) | |
46 | + elif (result_code == 7): | |
47 | + pyutil.log("Timeline Frame Count is less than 10", 4) | |
48 | + elif (result_code == 8): | |
49 | + pyutil.log("Args count no match", 4) | |
38 | 50 | except: |
51 | + pyutil.log("Subprocess interrupt", 4) | |
39 | 52 | return 2 |
53 | + return result_code | |
40 | 54 | else: |
55 | + pyutil.log("Blend file not exists", 4) | |
41 | 56 | return 3 |
42 | 57 | |
43 | -def render_video(blend_file = "", video_output = "", convert_to_webm = True, rm_original_video = True, hide_output = False): | |
58 | +def render_video(upload_dir="", blend_file="", sign_name="", convert_to_webm=True, rm_original_video=True, hide_output=False): | |
44 | 59 | if (file_exists(blend_file)): |
45 | - if not (isinstance(blend_file, str) and isinstance(video_output, str)): | |
60 | + if not ((isinstance(upload_dir, str) and isinstance(blend_file, str)) or | |
61 | + (isinstance(upload_dir, unicode) and isinstance(blend_file, unicode)) or | |
62 | + (isinstance(convert_to_webm, bool) and isinstance(rm_original_video, bool) and isinstance(hide_output, bool))): | |
63 | + pyutil.log("Args to render_video no match types", 4) | |
46 | 64 | return 1 |
47 | 65 | try: |
48 | - json_object = json.JSONEncoder().encode( | |
66 | + json_object= json.JSONEncoder().encode( | |
49 | 67 | { |
50 | - "video_output": video_output.upper(), | |
51 | - "convert_to_webm": convert_to_webm | |
68 | + "upload_dir": upload_dir, | |
69 | + "sign_name": sign_name | |
52 | 70 | } |
53 | 71 | ) |
54 | 72 | if (hide_output): |
55 | - dev_null = open(os.devnull, 'w') | |
56 | - result_code = subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object], stdout = dev_null, stderr = dev_null) | |
73 | + dev_null=open(os.devnull, 'w') | |
74 | + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object], stdout=dev_null, stderr=dev_null) | |
57 | 75 | else: |
58 | - result_code = subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object]) | |
76 | + result_code=subprocess.call(['blender', '-b', blend_file, '-noaudio', '-P', bpy_script_render, "--", json_object]) | |
59 | 77 | if (convert_to_webm): |
60 | - video_mp4 = video_output.upper() + ".mp4" | |
61 | - video_webm = video_output.upper() + ".webm" | |
78 | + video_mp4=os.path.join(upload_dir, sign_name + ".mp4") | |
79 | + video_webm=os.path.join(upload_dir, sign_name + ".webm") | |
62 | 80 | subprocess.call(["avconv", "-loglevel", "0", "-y", "-i", video_mp4, "-r", "24", "-vcodec", "libvpx", video_webm]) |
63 | 81 | if (rm_original_video): |
64 | 82 | subprocess.call(["rm", video_mp4]) |
83 | + if (result_code == 4): | |
84 | + pyutil.log("Except in process", 4) | |
85 | + elif(result_code == 5): | |
86 | + pyutil.log("Args count no match", 4) | |
65 | 87 | except: |
88 | + pyutil.log("Subprocess interrupt", 4) | |
66 | 89 | return 2 |
67 | 90 | return result_code |
68 | 91 | else: |
92 | + pyutil.log("JSON Key no match", 4) | |
69 | 93 | return 3 |
70 | 94 | |
71 | 95 | """ | ... | ... |
corretor.py
1 | +# -*- coding: utf-8 -*- | |
1 | 2 | from flask import request, make_response |
2 | 3 | from werkzeug import secure_filename |
3 | 4 | import pbclient |
... | ... | @@ -44,6 +45,24 @@ class Corretor: |
44 | 45 | project.allow_anonymous_contributors = False |
45 | 46 | pbclient.update_project(project) |
46 | 47 | |
48 | + def finish_task(self): | |
49 | + upload_session_id = request.form['upload_session_id'] | |
50 | + sign_name = request.form['sign_name'] | |
51 | + upload_dir = os.path.join(self.config['UPLOAD_FOLDER'], upload_session_id) | |
52 | + blend_file = os.path.join(upload_dir, sign_name + ".blend") | |
53 | + checkout_result = checkout.render_video(upload_dir, blend_file, sign_name) | |
54 | + pyutil.log("render_video code: " + str(checkout_result)) | |
55 | + code = 0 | |
56 | + result_msg = "" | |
57 | + if(checkout_result == 0): | |
58 | + result_msg = "Video gerado com sucesso." | |
59 | + code = 200 | |
60 | + else: | |
61 | + result_msg = "Erro durante geração de vídeo." | |
62 | + code = 400 | |
63 | + pyutil.log(result_msg) | |
64 | + return make_response(result_msg, code) | |
65 | + | |
47 | 66 | def create_project(self): |
48 | 67 | app_short_name = self.config['PYBOSSA_APP_SHORT_NAME'] |
49 | 68 | project = self.__find_project(app_short_name) |
... | ... | @@ -91,12 +110,13 @@ class Corretor: |
91 | 110 | file.save(uploaded_file) |
92 | 111 | renamed_file = os.path.join(upload_dir, secure_filename(sign_name + ".blend")) |
93 | 112 | os.rename(uploaded_file, renamed_file) |
94 | - check_result = checkout.check_action(str(renamed_file), str(sign_name)) | |
95 | - if (check_result == 0): | |
113 | + checkout_result = checkout.check_action(renamed_file, sign_name) | |
114 | + pyutil.log("checkout_result code: " + str(checkout_result)) | |
115 | + if (checkout_result == 0): | |
96 | 116 | result_msg = "File " + filename + " was uploaded." |
97 | 117 | code = 200 |
98 | 118 | else: |
99 | - result_msg = "File " + filename + " has not expected structure of blend file. " + str(check_result) | |
119 | + result_msg = "File " + filename + " has not expected structure of blend file. " + str(checkout_result) | |
100 | 120 | code = 400 |
101 | 121 | pyutil.log(result_msg) |
102 | 122 | return make_response(result_msg, code) | ... | ... |
main.py
... | ... | @@ -42,8 +42,11 @@ def upload_file(): |
42 | 42 | |
43 | 43 | @app.route("/finish_task", methods=["POST"]) |
44 | 44 | def finish_task(): |
45 | - # TODO read - request.data['upload_session_id'] e request.data['sign_name'] | |
46 | - return | |
45 | + try: | |
46 | + return controller.finish_task() | |
47 | + except: | |
48 | + pyutil.print_stack_trace() | |
49 | + raise | |
47 | 50 | |
48 | 51 | def read_settings(app): |
49 | 52 | here = os.path.abspath(__file__) |
... | ... | @@ -60,7 +63,7 @@ def setup_controller(): |
60 | 63 | |
61 | 64 | def run(): |
62 | 65 | setup_controller() |
63 | - app.run(port=app.config['SERVER_PORT']) | |
66 | + app.run(host=app.config['SERVER_HOST'], port=app.config['SERVER_PORT']) | |
64 | 67 | |
65 | 68 | if __name__ == '__main__': |
66 | 69 | try: | ... | ... |
view/template.html
... | ... | @@ -146,8 +146,6 @@ |
146 | 146 | $("#finish-button").removeClass("disabled-button"); |
147 | 147 | $("#finish-button").addClass("enabled-button"); |
148 | 148 | $("#finish-button").off("click").on("click", function() { |
149 | - // endpoint - /finish_task | |
150 | - // enviar mensagem via POST para renderizar video - upload/<session_hash>/<task.info.sign_name>.blend | |
151 | 149 | enableLoading(); |
152 | 150 | $.post("{{ server }}/finish_task", |
153 | 151 | { |
... | ... | @@ -159,8 +157,7 @@ |
159 | 157 | disableLoading(); |
160 | 158 | status = status_dict[$("#finish-button").text().trim()]; |
161 | 159 | saveAnswer(task, deferred, status); |
162 | - } | |
163 | - ); | |
160 | + }); | |
164 | 161 | }); |
165 | 162 | } |
166 | 163 | ... | ... |