.. _toonzscript: ToonzScript =========== ToonzScript is the scripting language used in Tahoma. Using ToonzScript allows users to create sequences of commands, thus avoiding the need to perform repetitive tasks manually. ToonzScript is based on QtScript and is built on the ECMAScript standard. A reference guide for the ECMAScript standard can be found at the following URL: `ECMAScript Reference `_ .. note:: Several well known scripting languages are based on the ECMAScript standard such as JavaScript, JScript, and ActionScript. The ToonzScript syntax is similar to that of JavaScript, for this reason, a good introduction to JavaScript can be very useful to better understand ToonzScript's basics. The original ToonzScript page with some (rather old) examples is available at the following URL: `ToonzScript page `_ Scripts can be run using the ``File → Run Script...`` command. Alternatively, commands can be typed and executed directly in the Script Console, which can be opened using the ``File → Open Script Console...`` command. .. note:: When a script is running, the Script Console will open automatically to show the commands contained within the script. .. _file_management: File management --------------- |toonz_script_runscript| The ToonzScript file extension is .js, scripts need to be in the ``Tahoma stuff/library/script`` folder, to be automatically retrieved by opening the Run Script command browser. .. _using_the_script_console: Using the Script Console ------------------------ |toonz_script_console| The Script Console allows the user to directly enter commands, additionally, dragging & dropping external text files into the console will automatically insert the path of the file(s). Start a series of lengthy operations from the console does not block the interface. .. _console_use_conventions: Console use conventions ''''''''''''''''''''''' The following conventions are implemented in the use of the Script Console: - Only the text of the last paragraph can be edited. - The Up and Down arrow keys allow the navigation of the command history. - The execution of time consuming commands doesn’t prevent interaction with the software interface. - The **ctrl+y** shortcut interrupts the execution of time consuming commands. - When entering a path in a string, any and all backslashes must be doubled. Following this rule the path: ``C:\tmp\test.jpg`` becomes ``C:\\tmp\\test.jpg`` . - Even if working on a Windows family OS the slash and the backslash can be used in the same way, thus the path ``C:\tmp\test.jpg`` is equivalent to the path ``C:/tmp/test.jpg`` . - The drag & drop of an external file into the console generates a string containing the file’s path and the backslashes are doubled automatically. - While you can copy multiple lines of text from the console is allowed only one line paste. .. _toonzscript_specifications: ToonzScript specifications -------------------------- Built on the foundaments of ECMAScript, ToonzScript retains a degree of similarity to the JavaScript syntax but, at the same time, implements some functions and several classes specifically built to make easier the interaction with the Tahoma software architecture. Here follows the list of the ToonzScript specifications: .. _commands: Commands '''''''' - | **print(arg1, arg2, ...)**: writes a message on console. | The print command doesn’t return the ``undefined`` value. Example:: print(“result=”,12*3); - | **run(filename)**: runs the script saved in filename. | The relative path are referred to the folder "/scripts/" Examples:: run(“test.js”) run(“C:\\Users\\Username\\Tests\\another_test.js”) - | **view(image)**: allows to view an image. | The view command can be legally used in the following context: ``view(new Image(filename))`` - | **view(level)**: allows to view a level. Example:: view(new Level(“C:\\Tahoma stuff\\sandbox\\drawings\\A.pli”)) - The variable ``ToonzVersion`` can be used to check the current version of Tahoma. .. _classes: Classes ''''''' For each class, the related constructor, methods and attibutes are listed. .. _file_path: File path ~~~~~~~~~ contains the path of an object. **Constructor:** - new **FilePath**\ (path) **Methods:** - path2 = path.\ **withExtension**\ (e) - path2 = path.\ **withName**\ (name) - path2 = path.\ **withParentDirectory**\ (d) - path2 = path.\ **concat**\ (f) - where f can be a FilePath or a string - files = path.\ **files**\ () - if path is a folder then this method returns the files inside the folder. **Attributes:** - path.\ **extension** - read and write attribute - path.\ **name** - read and write attribute - path.\ **parentDirectory** - read and write attribute - path.\ **lastModified** - read only attribute - path.\ **exists** - read only attribute - path.\ **isDirectory** - read only attribute **Examples**:: myRoot = new FilePath(“C:\\Users\\username\\”); f = myRoot.concat(“tests”).concat(“name.0001.tif”); print(f.extension); // => “tif” print(f.name); // => “name” print(f.parentDirectory); // => “C:\Users\username\tests” if(f.exists) print(“the file “,f,”exists”); else print(“the file “,f,”does not exist”); d = new Date().getTime() - f.lastModified.getTime(); d.setDate(d.getDate()-1); if(f.lastModified>=d) print(“file modified in the last 24 hours); files = myRoot.files(); for(k in files) print(files[k]); // print all files in myRoot .. _image: Image ~~~~~ contains an image, supported types are: tlv, pli or fullcolor. **Constructor:** - new **Image**\ () or new **Image**\ (filename) **Methods:** - img.\ **save**\ (filename) - the file extension has to be compatible with the kind of used level - img.\ **load**\ (filename) **Attributes:** - img.\ **width** - has value 0 if the image is a pli - img.\ **height** - has value 0 if the image is a pli - img.\ **dpi** - has value 0 if the image is a pli - img.\ **type** - accepted values (“Empty”, “Raster”, “ToonzRaster”, “Vector”) **Examples**:: img = new Image(“C:/images/basename.0003.tif”); view(img); print(img.width,img.height,img.dpi); img = new Image(“C:/images/name.pli”); // reads only the first frame. view(img); img.save(“C:/images/name.pli”); .. _level: Level ~~~~~ contains a level, the supported types are: tlv, pli or fullcolor. **Constructor:** - new **Level**\ () or new **Level**\ (filename) **Methods:** - level.\ **load**\ (filename) - level.\ **save**\ (filename) - the file extension has to be compatible with the kind of usedlevel - level.\ **getFrameIds**\ () - lists the names of all the frames - level.\ **getFrame**\ (frameId) - retrive the image of the specified frame - level.\ **getFrameByIndex**\ (index) - gets the frame specified by the index value (first value of index is 0) - level.\ **setFrame**\ (frameId, image) - sets a frame (if the level is not empty its content and the type of image has to be compatible) **Attributes:** - level.\ **name** - is a read and write attribute - level.\ **path** - is a read and write attribute - level.\ **frameCount** - is a read only value - level.\ **type** (“Empty”, ”Raster”, ”ToonzRaster”, ”Vector”) - is a read only value **Examples**:: // This example produces a "renumber" inputLevel= new Image(“C:/images/name.pli”); outputLevel = new Level(); for(i=0;i