Re: pdb.gimp_get_item_by_id(), or ids and layers and layer groups

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Quoting Javier Candeira <javier@xxxxxxxxxxxx>:

> On Mon, Feb 7, 2011 at 2:30 AM,
> <saulgoode@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>> The PDB functions already exist for handling layer groups, and
>> Script-fu does not have any problem making use of these functions. The
>> Python-fu extension has not yet been updated to handle groups (there
>> is no 'group' class).
>
> Do you mean that the Python-fu extension doesn't yet have access to
> the proper script-fu functions from pdb?

Not exactly. In Python, a "layer" is a reference to a data structure  
(in C terminology, a pointer to a struct). One of the fields is the  
layer's ID number. The PDB only deals with these ID numbers, and  
Python doesn't provide a direct way to handle IDs. The only thing you  
could do is get a list of all layers/groups (which would be a list of  
pointers to structures) and search that list for the structure  
containing the appropriate ID. Since the 'gimp-image-get-layers'  
procedure now only returns the IDs of the top-level layers/groups,  
there is no way to perform such a search. At some point, this will be  
remedied but to my knowledge there is currently no way to do what you  
want with Python.

Script-fu does not encounter this problem because there is no  
Script-fu data structure for a layer -- a layer is identified by the  
same ID number that the PDB uses. It does not matter that the "layer"  
IDs returned by 'gimp-image-get-layers' might actually be "group" IDs,  
to Script-fu it is just a number. Script-fu can pass that same number  
back to the PDB and the procedure will know what to do with it (for  
example, a call to 'gimp-item-is-group ID-number' will inform  
Script-fu that the ID belongs to a group object).

It is up to the script programmer to make sure that he passes the  
proper IDs to procedures -- there is no "type safety" for these GIMP  
objects. For example, if a layer's ID is passed to  
'gimp-item-get-children' then a run-time error will occur because that  
procedure is expecting a group object's ID number.

> If you can do this via script-fu, do you mind providing me with the
> calls, so I can try and call them from Python?

The following script will save your tree to the specified directory  
using the flat naming approach I described earlier. I did not attempt  
to get too fancy soas to keep the code simple. You will probably wish  
to base your filename on the image's name, rather than the directory  
name as I have done (be sure to handle the possibility of an  
"Untitled" image).

If you need further assistance, I recommend that you post to the GIMP  
Users Group forum ( http://gug.criticalhit.dk/viewforum.php?f=8 ). I  
visit there daily.

(define (script-fu-sg-save-tree image dir-name)
   (define (process-items items prefix)
     (set! prefix (string-append prefix "++"))
     (while (not (null? items))
       (if (zero? (car (gimp-item-is-group (car items))))
         (let ((filename (string-append prefix (car  
(gimp-item-get-name (car items))))))
           (file-png-save-defaults RUN-NONINTERACTIVE image (car  
items) filename filename)
           (set! items (cdr items)) )
         (begin
           (process-items (vector->list (cadr (gimp-item-get-children  
(car items))))
                          (string-append prefix (car  
(gimp-item-get-name (car items)))) )
           (set! items (cdr items)) ))))
   (process-items
       (vector->list
          (cadr (gimp-image-get-layers image)))
       (string-append
           dir-name
           DIR-SEPARATOR
           (unbreakupstr (last (strbreakup dir-name DIR-SEPARATOR))  
DIR-SEPARATOR) )))

(script-fu-register "script-fu-sg-save-tree"
   "Save image tree..."
   "Save each layer as a PNG retaining group information"
   "saulgoode"
   "saulgoode"
   "February 2011"
   "*"
   SF-IMAGE   "The image"       0
   SF-DIRNAME "Image Directory" "/home/saul"
   )

(script-fu-menu-register "script-fu-sg-save-tree"
                          "<Image>/File/Save")





_______________________________________________
Gimp-developer mailing list
Gimp-developer@xxxxxxxxxxxxxxxxxxxxxx
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-developer


[Index of Archives]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [GIMP for Windows]     [KDE]     [GEGL]     [Gimp's Home]     [Gimp on GUI]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux