Loading images from disc

I’m trying to create a bunch of blender textures to use as brushes in a script…
Ideally I’ll make this an operator that can be pointed at any directory and load all the images and add then to textures…

in 2.49 I could:


import os 
import bpy 
 
 
directory = "/media/mikedata/Projects/Alphabrushes" 
file = os.listdir(directory) 
 
 
for f in file: 
    img = bpy.data.images.new() 
    img.name = f 
    img.filename = directory + f 
    img.reload 
     
    tex = bpy.data.textures.new() 
    tex.name = f 
    tex.type = 8 
    tex.image = img



but in 2.5 there doesn’t seem to be an equivalent to " bpy.data.images.new"

Also, in 2.49 you could call a file selector as follows:


 import Blender
 from Blender import Window
 #
 def my_callback(filename):                # callback for the FileSelector
   print "You chose the file:", filename   # do something with the chosen file
 #

Window.FileSelector (my_callback, "Choose one!")

what is the equivalent in 2.5?

It seems to me that there are operators for blend files via selector and images via selector…
but I can’t figure out the general case…

From what I can find on a quick glance in the blender console & python documentation [1]

bpy.ops.image.open(path="", filename="", directory="", filter_blender=False,
    filter_image=True, filter_movie=True, filter_python=False, filter_font=False,
    filter_sound=False, filter_text=False, filter_btx=False,
    filter_folder=True, filemode=9)

and

bpy.ops.texture.new()

seem to be what you need here.

Dunno about how exactly to use them (yet) though.

Hope that helps?
If not I could try digging more into this.

Cheers,
Martin

[1] http://www.blender.org/documentation/250PythonDoc/bpy.ops.image.html
http://www.blender.org/documentation/250PythonDoc/bpy.ops.texture.html


import bpy 

directory = "/tmp/"
f = "test1.jpg"

print("Loading file " + f)
bpy.ops.image.open(path=directory+f)
img = bpy.data.images[f]
print(img.name)

tex = bpy.data.textures.new(f)
tex.type = 'IMAGE'
#tex.image = img

If I understand the documentation correctly this should work, but the “tex.image” line doesn’t work for some reason (no “image” attribute in “tex”?)
http://www.blender.org/documentation/250PythonDoc/bpy.types.ImageTexture.html

Martin

Thanks! I seem to have done the same thing in parallel since I read your first post!

I think i need to force an "update"somehow,because a “cloud” texture (the default) doesn’t have an image attribute… eventhough we’ve just changed it to an image type python hasn’t caught up!

which version are you using
and is this to laod a pic for a texture

cause i tried it in windows and get an error
unicode …
i did give the right path i think

#############
imgPath = ‘C:\Users\RJ\0blend25\26363\BF25_SVN_26363\ww2.jpg’

img = bpy.data.add_image(imgPath)
imtex = bpy.data.textures.new('ImageTex')
imtex.type = 'IMAGE' 
imtex = imtex.recast_type()
imtex.image = img

###########
but somehow it does not like the line!

any idea why it refuse to recognise this line for path

Thanks

Ricky, use

img = bpy.ops.image.open(imgPath)

is this another way to do it ?

this is uspposed to work well on Linux
but on windows big joke

i tested it again and still get the same error on the file path
and this is even before the line you’v given me!

File “obadd1.py”, line 8
verts.extend(Vector(0,1,0))
SyntaxError: (unicode error) 'unicodee
n 2-4: truncated \UXXXXXXXX escape

don’t know what i can do to make it work

i can upload the little script it’s vey short and simple if you want
cause don’t know how to pass it here - never been able to do that before!

Thanks

Im wondering if it works on linux and not windows if you need to use ‘/’ for directories on both platforms…

you mean i would have to change all backslash for foward slash !

may this is shown on the API page

but still it should not gie any unicode error i think
cause i entered it in the filed manually
if i had copied it from explorer it might include some unicode charact!

i’ll try to find the API page for this add imag

could not find any of these bpy.data!

but did find the new one
bpy.ops.image.open path="", filename="",

how should i read this is there a coma between the path and the filename?

THanks

That sounds resonable. Right now we simply changed an attribute (“type”) inside the texture object.
Now we need to tell Blender to “change” the object according to this attribute (i think) … which then also has an “image” attribute.
Will try this when I get some time.