Brendan
Brendan

Reputation: 73

calling a gimp-fu function from an external module

I'm trying to write a sort of wrapper library for GIMP to make my generative art projects easier, but i'm having a problem interfacing with gimpfu from a my wrapper module. The following plugin code runs fine, and displays an image with horizontal lines drawn across it:

from gimpfu import *
from basicObjects import *

def newFilt() :
    img = gimp.Image(500, 500, RGB)
    background = gimp.Layer(img, "Background", 500, 500,RGB_IMAGE, 100, NORMAL_MODE)
    img.add_layer(background, 1)
    background.fill(BACKGROUND_FILL)
    pdb.gimp_context_set_brush('1. Pixel')
    pdb.gimp_context_set_brush_size(2)
    for i in range(100):
        Line= line([(0,5*i),(500,5*i)])
        pdb.gimp_pencil(background,Line.pointcount,Line.printpoints())
    gimp.Display(img)
    gimp.displays_flush()

register(
    "python_fu_render",
    "new Image",
    "Filters",
    "Brendan",
    "Brendan",
    "2016",
    "Render",
    "",
    [],
    [],
    newFilt, menu="<Image>/File/Create")

main()

the 'line' class is defined in basicObjects, and is functioning as expected, however if I try to replace 'pdb.gimp_pencil(background,Line.pointcount,Line.printpoints())' with 'Line.draw(background)', and add a draw() function to the line class, as such:

from gimfu import *
class line:
    """creates a line object and defines functions specific to lines """
    def __init__(self, points):
        self.points = points
        self.pointcount = len(points)*2
    def printpoints(self):
        """converts point array in form [(x1,y1),(x2,y1)] to [x1,y1,x2,y2] as nessecary for gimp pdb calls"""
        output=[]
        for point in self.points:
            output.append(point[0])
            output.append(point[1])
        return output
    def draw(self,layer):
        pdb.gimp_pencil(layer,self.pointcount,self.printpoints())

the image is not rendered, and there is no messages displayed in the gimp error console.How can I make a pdb call from an external file? Would making the wrapper a separate plug-in help somehow?

Upvotes: 2

Views: 953

Answers (3)

Eric H.
Eric H.

Reputation: 2242

Yes, it would be fine to execute a python-fu script stored anywhere on the disk, probably using PYTHONPATH and DYLD_LIBRARY_PATH.

Upvotes: 0

Ismael Harun
Ismael Harun

Reputation: 147

Yes as already pointed out the gimpfu and gimp module only work from within the gimp application, or with a lot of hacking to create a duplicate environment, It would be so cool to run gimp extensions and plugins from a script without the gimp application overhead but that's basically the being able to have and eat your cake at the same time situation. If you don't need all the filters and effects that are bountifully available from gimp you might consider the PIL / Pillow modules. They don't reach the capability of gimp but has all the basic features for graphic image manipulation. Those run fine in python2 or 3 and are so much faster then gimp.

Upvotes: 1

jsbueno
jsbueno

Reputation: 110456

First: The gimp and gimp-fu module only work when the Python script runs as a plug-in, from within GIMP. I don't know what you are calling "external file" - but the entry point has always to be a plug-in script. It can them import other Python modules as any normal program.

Second: GIMP plug-ins runs are Python 2.x (2.7 in these days) - and therefore any declared class should inherit from object - declaring a class without inheriting from object as you do will only bring you unexpected problems - although that may not be your problem right now.

The class declaration looks alright, but your example on calling it does not - Line.draw(background) seems to indicate you are trying to call the method on the class itself, not on an instance of your line class.

Upvotes: 1

Related Questions