Toggle script Topic is solved

A forum dedicated to George scripting questions
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Toggle script

Post by schwarzgrau »

I try to write a script which searches for layers using the layer colors Red, Orange and Blue (my sketching layer colors), which are visible. Then writing their IDs in an array and turning them off. On the second button press it should turn the layers in this array back on. The first part of this script is pretty easy, but I'm struggling with the second part, since the script surely starts at the beginning. Is there any way to "pause" the script, till I hit the button again or make it toggle in some way?

Here is what I have so far

Code: Select all

loop = 1
i = 0
WHILE loop
		tv_LayerGetID i
		curLID= result	
			IF CMP(curLID,"NONE")==0
				tv_LayerInfo curlID
				PARSE result layerDisplay layerPosition layerOpacity layerName layerType layerStart layerEnd layerPrelighttable layerPostlighttable layerSelection

				tv_layercolor "get" curlID
				curlCOL = Result 

				IF CMP(layerDisplay,"On")==1

					IF CMP(curlCOL,"1") || CMP(curlCOL,"4") || CMP(curlCOL,"3")
						SKarray[i] = curlID
						tv_LayerDisplay curlID Off
					END				
				END
				i= i+1
			ELSE
				loop= 0
			END
	END
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

It seems to me George Script has no memory. I can't set a variable in the first run and access this variable in a second run, right?
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
Svengali
Posts: 1571
Joined: 28 Dec 2006, 10:08

Re: Toggle script

Post by Svengali »

You can safely store ANY info in the CONFIG.INI file using the tv_WriteUserString command (look it up). That info can then be read back later using the tv_ReadUserString command. The stored CONFIG.INI info remains, even between TVPaint sessions.

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
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

Thank you, thats some useful information. I guess I would have to write every entry in my array in a seperate line to read it afterwards, right?
But the problem persists, that I can't toggle between "writing-mode" and "reading-mode".
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
Svengali
Posts: 1571
Joined: 28 Dec 2006, 10:08

Re: Toggle script

Post by Svengali »

schwarzgrau wrote:Thank you, thats some useful information. I guess I would have to write every entry in my array in a seperate line to read it afterwards, right?
But the problem persists, that I can't toggle between "writing-mode" and "reading-mode".
Sure, you could write every entry in the array as a separate line, but I find it easier to store the states in a single, parse-able string like this:

LayerString = 4 0 1 1 0
where the first number is the LayerCount, and the other values are the LayerStates for each of the four layers, 0 = hidden 1 = visible.

So, first you store the LayerCount in LayerString, then loop to Concatenate the LayerStates of each layer (0 or 1) and separate each one with a space.

Store it: tv_WriteUserString "LayerStatus" "LayerStates" LayerString.

Retrieve it: tv_ReadUserString "LayerStatus" "LayerStates" "none"
parse result LayerCount LayerStates
Then, loop to read the LayerStates of each layer and restore them.

As for the toggle problem, I'd simply use a menu:

1. Save LayerStates
2. Load LayerStates

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
User avatar
NathanOtano
Posts: 1209
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Toggle script

Post by NathanOtano »

I'd personnally just write a string that turns off layers on the chosen colors if the string says on and turns the string to off, and vice versa. That way you always turn all your layers on and off even if there is new layers or deleted ones without worrying about listing all the layers somewhere
Working on Windows 10
Creator of Disnosc, providing storyboard, animation and design for 2D realistic pictural animation: https://www.disnosc.fr/ - nathanotano@disnosc.fr
Highly interested in animation workflows, I'm open to scripting new TVP functions for individuals and studios.
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

Thank you a lot Svengali. Seems the two buttons method is the way to go.
NathanOtano wrote:I'd personnally just write a string that turns off layers on the chosen colors if the string says on and turns the string to off, and vice versa. That way you always turn all your layers on and off even if there is new layers or deleted ones without worrying about listing all the layers somewhere
Usually this would be the simplest solution, but currently I'm working on a scene with nine characters, which are overlapping each other.
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

I've got two additional question:

1. How do I split the string correctly?
The string looks like this

Code: Select all

2 12994 12995
The first number is the layer count, the others are the layer IDs.

If I use

Code: Select all

parse result LayerCount LID
I get one variable called LayerCount, containing the number of layers and another variable called LID, containing all IDs, separated with a space. But how do I transform this string in an array?

I tried it with CUT(), but I constantly getting error messages

Code: Select all

tv_ReadUserString "Sketchlayers" "LayerIDs" "none"
parse result lc LID

FOR i = 1 TO lc
	cutstart = i*6
	cutend = cutstart+5
	SKarray[i] = CUT(LID, cutstart, cutend)
END
If I replace cutstart and cutend with numbers like CUT(LID, 6,11) It works.


2. Is it possible to check if an array is empty in george?
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
Svengali
Posts: 1571
Joined: 28 Dec 2006, 10:08

Re: Toggle script

Post by Svengali »

Maybe this simpler solution will help...

Set the GroupList string flags to indicate which colors you want to toggle = 1 means toggle 0 means ignore
In the sample script, the hardwired GroupList string flags are set for colors 1, 5, 7, 11 - which will toggle

I find Its best to capture LayerIDs on the fly.

Code: Select all

// ToggleGroups.grg
// loop through all layers, toggling specified Groups 

GroupList = " 0 1 0 0 0 1 0 1 0 0 0 1 0"				// 13 Group color states, 1 = toggle 0 = don't toggle

For i = 0 to 12											// load Group array with on/off flags
	Offset = (i * 2) + 2								// offset into GroupList string
	Group(i) = CHAR(GroupList, Offset)					// set each element to 1=toggle 0=no-toggle
END

ExitFlag = 0											// set ExitFlag false
Counter = 0												// init layers counter
While ExitFlag == 0										// loop while ExitFlag is false
	tv_LayerGetID Counter								// get layer id
	LayerID = result
	IF CMP(LayerID, "none")								// is this beyond last layer?
		ExitFlag = 1									// if yes, set ExitFlag true
	ELSE
		tv_LayerColor GET LayerID						// get Group color and number for this layer
		GroupColor = result
		FOR i = 0 to 12									// loop through Group colors
			IF Group(i) == 1							// is this one of the Group colors to be toggled? 
				IF i == GroupColor						// which toggle GroupColor is it?
					tv_LayerDisplay LayerID "Off"		// set layer display off
					PreviousState = result				// get PREVIOUS display state
					IF CMP(PreviousState, "Off")		// if it was previously OFF
						tv_LayerDisplay LayerID "On"	// toggle it ON OTHERWISE, leave it OFF
					END
				END
			END
		END
	END
	Counter = Counter + 1								// increment layers counter
END
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
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

Wow thank you a lot for this Sven! But I still don't know how to parse the string correctly.
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
Svengali
Posts: 1571
Joined: 28 Dec 2006, 10:08

Re: Toggle script

Post by Svengali »

The rules for parsing are simple.

GEORGE parses variables that are separated by spaces.

String1 = "1 2 3 4 5"
parse String1 var1 var2 var3 var4 var5
tv_warn var3 // will show the number 3

String2 = "1 2 three 4 5"
parse String2 var1 var2 var3 var4 var5
tv_warn var3 //will show the word(string) three

String3 = "1 2 3003 4 5"
parse String3 var1 var2 var3 var4 var5
tv_warn var3 // will show the number 3003

String4 = "1 2 'this is three' 4 5"
parse String4 var1 var2 var3 var4 var5
tv_warn var3 // will show the string this is three (single quotes allow inclusion of spaces stored as part of the string)

String5 = "1 2 3 4 5"
parse String5 var(1) var(2) var(3) var(4) var(5)
Will generate ERROR: ILLEGAL DESTINATION EXPRESSION IN PARSE
Apparently, GEORGE isn't sophisticated enough to parse into the elements of an array (maybe there is some way to force this???)

String6 = "1 2 3 4 5"
parse String6 var1 var2 var3 var4 var5
var(1) = var1
var(2) = var2
var(3) = var3
var(4) = var4
var(5) = var5
explicitly assigning array elements from the parsed variables is awkward but will work if absolutely necessary for some array operation...

Bottom line, string-packing and string-unpacking is an efficient way to store a bunch of different values (string and numeric) into a single long string variable when parsed with spaces - which can then be stored and retrieved from the config.ini file using tv_WriteUserString and tv_ReadUserString.

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
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

Thank you a lot for this detailed explanation! Is this information somewhere written cause I didn't found something about it in the George-Wiki?

Oh and could you tell me how I parse a string with undefinied length? In my case the parts of the string becomes longer with more layers and shorter if less layers are included. That's the reason why I tried to split it using CUT().
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
Svengali
Posts: 1571
Joined: 28 Dec 2006, 10:08

Re: Toggle script

Post by Svengali »

schwarzgrau wrote:Oh and could you tell me how I parse a string with undefinied length? In my case the parts of the string becomes longer with more layers and shorter if less layers are included. That's the reason why I tried to split it using CUT().
I haven't worked out a way to solve that, sorry. You might take a LOOK HERE (at NewParse Function) for some alternative parsing ideas...

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
User avatar
schwarzgrau
Posts: 1238
Joined: 23 Jan 2012, 22:08
Location: Offenbach / Germany
Contact:

Re: Toggle script

Post by schwarzgrau »

That was exactly what I'm searching for! Again thank you a lot Sven! Now everything works! By the way I figured out a way to make it work with one button (at least for this special case): The script searches for visible sketch layers, if it found some he will turn them off and write them down, if it doesn't found one, he searches in the config.ini for the string. If there is some it'll use the string to turn the layers back on. Not a bulletproof solution, but it works in my case.
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Cintiq 22HD
Windows 11 22H2 / TVP 11.7.0 PRO WIBU / Mobile Studio Pro 16" (2019)
Android 13 / TVP 11.7.0 / Galaxy Tab 7 FE
INSTAGRAM
User avatar
NathanOtano
Posts: 1209
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Toggle script

Post by NathanOtano »

Interesting :) Thanks
Working on Windows 10
Creator of Disnosc, providing storyboard, animation and design for 2D realistic pictural animation: https://www.disnosc.fr/ - nathanotano@disnosc.fr
Highly interested in animation workflows, I'm open to scripting new TVP functions for individuals and studios.
Post Reply