Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Let us say with petals
#1
Rainbow 
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/")
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.

Arrow 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))
Press ENTER to run the TinyScheme function.
;->  Cry
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)
    (theActive)
    (base-layer)
    (flower-layer)
    (petal-layer)
    (petal2-layer)
    (petal3-layer)
    (petal4-layer)
    (petal5-layer)
    (petal6-layer)
    (shade-layer)
    (map-layer)

    (varX1)
    (varY1)
    (varX2)
The initial value is missing for each local variable. Fortunately, they are all integers.

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)
    (theActive 0)
    (base-layer 0)
    (flower-layer 0)
    (petal-layer 0)
    (petal2-layer 0)
    (petal3-layer 0)
    (petal4-layer 0)
    (petal5-layer 0)
    (petal6-layer 0)
    (shade-layer 0)
    (map-layer 0)

    (varX1 0)
    (varY1 0)
    (varX2 0)
12. Repeat steps 1-7
;-> The script draws the pink flower with 8 petals on 500x500 pixels.


.webp   make-flowerPetal8.webp (Size: 6.96 KB / Downloads: 178)

The Layer window shows the following layers:
  • Shading
  • Centre Shading
  • Centre
  • Flower
  • Background
Reply
#2
(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.

Arrow We can call directly make-flower in the Script-Fu console of Gimp 2.10.28 without any registration.

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.
Reply
#3
Lightbulb 
This article is not about registering in Script-fu.

2. Optimizing Script-Fu

[Image: attachment.php?aid=6895]

.webp   make-flowerPetal4.webp (Size: 6.16 KB / Downloads: 290)
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.

.zip   make-flower.zip (Size: 2.67 KB / Downloads: 129)
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))
Press ENTER
;-> 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
However don't do like Gimphried.

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.
    (begin
        (gimp-context-set-pattern "Leather")
        (gimp-edit-bucket-fill layerMap 2 0 100 255 FALSE 0 0) ; Add pattern for bump map purposes
        (gimp-selection-none image)
        (gimp-image-set-active-layer image layerCentre)
        (plug-in-bump-map 1 image layerCentre layerMap 135 45 centreBump 0 0 0 0 1 0 0) ; then apply bump map. 
        (gimp-selection-layer-alpha layerMap)
)    ) ; Remove bump map layer and merge remaining 2 layers.

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.
    (gimp-context-set-pattern "Leather")
    (gimp-edit-bucket-fill layerMap 2 0 100 255 FALSE 0 0) ; Add pattern for bump map purposes
    (gimp-selection-none image)
    (gimp-image-set-active-layer image layerCentre)
    (plug-in-bump-map 1 image layerCentre layerMap 135 45 centreBump 0 0 0 0 1 0 0) ; then apply bump map. 
    (gimp-selection-layer-alpha layerMap)
)    ; Remove bump map layer and merge remaining 2 layers.

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)
with the modern:
Code:
(gimp-drawable-transform-rotate layerNext angle-rotation FALSE side/2 side/2 0 2 FALSE 3 1)
with the following variables that are constant in the context of given input parameters:
Code:
(define side/2 (/ side 2))
(define angle-rotation (/ *pi* 3))

If you run the definition of the angle-rotation constant in the Script-Fu console:
Code:
(define angle-rotation (/ *pi* 3))
angle-rotation
;-> 1.047197551

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*
  (* 4 (atan 1.0))
)
*pi*
;-> 3.141592654

Script-Fu console:
Code:
(make-flower       500  0         1        60        0        5        '(228 163 255)  '(105 10 144)    '(192 225 255))
;-> Let us say it with 6 petals
[Image: attachment.php?aid=6896]

.webp   make-flowerPetal6.webp (Size: 12.58 KB / Downloads: 342)
Regards.
Reply


Forum Jump: