Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get a layer ID by its position ? [Script-Fu]
#4
(07-22-2024, 04:05 AM)Nival Wrote: Oh thanks a lot !!
I'm beginner in Script-fu (Scheme) and carefully studied GIMP doc, and in the tutorial about the lists I see no mention of this function ! Big Grin 
They only speak about the use of car / cdr and compounds ... Angry (Which seemed so strange and absurdly complex...)


Quote:3.3.8. Accessing Other Elements In A List

OK, great, we can get the first element in a list, as well as the rest of the list, but how do we access the second, third or other elements of a list? There exist several "convenience" functions to access, for example, the head of the head of the tail of a list (caadr), the tail of the tail of a list (cddr), etc.

The basic naming convention is easy: The a's and d's represent the heads and tails of lists, so

Code:
(car (cdr (car x) ) )


could be written as:

Code:
(cadar x)


To get some practice with list-accessing functions, try typing in the following (except all on one line if you're using the console); use different variations of car and cdr to access the different elements of the list:

     
Code:
 (let* (
                (x  '( (1 2 (3 4 5) 6)  7  8  (9 10) )
                )
             )
             ; place your car/cdr code here
       )

     

Try accessing the number 3 in the list using only two function calls. If you can do that, you're on your way to becoming a Script-Fu Master!

Angry

So ... thanks again, all becomes so easy now ! ^^

Note : I really need that the script automatically use other layers ... it's part of the main purpose of my script (purpose of automating tasks, you know Tongue)

Script-fu is a subset of the Scheme language (itself a dialect of the Lisp language), so if you can google Scheme questions and answers. I found  list-ref by googling "scheme get nth item in list".

The car/cdr functions are useful and still make sense because you very often want to detatch the first element of the list (car) from the rest (cdr). In particular algorithm are often specified recursively and when processing a list you can replaced the iteration of the list by a function that processed the first item of a list and then call itself on the rest of the list. function to processing a list

Now of course you can enter the 3rd millenium and use Python for your Gimp scripts.
Reply


Messages In This Thread
RE: How can I get a layer ID by its position ? [Script-Fu] - by Ofnuts - 07-22-2024, 06:34 AM

Forum Jump: