Let us say with petals - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Let us say with petals (/Thread-Let-us-say-with-petals) |
Let us say with petals - Gimphried - 10-21-2021 1. Let us say with petals Make Flower has been written in Script-Fu by Daniel Bates in 2007. You can download the free open source Flower_Generator_Script_by_Insanity_Prevails.scm in the SamRocketMan's github reposity. Open the .scm script in the free editor Notepad++. script-fu-register in line 316 has strange NUL characters. Into the bargain, nowadays, the registration is in two steps. We need to indicate the Gimp menu to call this script in an additional call: Code: (script-fu-menu-register "script-fu-make-flower" "<Image>/Filters/Render/Make Flower/") We can call directly make-flower in the Script-Fu console of Gimp 2.10.28 without any registration. 1. In Notepad++, click in the beginning of the line 12 before the opening parenthesis without selecting anything. 2. Press the Notepad++ shortcut Ctrl+Alt+B. Notepad++ automatically selects the entire function make-flower from line 12 to line 312. This means that of course the parenthesis are well balanced. 3. Press Ctrl+C in Windows to copy the function in the clipboard. 4. Run Gimp. 5. Gimp menu Filters > Script-Fu > Console ;-> "Welcome to TinyScheme" appears in the top of the Script-Fu console. 6. Paste the content of the clipboard in the input area of the Script-Fu console above the Help button. Press ENTER ;-> Script-Fu replies by the name of the function: make-flower This means that there is not any errors of syntax! 7. Copy in the clipboard by Ctrl+C the following call of the function with all parameters: Code: (make-flower 500 2 2 30 1 5 '(230 120 210) '(170 50 150) '(170 170 50)) ;-> Oops: Code: Error: Bad syntax of binding spec in let* : ((theImage) (theActive) (base-layer) (flower-layer) (petal-layer) (petal2-layer) (petal3-layer) (petal4-layer) (petal5-layer) (petal6-layer) (shade-layer) (map-layer) (varX1) (varY1) (varX2)) 8. In Notepad++, select the Let*'s local variables from line 26 to 46: Code: (theImage) 10. To fix the issue, Notepad++ menu Search > Replace... 11. In the dlgbox "Replace", 2nd tab "Replace", Find what: ")" Enter the closing parenthesis without the double quotes. Replace with: " 0)" Enter a space separator before the initial value, then the initial value 0 and the closing parenthesis. Do not enter the double quotes. Check the option [x] in selection Press the Replace All button The expected result must be: Code: (theImage 0) ;-> The script draws the pink flower with 8 petals on 500x500 pixels. [attachment=6889] The Layer window shows the following layers:
RE: Let us say with petals - Ofnuts - 10-21-2021 (10-21-2021, 09:15 PM)Gimphried Wrote: However don't do it! We do not need to overload the Gimp Graphic User Interface (GUI) with additional menus that are not used daily. Lots of trouble... There are ways to hide/un-hide scripts on the fly, so that they don't clutter your menus and can still be used with a GUI. Optimizing Script-Fu - Gimphried - 10-22-2021 This article is not about registering in Script-fu. 2. Optimizing Script-Fu [attachment=6895] Since the flower is drawn in 1 s 196 ms 518 µs according to the CPU, the purpose of this chapter is not to optimize the speed. It is enough fast for an interpreted language. We wish to reduce the number of lines in order to better understand the architecture of the program and how to draw such a flower. Download the supplied make-flower.zip 3 Kb. [attachment=6894] Unzip the new make-flower.scm 9 Kb. The number of lines has been optimized to 130 lines thanks to 8 nested defines inside the main define. There are 23 lines of comment so 17.6% and one blank line. Script-Fu console: Code: (make-flower 500 1 0 60 0 5 '(130 65 66) '( 24 86 46) '( 9 29 16)) ;-> The new version draws another flower with 4 petals. Click the Browse button at the right of the input area of the Script-Fu console Search: "gimp-image-add-layer" The PDB announces: "Deprecated: Use 'gimp-image-insert-layer' instead." So the new version replaces the legacy: Code: (gimp-image-add-layer theImage base-layer 0) with parent=0: Code: (gimp-image-insert-layer image layer 0 -1) Contrary to the original version, pay attention that the new version introduces the alignement of parenthesis --typical of the Gimphried's style--. We are using the full length of modern screen with in line 74 the longest line of code with its comment ended at the 165th character. We introduce the case statement. For the purpose of the exercice of the optimization of the number of lines, Gimphried resumed the entire case statement on only one line: Code: (case petalDir ((0) (set! varX1 8) (set! varY1 4)) ((1) (set! varX1 4) (set! varY1 8)) ((2) (set! varX1 8) (set! varY1 8))) ; Cloud Layer X and Y The optimization of if + begin by and with multiple parameters could be more relevant: Since there is more than one statement in the then part, we need to group them inside a begin statement: Code: (if (> centreBump 0) ; Only bumpmap if the Centre Bump setting was not at zero. Ignore if set to zero. The above if snippet can be optimized with the logical operateur and accepting an unlimited number of parameters if and only if each statement of the then part --regrouped inside the begin-- never fails! Code: (and (> centreBump 0) ; Only bumpmap if the Centre Bump setting was not at zero. Ignore if set to zero. The new version is more readable reducing the number of hard-coded constants: Compare the legacy: Code: (gimp-drawable-transform-rotate petal-layer 1.05 FALSE (/ measure 2) (/ measure 2) 0 2 FALSE 3 1) Code: (gimp-drawable-transform-rotate layerNext angle-rotation FALSE side/2 side/2 0 2 FALSE 3 1) Code: (define side/2 (/ side 2)) If you run the definition of the angle-rotation constant in the Script-Fu console: Code: (define angle-rotation (/ *pi* 3)) We introduce the convention of the green arrow comment meaning that Script-Fu returns the result after this arrow. We retrieve the legacy angle 1.05 in radian since the global constant *pi* is defined in line 350 of C:\Program Files\GIMP 2\share\gimp\2.0\scripts\script-fu-compat.init Code: (define *pi* Script-Fu console: Code: (make-flower 500 0 1 60 0 5 '(228 163 255) '(105 10 144) '(192 225 255)) [attachment=6896] Regards. |