1414from ..utils import pfm
1515
1616# Add the TempfileManager class here
17+
1718class TempfileManager :
18- temp_files = {}
19-
20- @staticmethod
21- def track (object_id , file_path ):
22- # Track a temporary file for cleanup
23- if object_id not in TempfileManager .temp_files :
24- TempfileManager .temp_files [object_id ] = []
25- TempfileManager .temp_files [object_id ].append (file_path )
26-
27- @staticmethod
28- def delete_files (object_id ):
29- # Delete all tracked files for a given object
30- if object_id in TempfileManager .temp_files :
31- for file_path in TempfileManager .temp_files [object_id ]:
32- try :
33- os .remove (file_path )
34- except Exception as e :
35- print (f"[TFM] Error deleting file { file_path } : { e } " )
36- # Clean up the tracking list
37- del TempfileManager .temp_files [object_id ]
38-
39- @staticmethod
40- def cleanup ():
41- # Delete all tracked files for all objects
42- for object_id in list (TempfileManager .temp_files .keys ()):
43- TempfileManager .delete_files (object_id )
19+ _paths = {}
20+ _denoiser_process = None
4421
22+ @classmethod
23+ def track (cls , key , path ):
24+ if not key in cls ._paths :
25+ cls ._paths [key ] = {path }
26+ else :
27+ cls ._paths [key ].add (path )
28+
29+ @classmethod
30+ def delete_files (cls , key ):
31+ if not key in cls ._paths :
32+ return
33+ for path in cls ._paths [key ]:
34+ if os .path .exists (path ):
35+ os .remove (path )
36+ del cls ._paths [key ]
37+
38+ @classmethod
39+ def cleanup (cls ):
40+ # interrupt denoiser process inc ase it is still running
41+ if cls ._denoiser_process :
42+ cls ._denoiser_process .terminate ()
43+ # Need to convert dict_keys object to list, otherwise the loop throws:
44+ # "RuntimeError: dictionary changed size during iteration"
45+ keys = [_ for _ in cls ._paths .keys ()]
46+ for key in keys :
47+ cls .delete_files (key )
48+ cls ._paths .clear ()
4549
4650class FrameBuffer (object ):
4751 """ FrameBuffer used for viewport render """
@@ -67,7 +71,7 @@ def __init__(self, engine, context, scene):
6771
6872 # Denoiser initialization
6973 self ._initialize_denoiser_paths ()
70- self ._denoiser_process = None
74+ TempfileManager ._denoiser_process = None
7175 self .denoiser_result_cached = False
7276
7377 def _initialize_transparency (self , scene , context ):
@@ -191,27 +195,27 @@ def start_denoiser(self, luxcore_session):
191195 "-nrm" , self ._normal_file_path ,
192196 "-o" , self ._denoised_file_path ,
193197 ]
194- self ._denoiser_process = subprocess .Popen (
198+ TempfileManager ._denoiser_process = subprocess .Popen (
195199 args ,
196200 stdout = subprocess .PIPE ,
197201 stderr = subprocess .STDOUT ,
198202 text = True ,
199203 )
200204
201205 def is_denoiser_active (self ):
202- return self ._denoiser_process is not None
206+ return TempfileManager ._denoiser_process is not None
203207
204208 def is_denoiser_done (self ):
205- return self ._denoiser_process .poll () is not None
209+ return TempfileManager ._denoiser_process .poll () is not None
206210
207211 def load_denoiser_result (self , scene ):
208- with self ._denoiser_process .stdout as d_out :
212+ with TempfileManager ._denoiser_process .stdout as d_out :
209213 if d_out :
210214 print ("Denoiser output:" )
211215 for line in d_out :
212216 print (line , end = '' )
213- print ("Denoiser return code:" , self ._denoiser_process .returncode )
214- self ._denoiser_process = None
217+ print ("Denoiser return code:" , TempfileManager ._denoiser_process .returncode )
218+ TempfileManager ._denoiser_process = None
215219 shape = (self ._height * self ._width * 3 )
216220 try :
217221 with open (self ._denoised_file_path , "rb" ) as f :
@@ -233,11 +237,12 @@ def reset_denoiser(self):
233237 """ Denoiser was not started yet or the user has triggered an update """
234238 self .denoiser_result_cached = False
235239
236- if self ._denoiser_process :
240+ if TempfileManager ._denoiser_process :
237241 print ("Interrupting denoiser" )
238- self ._denoiser_process .terminate ()
239- print ("Denoiser outputs: " , self ._denoiser_process .stdout )
240- self ._denoiser_process = None
242+ TempfileManager ._denoiser_process .terminate ()
243+ print ("Denoiser outputs: " , TempfileManager ._denoiser_process .stdout )
244+ TempfileManager ._denoiser_process = None
245+ TempfileManager .delete_files (id (self ))
241246
242247 def update (self , luxcore_session , scene ):
243248 luxcore_session .GetFilm ().GetOutputFloat (self ._output_type , self .buffer )
0 commit comments