Trying to write a script to insert some text on layer Topic is solved

A forum dedicated to George scripting questions
Post Reply
User avatar
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Trying to write a script to insert some text on layer

Post by Lukas »

I want to be able to hit a button that pastest text from the clipboard into a specific layer.

Is there a StringFromClipboard kind of function? And if not, is there a way to automatically activate the textbox of tv_requeststring?

Also, what is the best way to revert to the tool (brush/color/everything) (can george access the tool history?). I tried using tv_brushrestore (see code) but it's unclear to me how to use it correctly.

I'll probably figure it out if I keep bashing my head against the wall, but scripts like this possibly exist out there... :) I just can't find any.

Edit: updated the code. (again)

Code: Select all

// Variables
dialogueLayerName = "DIALOGUE"
dialogueLayerColor = 7

// Ask user to insert the dialogue
tv_ReadUserString "LukasSketchPanel" "Dialogue" ""
text_to_add = result
IF CMP(text_to_add, "0") // tv_ReadUserString returns 0 if it doesn't exist yet
	text_to_add = ""
END
tv_ReqString "multiline" "Dialogue|"text_to_add
IF CMP(result, "Cancel") || CMP(result, "")
	EXIT
END
text_to_add = result
tv_writeUserString "LukasSketchPanel" "Dialogue" text_to_add
text_to_add = "'"text_to_add"'"
text_to_add = Replace(text_to_add, "\n", "' '")
text_to_add = text_to_add" 'none'" // 'none' is always used as a terminator in the packed string
Rest = "" // remaining string
LineCount = 1	// Item count
Lines(0) = 0 // Array of lines
While CMP(Rest,"none") == 0
	Parse text_to_add Line Rest
	Lines(LineCount) = Line
	text_to_add = Rest
	LineCount = LineCount + 1
END

// Paste brush at center of screen
tv_ProjectInfo
PARSE result projectname ProjectWidth projectheight projectpixelaspect projectframerate projectfield projectprojectstartframe

// ----------------------------------------------------
// Remember settings to revert to after script finished
// ----------------------------------------------------
// Remember color
tv_GetAPen
PARSE result R G B A
// Remember paper
tv_paperinfo
OriginalPaper = result
parse result z paperHardness z paperInvert z paperSize z paperAngle z paperOffsetX z paperOffsetY z paperFlipX z paperFlipY z paperActive z paperName
// Remember shape (line etc)
tv_getactiveshape
OriginalActiveShape = result
// Remember brush
tv_restorebrush backup
OriginalBrush = result
// Remember tool (stamp mode)
tv_GetActiveTool
OriginalTool = result
// Remember layer
tv_LayerCurrentID
OriginalLayer = result
tv_LayerGetImage // Remember frame number
parse result OriginalFrame // original frame number

CharacterCount = 0
FOR i = 1 to LineCount-1
	Line = Lines(i)
	IF LEN(Line) > CharacterCount
		CharacterCount = LEN(Line)
	END
END
CharacterWidth = 2700
MinFontSize = 50
MaxFontSize = 120
CharacterWidth = 2700 // Font size for a 1-letter wide character
ReductionFactor = 375 // Reduction in font size for each additional character
FontSize = CharacterWidth - (CharacterCount - 1) * ReductionFactor

CharacterPx = 60
FontSize = ProjectWidth/(CharacterCount * CharacterPx)*100


IF FontSize < MinFontSize
	//FontSize = MinFontSize //65 characters is te lang in dit geval op 1920w
END
IF FontSize > MaxFontSize
	FontSize = MaxFontSize
END

// Loop backwards trough layers to find DIALOGUE layer:
layerRun = 1
layerPos = 0
WHILE layerRun
	tv_LayerGetID layerPos
	lid = result
	IF CMP(lid,"NONE") == 0
		// Start run on layer
		tv_LayerInfo lid
		PARSE result layerDisplay layerPosition layerOpacity layerName layerType layerStart layerEnd layerPrelighttable layerPostlighttable layerSelection
		IF CMP(layerName, dialogueLayerName) == 1
			tv_layerSet lid
			layerRun = 0
		END
		// End run on layer
		layerPos = layerPos + 1
	ELSE
		// Create or go to the dialogue layer
		tv_LayerCreate dialogueLayerName
		DialogueLayerID = result
		tv_layercolor "set" 0 dialogueLayerColor
		tv_LayerMove 0 // Move the dialogue layer to top
		tv_layershift DialogueLayerID OriginalFrame
		layerRun = 0
	END
END

// Set text tool:
fontName = "Consolas"
resetVal = 1 // * 1 = true
letterVal = 0 // * 0 = Word
borderSize = 5
borderR = 255
borderG = 255
borderB = 255
stepVal = 9999999 // * Super big step so word is only drawn once
tv_textTool reset resetVal letter letterVal text text font fontName size FontSize border borderSize border_col borderR borderG borderB step stepVal
// Set text color:
textColR = 0
textColG = 150
textColB = 255
tv_SetAPen textColR textColG textColB

// Clear frame of dialogue layer
tv_layeranim
currentFrame = tv_projectcurrentframe
tv_exposurebreak currentFrame
tv_Clear 0

x = projectwidth / 2
y = FontSize * 1.5

FOR i = 1 to LineCount-1 // loop to display elements which are now in an array 
	Line = Lines(i)
	tv_TextTool text '"'Line'"'
	button = 0 // left mouse button
	tv_Dot x y button
	y = y + FontSize
END

// Revert layer (Disabled)
//tv_layerSet OriginalLayer

// Revert brush
tv_brushrestore OriginalBrush
// Revert tool
tv_cmd OriginalTool
// Revert shape mode
tv_SetActiveShape OriginalActiveShape
// Revert A color
tv_SetAPen R" "G" "B" "A // Reset A pen color to start color
// Revert Paper
tv_paper "active" paperActive "offsetx" paperOffsetX "offsety" paperOffsetY "flipx" paperFlipX "flipy" paperFlipY "hardness" paperHardness "invert" paperInvert "size" paperSize "angle" paperAngle "name" paperName

// Include
#INCLUDE "Include.grg"
Last edited by Lukas on 02 Apr 2024, 13:41, edited 3 times in total.
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
NathanOtano
Posts: 1202
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Trying to write a script to insert some text on layer

Post by NathanOtano »

From what I remember there is no text from clipboard to a variable possibility in george indeed, could be a nice addition.

What do you mean by "to automatically activate the textbox of tv_requeststring". Can"t you use tv_ReqString at the start of your script and ctrl+v? I don't understand the problem

tv_brushrestore "backup" help you backup your whole brush, you just need to parse it in multiple variables (or maybe all in one variable but I'm not sure if it works) and use them back when you set the brush again with the regular function. But it's a bit harder for custom brushes if you lose the image somehow : you kind of need to save them somewhere in a temp folder and reimport them. You can try swaping stylus tip with the shortcut to kind of avoid that, and putting it back after your script executed. From memory, you also kind of need to manage drawing mode and paper, sometimes some functions are messing with it

Here since you only switch to text tool, you shouldn't need to backup your brush? Just use tv_GetActiveShape and tv_SetActiveShaoe
Last edited by NathanOtano on 21 Mar 2024, 13:49, edited 9 times in total.
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
NathanOtano
Posts: 1202
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Trying to write a script to insert some text on layer

Post by NathanOtano »

--Bug, I sent two messages sorry--
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
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Re: Trying to write a script to insert some text on layer

Post by Lukas »

Thanks for your reply :)
NathanOtano wrote: 21 Mar 2024, 13:31What do you mean by "to automatically activate the textbox of tv_requeststring". Can"t you use tv_ReqString at the start of your script and ctrl+v? I don't understand the problem
When you run tv_ReqString it doesn't activate the textbox. you have to click it with the mouse before ctrl+V. Not something I'm incapable of, but it's bad UX, so if I can activate it with script that would be better.
NathanOtano wrote: 21 Mar 2024, 13:31Here since you only switch to text tool, you shouldn't need to backup your brush? Just use tv_GetActiveShape and tv_SetActiveShaoe
Oh nice, that works!

I did not use tv_TextTool though, but was using tv_TextBrush. I switched now, but am unable to draw more than a single word. How do I put in a string containing space characters in the Text Tool via george?

Code: Select all

tv_TextTool "letter 0" "text hello two words"
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
Hironori Takagi
Posts: 285
Joined: 14 May 2018, 10:15
Location: Tokyo, Japan
Contact:

Re: Trying to write a script to insert some text on layer

Post by Hironori Takagi »

I looked into this issue previously, but there was no way to import into TVPaint via the clipboard.
Because it is difficult to interact with external text files, some of my scripts made use of the memo field that is set for each clip.

The attached script is an example. I'm glad if you can use it as a reference.
Attachments
SetDialogueBox_221101.grg
(7.13 KiB) Downloaded 34 times
TVPaint 11.7.2(Nov 14 2023), Windows11 Pro, HP Spectre x360 Convertible 14-ea0xxx / TVPaint 11.7.1(Dec 22 2022) Mac OS 11.6, Apple MacBookPro M1 2020
User avatar
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Re: Trying to write a script to insert some text on layer

Post by Lukas »

Hironori Takagi wrote: 22 Mar 2024, 09:06 I looked into this issue previously, but there was no way to import into TVPaint via the clipboard.
Because it is difficult to interact with external text files, some of my scripts made use of the memo field that is set for each clip.

The attached script is an example. I'm glad if you can use it as a reference.
Thank you that's very generous!

I already learned one thing from your script and that's how to keep spaces intact in a string by doing it this way:

Code: Select all

tv_TextTool text '"'text_to_add'"'
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
NathanOtano
Posts: 1202
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Trying to write a script to insert some text on layer

Post by NathanOtano »

Ok I understand for the text box pb!
Yes indeed textool seems a bit more useful in your case :)
Cool that you found how to retain spaces in your text string

An interesting hack is that you can paste your clipboard in the Action or Dialogue or Notes box of your clip, or in the "Information" or "Author" or "Notes" box for your project, and use that box in your script to get your string (each has a function to get the string). But I guess you need to go to project timeline view, click and paste etc so it's kind of as many clicks as reqstring.
In my storyboard export script I paste those infos as text in pixels around the exported frames to have everything in my png export : https://forum.tvpaint.com/viewtopic.php ... storyboard
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
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Re: Trying to write a script to insert some text on layer

Post by Lukas »

NathanOtano wrote: 22 Mar 2024, 09:51 Ok I understand for the text box pb!
Yes indeed textool seems a bit more useful in your case :)
Cool that you found how to retain spaces in your text string

An interesting hack is that you can paste your clipboard in the Action or Dialogue or Notes box of your clip, or in the "Information" or "Author" or "Notes" box for your project, and use that box in your script to get your string (each has a function to get the string). But I guess you need to go to project timeline view, click and paste etc so it's kind of as many clicks as reqstring.
In my storyboard export script I paste those infos as text in pixels around the exported frames to have everything in my png export : https://forum.tvpaint.com/viewtopic.php ... storyboard
Nice! I will definately check your tools out as I currently use TVPaint 99% for storyboarding anyway.

I updated the code in the first post to the current state. Everything is working as I need it now, except for the brush 'Stamp' mode is not being reverted to its original state. (My storyboard brushes are set to 'Alpha Stamp', but I have not yet found a way to get and set the Stamp Mode).

Brush image/brush color/paper etc is all retained. Just the stamp mode is left. Any clues?
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Re: Trying to write a script to insert some text on layer

Post by Lukas »

Figured it all out:

Code: Select all

// Variables
dialogueLayerName = "DIALOGUE"
dialogueLayerColor = 7

// Ask user to insert the dialogue
tv_ReqString "multiline" "Dialogue|"
IF CMP(result, "Cancel") || CMP(result, "")
	EXIT
END
text_to_add = result
text_to_add = "'"text_to_add"'"
text_to_add = Replace(text_to_add, "\n", "' '")
text_to_add = text_to_add" 'none'" // 'none' is always used as a terminator in the packed string
Rest = "" // remaining string
LineCount = 1	// Item count
Lines(0) = 0 // Array of lines
While CMP(Rest,"none") == 0
	Parse text_to_add Line Rest
	Lines(LineCount) = Line
	text_to_add = Rest
	LineCount = LineCount + 1
END

// ----------------------------------------------------
// Remember settings to revert to after script finished
// ----------------------------------------------------
// Remember starting color
tv_GetAPen
PARSE result R G B A
// Remember starting paper
tv_paperinfo
beginPaper = result
parse result z paperHardness z paperInvert z paperSize z paperAngle z paperOffsetX z paperOffsetY z paperFlipX z paperFlipY z paperActive z paperName
// Remember starting shape (line etc)
tv_getactiveshape
beginActiveShape = result
// Remember starting brush
tv_restorebrush backup
beginBrush = result
// Remember starting layer
tv_LayerCurrentID
beginLayer = result
tv_LayerGetImage // get original frame number
parse result OriginalFrame // original frame number
// TODO also remember brush stamp (origin/alpha/etc)

// Loop backwards trough layers to find DIALOGUE layer:
layerRun = 1
layerPos = 0
WHILE layerRun
	tv_LayerGetID layerPos
	lid = result
	IF CMP(lid,"NONE")==0
		// Start run on layer
		tv_LayerInfo lid
		PARSE result layerDisplay layerPosition layerOpacity layerName layerType layerStart layerEnd layerPrelighttable layerPostlighttable layerSelection
		IF CMP(layerName, dialogueLayerName)==1
			tv_layerSet lid
			layerRun = 0
		END
		// End run on layer
		layerPos = layerPos + 1
	ELSE
		// Create or go to the dialogue layer
		tv_LayerCreate dialogueLayerName
		DialogueLayerID = result
		tv_layercolor "set" 0 dialogueLayerColor
		tv_LayerMove 0 // Move the dialogue layer to top
		tv_layershift DialogueLayerID OriginalFrame
		layerRun = 0
	END
END

// Set text tool:
fontName = "Consolas"
fontSize = 50
resetVal = 1 // * 1 = true
letterVal = 0 // * 0 = Word
borderSize = 5
borderR = 255
borderG = 255
borderB = 255
stepVal = 9999999 // * Super big step so word is only drawn once
tv_textTool reset resetVal letter letterVal text text font fontName size fontSize border borderSize border_col borderR borderG borderB step stepVal
// Set text color:
textColR = 0
textColG = 150
textColB = 255
tv_SetAPen textColR textColG textColB

// Clear frame of dialogue layer
tv_layeranim
currentFrame = tv_projectcurrentframe
tv_exposurebreak currentFrame
tv_Clear 0

// Paste brush at center of screen
tv_ProjectInfo
PARSE result projectname projectwidth projectheight projectpixelaspect projectframerate projectfield projectprojectstartframe
x = projectwidth / 2
y = 120

FOR i = 1 to LineCount-1 // loop to display elements which are now in an array 
	Line = Lines(i)
	tv_TextTool text '"'Line'"'
	button = 0 // left mouse button
	tv_Dot x y button
	y = y + 60
END

// Revert to starting layer
//tv_layerSet beginLayer

// Revert to starting brush/tool
tv_brushrestore beginBrush // Reset brush
//TODO Reset brush stamp mode (origin/alpha/etc)
tv_SetActiveShape beginActiveShape // Reset shape mode
tv_SetAPen R" "G" "B" "A // Reset A pen color to start color
tv_paper "active" paperActive "offsetx" paperOffsetX "offsety" paperOffsetY "flipx" paperFlipX "flipy" paperFlipY "hardness" paperHardness "invert" paperInvert "size" paperSize "angle" paperAngle "name" paperName

// Include
#INCLUDE "Include.grg"
This is very useful for and you need to paste dialogue on screen, before doing (scratch)voices.
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
NathanOtano
Posts: 1202
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Trying to write a script to insert some text on layer

Post by NathanOtano »

Yeah for stamp mode I remember that on my out of peg script I needed to store it separately and restore it separately with a specific command
Thanks for the script :) really useful
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
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Re: Trying to write a script to insert some text on layer

Post by Lukas »

You're welcome :)
NathanOtano wrote: 01 Apr 2024, 13:48 Yeah for stamp mode I remember that on my out of peg script I needed to store it separately and restore it separately with a specific command
Would you mind sharing the code of storing and restoring the stamp mode? I couldn't find the script you mentioned.

My brushes with 'Alpha Stamp' keep reverting to 'Origin Stamp' after running the script. Can't find the function to address it. I might be overlooking something in the documentation..
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
NathanOtano
Posts: 1202
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Trying to write a script to insert some text on layer

Post by NathanOtano »

Ok! Sorry I should have directly given you the code.
I actually checked it and I completely forgot but tv_cmd was my trick? I don't know where i got this from since it doesn't seem to follow the documentation really well... Here are the two portions of code I used :

Code: Select all

tv_GetActiveTool 
tv_writeuserstring "oopbrushmemo" "originaltool" result

Code: Select all

tv_readuserstring "oopbrushmemo" "originaltool"
tv_cmd result
Tell me if it makes you revolve your problem? I havn't checked myself

Here are my out of peg scripts : https://forum.tvpaint.com/viewtopic.php ... out+of+peg
I don't see any other relevant function relevant for our needs here but if you want I'm happy to share the code
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
Lukas
Posts: 526
Joined: 14 Jan 2011, 11:15
Contact:

Re: Trying to write a script to insert some text on layer

Post by Lukas »

That works perfectly! Thanks :)
  • Lukas Sketch Panel
  • TVPaint Pro 11.7.3
  • MacBook Pro 2018 macOS Ventura 13.4.1 + PC Windows 10
  • Wacom Cintiq 27QHD + Wacom Intuos4
  • YouTube.com/@ClubBaboo
  • YouTube.com/@FrameOrder
User avatar
NathanOtano
Posts: 1202
Joined: 01 Apr 2014, 07:07
Location: Biarritz, France
Contact:

Re: Trying to write a script to insert some text on layer

Post by NathanOtano »

Great! :)
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