I have a large script where I define const sessionWidth = '5.1'
right at the top of the script. It's never redeclared anywhere in the script.
However I have had numerous issues with the sessionWidth
variable not making it into functions and forcing me to pass it on through every function and sub-function.
Are there situations where global variables don't behave global? I have an example below where sessionWidth
was 'undefined' , whereas a different global worked, but it has happened across multiple functions.
With the below script when I was debugging, I had the script log the value of sessionWidth when it was inside the function function createStemFXAuxes
,
i.e. log(`basePresetPath: ${basePresetPath}, sessionWidth: ${sessionWidth}`)
It's result was basePresetPath: Mix Templates, sessionWidth: undefined
Here's some of the script:
//Global variables at top of script
const sessionWidth = 'Stereo'; //'Stereo', '5.1' or '7.1'
const basePresetPath = 'Mix Templates'
function main(){
//////some other code here
log('Starting Stem FX setup...');
try {
processStemFX(stemNames, sessionWidth); //if sessionWidth not passed in, createStemFXAuxes fails
log('Stem FX setup completed.');
} catch (error) {
log(`Error during Stem FX setup: ${error}`);
throw error; // Abort the script
}
//code continues...
}
function processStemFX(stemNames) { //if sessionWidth not passed in, createStemFXAuxes fails
log(` At start of processStemFX the path is: basePresetPath: ${basePresetPath}, sessionWidth: ${sessionWidth}`);
// Select only the last Stem Aux track to prepare for FX Auxes
const lastStemAuxTrack = `${stemNames[stemNames.length - 1]} Aux`;
sf.ui.proTools.trackSelectByName({ names: [lastStemAuxTrack] });
// Log the session width before any calls
log(`sessionWidth before calling createStemFXAuxes: ${sessionWidth}`);
// Execute the sequence of FX setup steps
log(` Right before createStemFXAuxes the path is: basePresetPath: ${basePresetPath}, sessionWidth: ${sessionWidth}`);
createStemFXAuxes(stemNames, sessionWidth);
}
// Function to create and rename Stem FX Auxes using batch track rename functionality
function createStemFXAuxes(stemNames) {
// Construct the full path for the Track Preset
const presetPath = [basePresetPath, sessionWidth, `${sessionWidth} Default Stem FX`];
// Log the constructed path for verification
log(`Attempting to load Track Preset from path: ${presetPath.join(' > ')}`); // Logged: Attempting to load Track Preset from path: Mix Templates > > undefined Default Stem FX
log(`basePresetPath: ${basePresetPath}, sessionWidth: ${sessionWidth}`); // Logged: basePresetPath: Mix Templates, sessionWidth: undefined
// Attempt to create the track preset
try {
createTrackPreset(presetPath, stemNames.length.toString());
} catch (error) {
log(`Error loading Track Preset: ${error}`); // log result: Error loading Track Preset: Error: PopupMenuSelectAction
}
sf.ui.proTools.waitForNoModals();
sf.ui.proTools.invalidate();
//further scripting related to renaming the newly created tracks...
}
}
- Raphael Sepulveda @raphaelsepulveda2024-09-23 16:32:53.496Z
@Forrester_Savell, does this simplified version of the script log correctly on your end?
const sessionWidth = 'Stereo'; //'Stereo', '5.1' or '7.1' const basePresetPath = 'Mix Templates'; function processStemFX(stemNames) { createStemFXAuxes(stemNames); } function createStemFXAuxes(stemNames) { const presetPath = [basePresetPath, sessionWidth, `${sessionWidth} Default Stem FX`]; log(`Attempting to load Track Preset from path: ${presetPath.join(' > ')}`); log(`basePresetPath: ${basePresetPath}, sessionWidth: ${sessionWidth}`); } function main() { processStemFX(["DRUMS", "SYNTHS"]); } main();
- FForrester Savell @Forrester_Savell
That code logs correctly.
I went back and removed every argument that was referencing the global variables and the script worked. Clearly operator error 🤦♂️. I suspect it was because I sometimes was calling them into some functions but not others.
Thanks, as always, for your excellent help!!
Raphael Sepulveda @raphaelsepulveda2024-09-24 01:24:51.358Z
Ok great! Sometimes it's good to simplify an issue as possible and then work our way back from there.