I need a python script which gets a dynamically created image file from given URL and create a material with that image file.
Then I will apply that material to my blender object.
The python code below works for local image files
import bpy, os
def run(origin):
# Load image file from given path.
realpath = os.path.expanduser('D:/color.png')
try:
img = bpy.data.images.load(realpath)
except:
raise NameError("Cannot load image %s" % realpath)
# Create image texture from image
cTex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
cTex.image = img
# Create material
mat = bpy.data.materials.new('TexMat')
# Add texture slot for color texture
mtex = mat.texture_slots.add()
mtex.texture = cTex
# Create new cube
bpy.ops.mesh.primitive_cube_add(location=origin)
# Add material to created cube
ob = bpy.context.object
me = ob.data
me.materials.append(mat)
return
run((0,0,0))
I tried :
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
But I had no luck with it. First error I got was
ImportError: No module named 'StringIO'
Does python scripting API in Blender use restirictive Modules or what?
Thank you for your help.