![]() ![]() Txt = "Without templating" text (txt, 20, 30, 200 ) fill ( 0.2 ) font ( "Dolly-Roman", 14 ) Notice its repetitiveness.įill ( 1, 0, 0.25 ) font ( "Dolly-Bold", 14 ) The script with plain top-to-bottom scripting. Moreover, changing the header color or the horizontal margin is much easier in the second script because we have to do it only once: by changing the code in the header() definition or by changing the value of the x variable. then you would have to define fonts and colors over and over again, and probably end up feeling like a robot trying to explain something to another robot. The first script is in fact less code, but imagine it would draw not two but twenty paragraphs. In the example below, we put two identical scripts next to each other, one using plain top-to-bottom scripting and one using templates. The return statement always comes last inside the definition. This can be any number of things, a number, a string, a list. The return statement inside a definition returns a value. ![]() def paragraph (txt, x, y, w ):įill ( 0.2 ) font ( "Dolly-Roman", 14 ) text (txt, x, y, w ) return textheight (txt, w ) ![]() For example, textheight(txt, w) returns the height of a paragraph displaying txt with a width of w. You may have noticed that some commands in NodeBox have a return value that you can store in a variable. NodeBox would now execute all of the code in the header() definition, with txt being "Templating", x being 20, and y being 40. Once the command is called, you supply real values for txt, x and y. Theses are used like variables inside the definition. This is where parameters come in: notice how the definition of header() uses a txt, a x, and a y parameter. Command definitions need to be called from somewhere else in the script. Ĭommand definitions are not executed by themselves: you are instead teaching NodeBox something, for example, what a header is. Commands that change and return a value use a verb: adjust_contrast, strip_tags.Commands that return True or False start with is: is_white, is_alive, is_big.Use lower_case_with_underscores (except for essential commands like imagesize). ![]() Use a simple and relevant word that describes what the command does.Don't forget the colon at the end of the line - this tells NodeBox that the intented statements below are what the command does. The definition of a command starts with def, followed by the name of the command (pick a relevant name), and a list of parameters. If we want to modify the way a flower is drawn, we only need to modify the code at one location.ĭefining your own commands is like composing your own toolbox, with a set of tools that you can apply to your creations over and over.Īs a simple example I've defined my own header() command below: def header (txt, x, y ):įill ( 1, 0, 0 ) font ( "Dolly-Bold", 18 ) text (txt, x, y ) We only need to define the actions to draw a flower once. This command can then be called one time, a hundred times, or a thousand times. Creating a command is a design strategy in which you re-use certain actions or pieces of your script.įor example, in the above example all the actions to draw a flower (ten red-pinkish ovals connected by lines) are grouped in a handy flower() command. Aside from the predefined commands in the NodeBox reference you can also define your own custom commands. ![]()
0 Comments
Leave a Reply. |