change "Toggle offset correction and manage related file renaming." behaviour so instead…
- User
change "Toggle offset correction and manage related file renaming." behaviour so instead of tecogan_no_offset.bat there would be 2 other files tecogan_cuda_no_offset.bat or tecogan_dml_no_offset.bat depending on what mode currently activated (DML in 1st case and CUDA in secode) alse make sure there would be no problem is I start script with Toggle offset correction and manage related file renaming." enabled. import tkinter as tk from tkinter import font, messagebox import configparser import os
class OffsetConfigEditor: def init(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor")
# Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.toggle_offset_correction ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.pack(side=tk.LEFT, padx=5) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) self.cuda_radio = tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode) self.cuda_radio.pack(side=tk.LEFT, padx=5) self.dml_radio = tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode) self.dml_radio.pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 4 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): self.config.set(section, key, value) self.write_config() def toggle_offset_correction(self): """Toggle offset correction and manage related file renaming.""" tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_offset_bat = os.path.join(self.batch_folder, "tecogan_offset.bat") tecogan_no_offset_bat = os.path.join(self.batch_folder, "tecogan_no_offset.bat") try: if self.run_offset_var.get(): if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_offset_bat) if os.path.exists(tecogan_no_offset_bat): os.rename(tecogan_no_offset_bat, tecogan_bat) # Disable mode switching self.cuda_radio.config(state=tk.DISABLED) self.dml_radio.config(state=tk.DISABLED) else: if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_no_offset_bat) if os.path.exists(tecogan_offset_bat): os.rename(tecogan_offset_bat, tecogan_bat) # Enable mode switching self.cuda_radio.config(state=tk.NORMAL) self.dml_radio.config(state=tk.NORMAL) # Save state to config self.save_run_offset_state() except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during toggling offset correction: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during toggling offset correction: {e}") def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 4 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 4 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 - Assistant
- User
write full code ready to use
- Assistant
- User
rewrite batch script at the end (after -----------------------------------): add taking offset_x and offset_y values from config_tg-.ini (offset_x_value and offset_y_value ) at same path from such section below and make loop for all %_out_dir% path (instead run once using last folder name from those path variable, find all folders that starting with "output" at the end of path): [Settings] run_offset_once=False offset_x_value=-3 offset_y_value=-3
if not "%offset_x%"=="0" ( ..\offset\offset.exe %_out_dir% %offset_x% %offset_y% ) else if not "%offset_y%"=="0" ( ..\offset\offset.exe %_out_dir% %offset_x% %offset_y% )
- Assistant
Certainly! Below is a rewritten batch script that extracts the
offset_x_valueandoffset_y_valuefrom theconfig_tg-.inifile, and loops over all directories that start with "output" in the %_out_dir% path to apply the offset usingoffset.exe. - User
change script to extract %_base_out_dir% from %_out_dir% (examle of path [it always have "ouput" word at the end] "I:\JavPlayer_202c_CUDA\TG\output0") value
- Assistant
To extract
%_base_out_dir%from%_out_dir%, where%_out_dir%is a path that always ends with a directory name starting with "output", we can use batch string manipulation. Here's how you can modify the script to achieve this: - User
no, set "_out_dir=I:\JavPlayer_202c_CUDA\TG\output0" here pase could be anything it's just example how it ends so make script universla for any path
- Assistant
- User
set "_out_dir=I:\JavPlayer_202c_CUDA\TG\output0" rem Example path, replace with actual variable delete line if not needed
- Assistant
Certainly! We can adjust the script to ensure it dynamically works with the provided
%_out_dir%environment variable without any hardcoded example path. This makes the script universal. Here's the modified script:@echo off rem ----------------------------------- rem Load Configuration from config_tg-.ini setlocal enabledelayedexpansion set "ini_file=..\TG\TGMAIN\config_tg-.ini" set "offset_x=0" set "offset_y=0" for /f "usebackq tokens=1,* delims==" %%A in (`findstr /r /c:"^offset_x_value=" /c:"^offset_y_value=" "%ini_file%"`) do ( set "name=%%A" set "value=%%B" set "!name!=!value!" ) rem Check if %_out_dir% is defined if not defined _out_dir ( echo _out_dir environment variable is not set. Exiting script. exit /b ) rem Extract base directory from _out_dir for %%A in ("%_out_dir%") do ( set "_base_out_dir=%%~dpA" ) rem Remove the trailing backslash set "_base_out_dir=%_base_out_dir:~0,-1%" rem Logic to run offset for all directories starting with 'output' in _base_out_dir for /d %%D in ("%_base_out_dir%\output*") do ( if not "%offset_x%"=="0" ( echo Running offset for directory "%%D" with offset_x=%offset_x% and offset_y=%offset_y% ..\offset\offset.exe "%%D" %offset_x% %offset_y% ) else if not "%offset_y%"=="0" ( echo Running offset for directory "%%D" with offset_x=%offset_x% and offset_y=%offset_y% ..\offset\offset.exe "%%D" %offset_x% %offset_y% ) ) endlocal