26
26
"""
27
27
28
28
import os
29
- import shutil
29
+ import copy
30
30
import subprocess
31
31
import sys
32
32
import re
@@ -331,8 +331,6 @@ def sliderPreview_valueChanged(self, new_value):
331
331
332
332
def preview_timer_onTimeout (self ):
333
333
"""Timer is ready to Render frame"""
334
- self .preview_timer .stop ()
335
-
336
334
# Update preview label
337
335
preview_frame_number = self .win .sliderPreview .value ()
338
336
log .info ('Previewing frame %s' % preview_frame_number )
@@ -470,7 +468,7 @@ def error_with_blender(self, version=None, command_output=None):
470
468
# Enable the Render button again
471
469
self .enable_interface ()
472
470
473
- def inject_params (self , path , frame = None ):
471
+ def inject_params (self , source_path , out_path , frame = None ):
474
472
# determine if this is 'preview' mode?
475
473
is_preview = False
476
474
if frame :
@@ -480,7 +478,7 @@ def inject_params(self, path, frame=None):
480
478
481
479
# prepare string to inject
482
480
user_params = "\n #BEGIN INJECTING PARAMS\n "
483
- param_data = self .params . items ( )
481
+ param_data = copy . deepcopy ( self .params )
484
482
param_data .update (self .get_project_params (is_preview ))
485
483
for k , v in param_data .items ():
486
484
if isinstance (v , (int , float , list , bool )):
@@ -501,27 +499,29 @@ def inject_params(self, path, frame=None):
501
499
if s .get ("blender_gpu_enabled" ):
502
500
gpu_enable_py = os .path .join (info .PATH , "blender" , "scripts" , "gpu_enable.py" )
503
501
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 "
506
509
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 )
514
511
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 :
517
514
script_body = f .read ()
518
515
519
- # modify script variable
516
+ # insert our modifications to script source
520
517
script_body = script_body .replace ("# INJECT_PARAMS_HERE" , user_params )
521
518
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 )
525
525
526
526
@pyqtSlot (str )
527
527
def update_image (self , image_path ):
@@ -549,14 +549,8 @@ def Render(self, frame=None):
549
549
target_script = os .path .join (info .BLENDER_PATH , self .unique_folder_name ,
550
550
self .selected_template .replace (".blend" , ".py" ))
551
551
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 )
560
554
561
555
# Create new thread to launch Blender (and read the output)
562
556
QMetaObject .invokeMethod (
@@ -583,6 +577,7 @@ def __init__(self, parent, *args):
583
577
# Preview render timer
584
578
self .preview_timer = QTimer (self )
585
579
self .preview_timer .setInterval (300 )
580
+ self .preview_timer .setSingleShot (True )
586
581
self .preview_timer .timeout .connect (self .preview_timer_onTimeout )
587
582
588
583
# Init dictionary which holds the values to the template parameters
0 commit comments