import Gimp image (xcf)

I made a script for importing gimp image file (xcf), you can import the flatened image or one image per layer.

You need to install xcftools (http://henning.makholm.net/software), i m afraid its linux only. If someone knows a similar tool for windows or mac i can adapt the script.

#!BPY
"""
Name: 'Gimp (xcf)...'
Blender: 245
Group: 'Import'
Tooltip: 'Import gimp xcf with layers'
"""


__author__= ['Victor Barberan']
__url__ = (' ')
__version__= '0.1'
__bpydoc__= '''
Import Gimp xcf format.
This script requires xcftools (http://henning.makholm.net/software).
'''


# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

import Blender, commands, os
from Blender import *

xcfinfo = 'xcfinfo'
xcf2png = 'xcf2png'

flat = True

#load xcf file 
def load_gimp(xcf_path):
    
    xcf_file = Blender.sys.basename(xcf_path)
    tempdir = Get('tempdir')
    
    if flat:
        #convert flatened xcf 
        xcf_png = tempdir+xcf_file[:-4]+'.png'
        out = commands.getoutput(xcf2png+' "'+xcf_path+'" -T -o "'+xcf_png+'"')
        
        #check if image was generated
        if not os.path.exists(xcf_png):
            Draw.PupMenu('Error importing file...%t| Do you have xcftools instaled??')
            return None
        
        #import image
        newImg = Image.Load(xcf_png)
        newImg.name = xcf_file[:-4]
        newImg.makeCurrent()
        Draw.Redraw()
        if os.path.exists(xcf_png): os.remove(xcf_png)
        
    else:
        #get info of the xcf file
        info = commands.getoutput(xcfinfo+' '+'"'+xcf_path+'"')
        print info
        
        #check if info was generated
        if 'not found' in info:
            Draw.PupMenu('Error importing file...%t| Do you have xcftools instaled??')
            return None
        
        #get number of layers
        layer_num = int(info.splitlines()[0].split(',')[2].strip('layers').strip())
        layer_names = []
        
        #get layer names and put in a list
        for n in range(layer_num):
            names = info.splitlines()[n+1].split(' ')[4:]
            name = ''
            for n in names:
                    name += n+' '
            layer_names.append(name.strip())
            
        #convert every layer to png, and import it
        for layer in layer_names:
                
            #generate  png image
            layerpng = tempdir+layer.strip('"')+'.png'
            out = commands.getoutput(xcf2png+' "'+xcf_path+'" "'+layer+'" -T -o "'+layerpng+'"')
    
            #check if image was generated
            if not os.path.exists(layerpng):
                Draw.PupMenu('Error importing file...%t| Do you have xcftools instaled??')
                return None
            
            #import image
            newImg = Image.Load(layerpng)
            newImg.name = xcf_file+': '+layer
            if os.path.exists(layerpng): os.remove(layerpng)
        
        newImg.makeCurrent()
        Draw.Redraw()
            
            
if __name__=='__main__':

    ask = Draw.PupMenu('How do you like to import the Gimp file?%t|    Flatened layers %x1|    One image per layer%x2')
    if ask == 1:
        flat = True
        Window.FileSelector(load_gimp, 'Import Gimp', '*.xcf')
    elif ask == 2:
        flat = False
        Window.FileSelector(load_gimp, 'Import Gimp', '*.xcf')

Copy the script to your blender scripts directory, and it should appear in the file->import menu.

Hope someone finds it useful.

sorry for my bad english.

vico

sounds great, it would be amazing to combine the multi layer import with the cutout 2d script - and if it could be made osx/windows

Cool idea. Fits right into my work flow and saves steps. Thanks a bunch.

why is it only linux?

thanks all for your comments.

@krupa: when i have a little spare time i will look into the 2d cutout script to see how integrate them both.

Isidore: because the program xcftools is only linux, and is used to convert the xcf to png for importing, i will try with imagemagick (linux, win and mac compatible). I will post results here.

vico

Marvelous, itd be great if imagemagick worked as this workflow would be great to use…its something that you do all the time in after effects from photoshop and an opensource alternative is very useful!

Nice work! Please take a look at copyright part of your license block. :wink:

Nice work! Please take a look at copyright part of your license block.

uups, the problem of copy-paste… :slight_smile:
edited…

Made a script for 2d cutout from gimp layers:

http://blenderartists.org/forum/showthread.php?t=114409

Hi! Thank you for this topic - it helped me to find xcftools, now i can use GIMP for level design with xcfinfo.

I compiled xcftools for mac, so you can copy it in /usr/bin and vico’s script should work.
You can copy it with “sudo cp” command in terminal, i. e.:
sudo cp /xcfinfo /usr/bin/xcfinfo

Here’s binary for mac:
http://dl.getdropbox.com/u/1765875/xcf2png
http://dl.getdropbox.com/u/1765875/xcf2pnm
http://dl.getdropbox.com/u/1765875/xcfinfo

Maybe you need to make it executable - chmod should help. i.e.:
chmod 755 /xcf2png
chmod 755 /xcf2pnm
chmod 755 /xcfinfo

It’s possible to build xcftools for windows, but i have problems with cygwin and mingw… =(

If my post helped you - follow my game twitter: http://twitter.com/iTraceur
I will very appreciate if you retweet something, promote it somehow or else…

sorry for bumping…
so, nobody can get this script to run on win? i’m afraid that i’m very stupid to make my own compilation of xcftools for win… :frowning:

no windows version yet ? :expressionless:

Hey, I’ve been looking for xcftools for Mac for a while and this is the only source that has it. The only problem is that the Dropbox links are not working! iTraceur, can you please update the links so they work! Thanks!

Is there anything changed since the current version of Blender is now at 2.61?
BTW, why isn’t this supported directly from in Blender itself?

EDIT:
It is available under the add ons:
Ctrl + Alt + U > Add Ons > Search for: GIMP

It is depending on XCFTOOLS to be installed,
so make sure to install the XCFTOOLS;
for Ubuntu:
Ubuntu Software Center > search for: XCFTOOLS
install…

Then activate the Blender add on as described above.

Isn’t this already an addon?
Pardon my ignorance.

:slight_smile: Hey, yes it is, I did edit this in, but didn’t see your reply yet. (didn’t refresh the page, didn’t expect a reply so soon) :wink:

It is depending on the XCF Tools, which explains why it isn’t enabled by default.

=======================
The default format for GIMP images is: XCF.
You can’t import XCF images into Blender by default.
But there is an add on available in Blender, which needs to be enabled.
Before doing this, you need to install the XCF TOOLS.

Blender Add On
It is available in Blender under the add ons:

Ctrl + Alt + U > Add Ons > Search for: GIMP
Or:
File > User Preferences > Add Ons > Search for: GIMP

It is depending on XCFTOOLS to be installed,
so make sure to install the XCFTOOLS;

XCF Tools
Example of how to install the XCF Tools for Ubuntu:

Ubuntu Software Center > search for: XCFTOOLS
install

Then activate the Blender add on as described above.

Video Example
Need more help?
Here is a youtube video showing you how to enable the add on in Blender and install XCF Tools (Linux example).
[video]www.youtube.com/watch?v=fpi9gNjxWzM[/video]