Skip to content

Commit b1207b1

Browse files
committed
Blender: Eliminate initial script copy, make preview timer single-shot
1 parent 8c3d9b2 commit b1207b1

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/windows/views/blender_listview.py

+23-28
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"""
2727

2828
import os
29-
import shutil
29+
import copy
3030
import subprocess
3131
import sys
3232
import re
@@ -331,8 +331,6 @@ def sliderPreview_valueChanged(self, new_value):
331331

332332
def preview_timer_onTimeout(self):
333333
"""Timer is ready to Render frame"""
334-
self.preview_timer.stop()
335-
336334
# Update preview label
337335
preview_frame_number = self.win.sliderPreview.value()
338336
log.info('Previewing frame %s' % preview_frame_number)
@@ -470,7 +468,7 @@ def error_with_blender(self, version=None, command_output=None):
470468
# Enable the Render button again
471469
self.enable_interface()
472470

473-
def inject_params(self, path, frame=None):
471+
def inject_params(self, source_path, out_path, frame=None):
474472
# determine if this is 'preview' mode?
475473
is_preview = False
476474
if frame:
@@ -480,7 +478,7 @@ def inject_params(self, path, frame=None):
480478

481479
# prepare string to inject
482480
user_params = "\n#BEGIN INJECTING PARAMS\n"
483-
param_data = self.params.items()
481+
param_data = copy.deepcopy(self.params)
484482
param_data.update(self.get_project_params(is_preview))
485483
for k, v in param_data.items():
486484
if isinstance(v, (int, float, list, bool)):
@@ -501,27 +499,29 @@ def inject_params(self, path, frame=None):
501499
if s.get("blender_gpu_enabled"):
502500
gpu_enable_py = os.path.join(info.PATH, "blender", "scripts", "gpu_enable.py")
503501
try:
504-
f = open(gpu_enable_py, 'r')
505-
gpu_code_body = f.read()
502+
with open(gpu_enable_py, 'r') as f:
503+
gpu_code_body = f.read()
504+
if gpu_code_body:
505+
log.info("Injecting GPU enable code from {}".format(gpu_enable_py))
506+
user_params += "\n#ENABLE GPU RENDERING\n"
507+
user_params += gpu_code_body
508+
user_params += "\n#END ENABLE GPU RENDERING\n"
506509
except IOError as e:
507-
log.error("Could not load GPU enable code! {}".format(e))
508-
509-
if gpu_code_body:
510-
log.info("Injecting GPU enable code from {}".format(gpu_enable_py))
511-
user_params += "\n#ENABLE GPU RENDERING\n"
512-
user_params += gpu_code_body
513-
user_params += "\n#END ENABLE GPU RENDERING\n"
510+
log.error("Could not load GPU enable code! %s", e)
514511

515-
# Open new temp .py file, and inject the user parameters
516-
with open(path, 'r') as f:
512+
# Read Python source from script file
513+
with open(source_path, 'r') as f:
517514
script_body = f.read()
518515

519-
# modify script variable
516+
# insert our modifications to script source
520517
script_body = script_body.replace("# INJECT_PARAMS_HERE", user_params)
521518

522-
# Write update script
523-
with open(path, "w", encoding="UTF-8", errors="strict") as f:
524-
f.write(script_body)
519+
# Write final script to output dir
520+
try:
521+
with open(out_path, "w", encoding="UTF-8", errors="strict") as f:
522+
f.write(script_body)
523+
except Exception:
524+
log.error("Could not write blender script to %s", out_path, exc_info=1)
525525

526526
@pyqtSlot(str)
527527
def update_image(self, image_path):
@@ -549,14 +549,8 @@ def Render(self, frame=None):
549549
target_script = os.path.join(info.BLENDER_PATH, self.unique_folder_name,
550550
self.selected_template.replace(".blend", ".py"))
551551

552-
# Copy the .py script associated with this template to the temp folder. This will allow
553-
# OpenShot to inject the user-entered params into the Python script.
554-
# XXX: Note that copyfile() is used instead of copy(), as the original
555-
# file may be readonly, and we don't want to duplicate those permissions
556-
shutil.copyfile(source_script, target_script)
557-
558-
# Open new temp .py file, and inject the user parameters
559-
self.inject_params(target_script, frame)
552+
# Read .py file, inject user parameters, and write to output path
553+
self.inject_params(source_script, target_script, frame)
560554

561555
# Create new thread to launch Blender (and read the output)
562556
QMetaObject.invokeMethod(
@@ -583,6 +577,7 @@ def __init__(self, parent, *args):
583577
# Preview render timer
584578
self.preview_timer = QTimer(self)
585579
self.preview_timer.setInterval(300)
580+
self.preview_timer.setSingleShot(True)
586581
self.preview_timer.timeout.connect(self.preview_timer_onTimeout)
587582

588583
# Init dictionary which holds the values to the template parameters

0 commit comments

Comments
 (0)