file operations


(Bart) #1

is there a way for:

  • creating subfolder in export directory
  • copy a image from their source directory to the export directory
  • converting images (may from TGA to JPEG)?

Thanks!


(z3r0 d) #2

yes

yes

that’s not a file operation

[but if you’ve installed the proper modules, there is a way to do that too]

python has documentation too… search it!!!

[helpful places to start looking for answers to your questions]
http://python.org/doc/2.4.2/lib/module-shutil.html
http://python.org/doc/2.4.2/lib/module-os.path.html
http://python.org/doc/2.4.2/lib/module-os.html

btw, [email protected] is someone that has been significant in the blender community for some time… like, longer than me…


(ascotan) #3
  • creating subfolder in export directory

import os
os.mkdir(path)

  • copy a image from their source directory to the export directory

import shutil
shutil.copyfile(src, dst)

  • converting images (may from TGA to JPEG)?

Check out the PIL (python imaging library)
You might be able to call the pil tool pilconvert like this:
pilconvert mypic.tga mypic.jpeg
However users would need to install PIL and python to run a script that would include this.


(Bart) #4

Thanx for the answers, i made a mistake: my question is about using just Blender without a required Python installation.

So is there a way?


(jms) #5

#-------------------------------
# a piece of the povanim's script
#-------------------------------
import Blender
import sys

# --- to avoid to load the os module
try:
  import nt
  os=nt
  os.sep='\\'
except:
  import posix
  os=posix
  os.sep='/' 
#...

Stolen in the shutil.py :


def copyfileobj(fsrc, fdst, length=16*1024):
    """copy data from file-like object fsrc to file-like object fdst"""
    while 1:
        buf = fsrc.read(length)
        if not buf:
            break
        fdst.write(buf)

def copyfile(src, dst):
    """Copy data from src to dst"""
    if _samefile(src, dst):
        raise Error, "`%s` and `%s` are the same file" % (src, dst)

    fsrc = None
    fdst = None
    try:
        fsrc = open(src, 'rb')
        fdst = open(dst, 'wb')
        copyfileobj(fsrc, fdst)
    finally:
        if fdst:
            fdst.close()
        if fsrc:
            fsrc.close()


(Bart) #6

Ok, got anything working except converting images. Just one strange thing:
I can create a directory with mkdir but i can’t copy files into it always getting errno13 permission denied (Win2k and WinXP).
As i read directories created with 777 permissions. Is there a work around for that?
Why i can write an exported files anywhere, create a folder anwhere but can’t copy a file into it?


(Bart) #7

Ha, as iread it is a typical Windows-Python problem :frowning: but i found a work around using os.system(command):

def fileop(self):
DrawProgressBar (self.bar, “Copy Textures…”)
parDir = sys.dirname(sys.expandpath(self.filename))
parDir = sys.makename(parDir,sys.sep)
newDir = sys.makename(parDir,texDir)
try:
os.mkdir(newDir)
except:
print""
allFiles = len(self.fileopList)
if sys.sep == “\”:
for i in range(allFiles):
command = ‘copy %s %s /Y’ % (self.fileopList[i], newDir)
os.system(command)
else:
for i in range(allFiles):
self.copyfile(self.fileopList[i],newDir)

Works perfect!