Graph Editor MEL Script can't run when executed whole, only line by line


I’m trying to make a version of Maya’s “Graph Editor Frame Playback Range” command, that will also filter out from view any anim curves that aren’t in highlighted Animation Layers.

It’s been a pet peeve for a while when animating, that each time you select an object with anim layers, the Graph Editor gets cluttered with hidden and locked curves.

I have this so far:

// create array of selected anim layers
string $layers[] = getSelectedAnimLayer("AnimLayerTab");
// view only connections present in select anim layers
selectionConnection -e -select $layers graphEditor1FromOutliner;

// Maya's default command for Graph Editor Frame Playback Range
animView -startTime (`playbackOptions -query -minTime` - 1) -endTime (`playbackOptions -query -maxTime` + 1) graphEditor1GraphEd;

This works in Maya 2020, but not in Maya 2022, where it instead frames the entire animation range.
In 2022 it can work only if you run each line individually, and I’m failing to understand why.

If anyone could explain why and how to fix this, it’d be much appreciated.