The predefined functions if any are executed by TCAX in order, there are in total four interfaces:
tcaxPy_Init
Usually, it does some initialization, and will be executed only once before any other predefined functions of the tcaxPy script.
Implementation example:
def tcaxPy_Init():
# some common pre-defined global values
global fontSize # as name implies
global resX # horizontal resolution
global resY # vertical resolution
global marginX # horizontal margin
global marginY # vertical margin
global spacing # space between texts
global frameDur # milliseconds per frame
global lineNum # number of lines
global textNum # textNum[i], number of texts in ith line
global start # start[i], start time of a line
global end # end[i], end time of a line
global kar # kar[i][j], karaoke time of a syllable
global elapKar # elapKar[i][j], elapsed karaoke time before reaching a certain syllable
global text # text[i][j], as name implies
global textLength # textLength[i], total width of a line
global width # width[i][j], width of a text
global height # height[i][j], height of a text
global advance # advance[i][j], advance of a text, usually larger than width
global advDiff # advDiff[i][j], distance between the current text to the first text of the line
fontSize = GetVal(val_FontSize)
resX = GetVal(val_ResolutionX)
resY = GetVal(val_ResolutionY)
marginX = GetVal(val_OffsetX)
marginY = GetVal(val_OffsetY)
spacing = GetVal(val_Spacing)
frameDur = 1000 / GetVal(val_FXFPS)
lineNum = GetVal(val_nLines)
textNum = GetVal(val_nTexts)
start = GetVal(val_BegTime)
end = GetVal(val_EndTime)
kar = GetVal(val_KarTime)
elapKar = GetVal(val_KarTimeDiff)
text = GetVal(val_Text)
textLength = GetVal(val_TextLength)
width = GetVal(val_TextWidth)
height = GetVal(val_TextHeight)
advance = GetVal(val_TextAdvance)
advDiff = GetVal(val_TextAdvanceDiff)
# some user-defined global values
global font
global fontBord
font = InitFont(GetVal(val_FontFileName), GetVal(val_FaceID), fontSize, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(255, 255, 255), 0, 0)
fontBord = InitFont(GetVal(val_FontFileName), GetVal(val_FaceID), fontSize, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(0, 0, 0), 2, 1)
tcaxPy_Main
It provides some easy to use information through the parameters, and the function will be executed many times, which is decided by the number of syllables or texts.
Description of parameters:
_i:the current line being handled whose index is _i
_j:he current text being handled whose index is _j in the current line
_n:number of texts in the current line
_start:start time of the current line
_end:end time of the current line
_elapk:elapsed time from the first text of the line to the current text
_k:the karaoke time of the current text
_x:the horizontal position of the current text (an5)
_y:the vertical position of the current text (an5)
_a:the advance of the current text
_txt:the content of the current text
Note:
- the existing time of the current text is from _start + _elapk to _start + _elapk + _k
- you can replace the parameter names with whatever you preferred.
Implementation example:
def tcaxPy_Main(_i, _j, _n, _start, _end, _elapk, _k, _x, _y, _a, _txt):
ASS_BUF = [] # used for saving ASS FX lines
TCAS_BUF = [] # used for saving TCAS FX raw data
#############################
# TODO: write your codes here #
ass_main(ASS_BUF, SubL(_start, _end), pos(_x, _y) + K(_elapk) + K(_k), _txt)
#############################
return (ASS_BUF, TCAS_BUF)
tcaxPy_User
It will be executed only once and if this function is used, tcaxPy_Main function will be ignored. The difference between tcaxPy_User function and tcaxPy_Main function is that tcaxPy_User function leaves all the work to be done by the user, while tcaxPy_Main function leaves the I/O task to TCAX. The structure of tcaxPy_Main function is somewhat dull, not as flexible as tcaxPy_User function. However, you can make your own script template by implementing the tcaxPy_User interface.
Implementation example:
def tcaxPy_User():
ASS = CreateAssFile(GetVal(val_OutFile) + '.ass', GetVal(val_AssHeader))
for i in range(lineNum):
initPosX = (resX - textLength[i]) / 2 + marginX # if marginX = 0, then it's just on the middle
initPosY = marginY
for j in range(textNum[i]):
ASS_BUF = [] # you can put the BUF anywhere according to your usage
if text[i][j] == '' or text[i][j] == ' ' or text[i][j] == ' ':
continue
posX = initPosX + advDiff[i][j] + advance[i][j] / 2
posY = initPosY
ass_main(ASS_BUF, SubL(start[i], end[i]), an(8) + pos(posX, posY) + K(elapKar[i][j]) + K(kar[i][j]), text[i][j])
WriteAssFile(ASS, ASS_BUF)
Progress(i, j)
FinAssFile(ASS)
tcaxPy_Fin
Usually, it does some finalization, and it will be executed only once after any other predefined functions of the tcaxPy script.
Implementation example:
def tcaxPy_Fin():
FinFont(font)
FinFont(fontBord)
Pause()
TCC Options to Enable/Disable the Interfaces
You can change the options about whether to use these functions through the TCC file:
< tcaxpy init = true >
true - will use tcaxPy_Init function, and you should implement the tcaxPy_Init interface in your tcaxPy scripts.
< tcaxpy user = false >
false - will use tcaxPy_Main function instead, and you should implement the tcaxPy_Main interface in your tcaxPy scripts.
< tcaxpy fin = false >
false - will not use tcaxPy_Fin function, so you do not have to implement it.
you can change the settings according to your usage.
Miscellaneous
Python is a powerful scripting language, you can break the limitations of TCAX, and enhance it by using external modules and co-operate with your own tcaxPy script template, which can be achieved by using your own implementation of the predefined tcaxPy_User interface. Though such task is never easy, and requires your better understanding of Python programming and the concepts of TCAX.
Here is an example of implementing a new tcaxPy_User function.
http://www.tcax.org/forum.php?mod=viewthread&tid=278
预定义函数接口
预定义函数(如果有)由 TCAX 依次执行,共有四个接口:
tcaxPy_Init
通常,它会进行一些初始化,并且只会在 tcaxPy 脚本的任何其他预定义函数之前执行一次。
实现示例:
def tcaxPy_Init():
# 一些常见的预定义全局值
global fontSize # 顾名思义
global resX # 水平分辨率
global resY # 垂直分辨率
global marginX # 水平边距
global marginY # 垂直边距
全局间距 # 文本之间的空间
global frameDur # 每帧毫秒
global lineNum # 行数
global textNum # textNum[i],第 i 行的文本数
global start # start[i],一行的开始时间
global end # end[i],一行的结束时间
global kar # kar[i][j],一个音节的卡拉OK时间
global elapKar # elapKar[i][j],到达某个音节前经过的卡拉OK时间
global text # text[i][j],顾名思义
global textLength # textLength[i],一行的总宽度
global width # width[i][j], 一个文本的宽度
global height # height[i][j], 一个文本的高度
globalAdvance #advanced[i][j],一个文本的advance,通常大于width
global advDiff # advDiff[i][j],当前文本到行首文本的距离
fontSize = GetVal(val_FontSize)
resX = GetVal(val_ResolutionX)
resY = GetVal(val_ResolutionY)
marginX = GetVal(val_OffsetX)
marginY = GetVal(val_OffsetY)
间距 = GetVal(val_Spacing)
frameDur = 1000 / GetVal(val_FXFPS)
lineNum = GetVal(val_nLines)
textNum = GetVal(val_nTexts)
开始 = GetVal(val_BegTime)
结束 = GetVal(val_EndTime)
kar = GetVal(val_KarTime)
elapKar = GetVal(val_KarTimeDiff)
文本 = GetVal(val_Text)
textLength = GetVal(val_TextLength)
宽度 = GetVal(val_TextWidth)
高度 = GetVal(val_TextHeight)
提前 = GetVal(val_TextAdvance)
advDiff = GetVal(val_TextAdvanceDiff)
# 一些用户定义的全局值
全局字体
全局字体栏
font = InitFont(GetVal(val_FontFileName), GetVal(val_FaceID), fontSize, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(255, 255, 255), 0, 0)
fontBord = InitFont(GetVal(val_FontFileName), GetVal(val_FaceID), fontSize, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(0, 0, 0), 2, 1)
补充:下面这部分为TCAX的时间控制函数相关内容(即对前面tcaxPy_Main参数的中文翻译),跟Aegisub的retime比较类似
tcaxPy_Main
此函数通过参数提供一些便于使用的信息,函数会被执行很多次,这取决于音节或文本的数量。
参数说明:
_i:当前行,即第i行,i是文本行的计数器(相当于Aegisub的$li)
_j:当前行的第j个文字,j是当前行的文字序号的计数器相当于Aegisub的syl.i、$si)
_n:当前行的文本总数,其功能跟Aegisub的char修饰语有类似之处
_start:当前行的开始时间(相当于Aegisub的line.start、$lstart)
_end:当前行的结束时间(相当于Aegisub的line.end_time、$lend)
_elapk:从该行的第一个文本到当前文本经过的时间(相当于Aegisub的syl.start_time、$start2syl)
_k:当前文本的卡拉 OK 时间(相当于Aegisub的$dur、$sdur、$kdur)
_x:当前文本的横坐标 (an5)(相当于Aegisub的$center、$scenter、$lcenter)
_y:当前文本的纵坐标 (an5)(相当于Aegisub的$middle、$smiddle、$lmiddle)
_a:当前文本的宽度(相当于Aegisub的syl.width、line.width、$swidth、$lwidth)
_txt:当前文本的内容(相当于Aegisub的syl.text、line.text)
注意:
1.当前文本的存在时间是从_start + _elapk 到_start + _elapk + _k
2.您可以用您喜欢的任何名称替换参数名称。
实现示例:
def tcaxPy_Main(_i, _j, _n, _start, _end, _elapk, _k, _x, _y, _a, _txt):
ASS_BUF = [] # 用于保存 ASS FX 线路
TCAS_BUF = [] # 用于保存 TCAS FX 原始数据
############################
# TODO:在这里写你的代码#
ass_main(ASS_BUF, SubL(_start, _end), pos(_x, _y) + K(_elapk) + K(_k), _txt)
############################
返回(ASS_BUF,TCAS_BUF)
tcaxPy_User
只会执行一次,如果使用该函数,tcaxPy_Main 函数将被忽略。tcaxPy_User 函数和 tcaxPy_Main 函数的区别在于,tcaxPy_User 函数将所有工作留给用户完成,而 tcaxPy_Main 函数将 I/O 任务留给 TCAX。tcaxPy_Main 函数的结构有些单调,不如 tcaxPy_User 函数灵活。但是,您可以通过实现 tcaxPy_User 接口来制作自己的脚本模板。
实现示例:
def tcaxPy_User():
ASS = CreateAssFile(GetVal(val_OutFile) + '.ass', GetVal(val_AssHeader))
对于范围内的 i(lineNum):
initPosX = (resX - textLength[i]) / 2 + marginX # 如果 marginX = 0,那么它就在中间
initPosY = marginY
对于范围内的 j(textNum[i]):
ASS_BUF = [] # 可以根据自己的使用情况把BUF放在任何地方
如果 text[i][j] == '' 或 text[i][j] == ' ' 或 text[i][j] == ' ':
继续
posX = initPosX + advDiff[i][j] + Advance[i][j] / 2
posY = initPosY
ass_main(ASS_BUF, SubL(start[i], end[i]), an(8) + pos(posX, posY) + K(elapKar[i][j]) + K(kar[i][j]) , 文本[i][j])
WriteAssFile(ASS, ASS_BUF)
进度(i, j)
FinAssFile(ASS)
tcaxPy_Fin
通常,它会做一些终结,并且只会在 tcaxPy 脚本的任何其他预定义函数之后执行一次。
实现示例:
def tcaxPy_Fin():
FinFont(字体)
FinFont(fontBord)
暂停()
启用/禁用接口的 TCC 选项
您可以通过 TCC 文件更改有关是否使用这些函数的选项:
< tcaxpy init = true >
true - 将使用 tcaxPy_Init 函数,并且您应该在 tcaxPy 脚本中实现 tcaxPy_Init 接口。
< tcaxpy user = false >
false - 将使用 tcaxPy_Main 函数代替,您应该在 tcaxPy 脚本中实现 tcaxPy_Main 接口。
< tcaxpy fin = false >
false - 不会使用 tcaxPy_Fin 函数,因此您不必实现它。
您可以根据使用情况更改设置。
杂项
Python 是一种强大的脚本语言,您可以打破 TCAX 的局限性,并通过使用外部模块对其进行增强,并配合您自己的 tcaxPy 脚本模板,这可以通过使用您自己的预定义 tcaxPy_User 接口的实现来实现。尽管这样的任务绝非易事,并且需要您更好地理解 Python 编程和 TCAX 的概念。
这是一个实现新 tcaxPy_User 函数的示例。
http://www.tcax.org/forum.php?mod=viewthread&tid=278