Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Fu - mkdir not working
#1
Hi all, I've got another Python-Fu question. I'm trying to set up a script for exporting a file. As part of that, I want to create a new folder for the output if it doesn't exist. Here's the code:

Code:
#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os
from gimpfu import*

def folderCheck(filePath, newFolder):
   filePath.append(newFolder)
   outFolder = "/".join(filePath)
   outFolderExists = os.path.exists(outFolder)
   if outFolderExists == False:
       os.mkdir(outFolder)
   return outFolder

def exportPNG8(image, layer):
   filePath = pdb.gimp_image_get_filename(image)
   filePathSplit = filePath.split("/")
   fileName = filePathSplit[-1]
   outFolder = folderCheck(filePathSplit[0:-1], "PNG8")

register(
   "python_fu_marvelmods_export_png8",
   "Exports a texture to PNG8 format.",
   "Exports a texture to PNG8 format.",
   "BaconWizard17",
   "BaconWizard17",
   "January 2023",
   "Export as PNG8",
   "*",
   [
       (PF_IMAGE, "image", "Input image", None),
       (PF_DRAWABLE, 'drawable', 'Layer, mask or channel', None)
   ],
   [],
   exportPNG8,
   menu='<Image>/Marvel Mods/Export Textures/By Texture Format'
)

main()

Everything here is functional except for the part that creates the new folder. I don't have a function for exporting the image right now. My plan was to work on that after I got the folder creation part figured out. I tested the folderCheck function outside of GIMP in regular Python (with some expected inputs), and it worked completely fine and created the expected folder. Here's that test code for comparison, which worked flawlessly:

Code:
import os

def folderCheck(filePath, newFolder):
   filePath.append(newFolder)
   outFolder = "/".join(filePath)
   outFolderExists = os.path.exists(outFolder)
   if outFolderExists == False:
       os.mkdir(outFolder)
   return outFolder

filePath = "C:/Users/ethan/Desktop/Test.xcf"
filePathSplit = filePath.split("/")
fileName = filePathSplit[-1]
outFolder = folderCheck(filePathSplit[0:-1], "PNG8")
 
Does GIMP just not have permissions to create a new folder, does this function not work in GIMP, or is there something else going on here?

Edit: Something I thought of: Is creating the folder even necessary? Or will GIMP automatically create a folder if it doesn't exist for export?
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply


Messages In This Thread
Python Fu - mkdir not working - by BaconWizard17 - 01-27-2023, 03:52 AM
RE: Python Fu - mkdir not working - by gasMask - 01-27-2023, 07:50 AM
RE: Python Fu - mkdir not working - by Kevin - 01-27-2023, 09:17 AM
RE: Python Fu - mkdir not working - by Ofnuts - 01-27-2023, 10:06 AM
RE: Python Fu - mkdir not working - by Kevin - 01-31-2023, 10:12 AM

Forum Jump: