Determine whether script has already run during current session.

A forum dedicated to George scripting questions
Post Reply
spyderheart
Posts: 124
Joined: 22 Jan 2013, 03:11

Determine whether script has already run during current session.

Post by spyderheart »

Hi all,
I'm trying to determine via George whether or not the current script has run since the current project was opened.
Using the project ID is not really useful since it is consistent between sessions.

I think what I need is access to the system file information - especially the "Last Opened" property. If I could access that, I could also log the time every time I run the script in the user string. Then each time I can compare the time of the last script execution vs the time the project was opened - if earlier, then this must be the first script execution since opening the project.

I'm open to other suggestions too. My larger goal is to be able to uniquely identify clips and layers between sessions. It's easy within a session as you can just store the clip ID and layer ID in the userstring and call them up whenever needed, but these are re-assigned every time you open a project so if you've just re-opened a project any scripting that was specific to a clip or layer is now in a state of uncertainty until you do some verifications in your scripts (check the name, the position, etc.). Even then - you can never regain total certainty that you are looking at the same clip or layer that you originally stored and not a duplicate, or different one that was renamed for example.

All this to say - if I could get my script to do this verification work ONLY on its first execution since opening the project, then the script could proceed to only use the ID's for the rest of the session - which is much faster and less ambiguous.

I'm working towards a custom tool that I intend to share when it's ready - so I prefer to avoid any external solutions like command line scripts and the like. Pure George over here ;)

Thanks!
David
--------------------------------------------------------
TVPaint 11.7 Standard
System: 64GB RAM // 3 TB SSD // Core i7 8700K // NVIDIA GTX 1080// Windows 10 Home
spyderheart
Posts: 124
Joined: 22 Jan 2013, 03:11

Re: Determine whether script has already run during current session.

Post by spyderheart »

***UPDATE***

So far I have come up with a workaround that should work most of the time. When my script runs, it checks the clipstring for the ID the clip had last time the was run (because the same script also wrote the ID to the clipstring last time). If it doesn't match - chances are the the project has been closed and re-opened since last script execution. Then I can do all the verification needed and update the stored clip ID . Next time the script runs during the same session - it will see that the ID is same as last time so it is safe to assume it is the same session. Duplicating a clip could trigger an extra verification here and there but that would be harmless and, in some cases, necessary.

Code: Select all

tv_readclipstring "MyScript" "LastClipID"
lastID = result

tv_clipcurrentID
curID = result

IF CMP(curID,lastID)==0

//ID's don't match - Assume this is the first time script has run since opening project.
//Run code for new session here (in my case, checking layer id's and names etc. to uniquely identify them):

...

//Update the stored ID so that next check during this session gives a match:

tv_writeclipstring "MyScript" "LastClipID" curID 

END

//Continue with script's original purpose
...
The weakness of this approach is that if the clip ID happens to be the same as last session, the script will not detect the "first run" status. From what I can see though, that is pretty unlikely.
David
--------------------------------------------------------
TVPaint 11.7 Standard
System: 64GB RAM // 3 TB SSD // Core i7 8700K // NVIDIA GTX 1080// Windows 10 Home
Svengali
Posts: 1571
Joined: 28 Dec 2006, 10:08

Re: Determine whether script has already run during current session.

Post by Svengali »

See if this works for you:

Code: Select all

// INIT.GRG
tv_WriteUserString "INIT" "Counter" 0
EXIT

Code: Select all

// UPDATE.GRG

tv_ReadUserString "INIT" "Counter" 0
Counter = result
NewCounter = Counter + 1
tv_WriteUserString "INIT" "Counter" NewCounter
IF Counter > 0
	tv_warn "Update script has already run " Counter "time(s)..."
ELSE
	tv_warn "Update script has not been run yet"
END
Start tvpaint with the following command: tvpaint.exe "script=INIT.GRG"
(you will need to replace tvpaint.exe with your path and version that starts you up.)

INIT.GRG gets executed and initializes the CONFIG.INI UserString "INIT" "Counter" to 0.

UPDATE.GRG each time it runs, first reads the current value from the UserString "INIT" "Counter", increments it and writes it back.
It then tests if the counter value is more than zero, and if it is, then UPDATE.GRG was run at least once in current TVPaint session.

Sven
TVP Pro 11.0.10-64bit Win10 - 64GB ram -2TB HHD - 256GB SSD - Wacom Cintiq 16, driver 6.3.41-1
Android Tablet: rel. 11, Samsung Galaxy Note10.1 - 32GB with microSD 32GB
Android Tablet: rel. 11.5, Samsung Galaxy Tab S7plus - 128GB with microSD 64GB
spyderheart
Posts: 124
Joined: 22 Jan 2013, 03:11

Re: Determine whether script has already run during current session.

Post by spyderheart »

Thanks Sven,
I'll try that out when I'm back on a Windows system. On OSX I see that you can open a .grg file with TVPaint by right clicking it and choosing "Open With..." - but it doesn't appear to run the script on startup (tested using your INIT.GRG).
This approach could work for you or me, but I'm still hoping to find something that just works out of the box when people out in the wild install the tvpx panel (preferably on any OS). The other limitation is that it needs to reset the counter when a new project is opened and not necessarily only when the TVPaint application is opened.

So far I've had good results with the clip ID comparison method. I'm more or less comfortable with the small odds of a clip having the same ID between 2 sessions - something I have yet to witness. Also, in the case of the tool I'm working on, the potential error caused by the ambiguity is not that critical - just a bit annoying. So for now I guess I'm sticking with the clip ID method... unless TVPaint chimes in here with some magic :D
David
--------------------------------------------------------
TVPaint 11.7 Standard
System: 64GB RAM // 3 TB SSD // Core i7 8700K // NVIDIA GTX 1080// Windows 10 Home
Post Reply