Actually, it's not the problem of the code being in usersetup py, because this file only gets read when maya gets started.
The problem is, that I had to bind the scriptjob to the event "readingFile" instead of the closure event of "FBXIMPORTWINDOW" since otherwise it wasn't triggered when referencing FBX. the "readingFile" event gets triggered whenever a file is read (opening a scene, creating a new scene, importing a file, referencing a file). So basically the scriptjob doesn't know the difference between importing/referencing a file and opening one, it also doesn't know the filetype of the file that is read, which is why I originally bound the scriptjob to the FBX window.
I tried to fix this problem by adding a scriptjob that deletes the rangevariables from prefs when the scene gets flushed Something that happens when a new Scene is created or an existing scene is opened, but not when a scene is referenced/imported. I also modified the function that resets the timeline to first check if those variables exist and just create them when they don't.
import maya.cmds as mc
def rmVar():
mc.optionVar(rm = "startPlaybackRangeStorage")
mc.optionVar(rm = "endPlaybackRangeStorage")
mc.optionVar(rm = "startPlaybackTimeStorage")
mc.optionVar(rm = "endPlaybackTimeStorage")
def storePlayBackRange():
start = mc.playbackOptions(q=True, min=True)
end = mc.playbackOptions(q=True, max=True)
startT = mc.playbackOptions(q=True, ast=True)
endT = mc.playbackOptions(q=True, aet=True)
mc.optionVar(fv = ["startPlaybackRangeStorage", start])
mc.optionVar(fv = ["endPlaybackRangeStorage", end])
mc.optionVar(fv = ["startPlaybackTimeStorage", startT])
mc.optionVar(fv = ["endPlaybackTimeStorage", endT])
def resetPBS():
if mc.optionVar(ex = "startPlaybackRangeStorage")==True:
s = mc.optionVar(q="startPlaybackRangeStorage")
e = mc.optionVar(q="endPlaybackRangeStorage")
sT = mc.optionVar(q="startPlaybackTimeStorage")
eT = mc.optionVar(q="endPlaybackTimeStorage")
mc.playbackOptions(min = s, max= e, e= True, aet= eT, ast = sT)
else:
storePlayBackRange()
storePlayBackRange()
mc.scriptJob(ct = ["readingFile",resetPBS])
mc.scriptJob(e = ["playbackRangeChanged",storePlayBackRange])
mc.scriptJob(e = ["playbackRangeSliderChanged",storePlayBackRange])
mc.scriptJob(ct = ["flushingScene",rmVar])
If this doesn't work, I'm afraid I can't solve your problem. Scriptjobs are fairly limited and I already used the only combination of conditions where I can assure they get triggered when a file is opened in the right order.
But I hope it works now.