[英]Blender material from URL
我需要一个python脚本,它从给定的URL获取动态创建的图像文件,并使用该图像文件创建一个材料。
然后我会将那些材料应用到我的搅拌器对象上。
下面的python代码适用于本地图像文件
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))
我试过了 :
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
但我没有运气。 我得到的第一个错误是
ImportError:没有名为'StringIO'的模块
Blender中的python脚本API是使用restirictive Modules还是什么?
谢谢您的帮助。
你似乎使用Python 3.3,它没有cStringIO
。 请改用io.BytesIO
:
import io
data = io.BytesIO(urllib.urlopen(URL).read())
[编辑]
在osx上的Blender 2.68a中测试:
import io
from urllib import request
data = io.BytesIO(request.urlopen("http://careers.stackoverflow.com/jobs?a=288").read())
data
>>>> <_io.BytesIO object at 0x11050aae0>
[EDIT2]
好吧,似乎搅拌机只能从文件加载。 以下是对脚本的修改,用于下载URL,将其存储在临时位置,从中创建材料,将材料打包在混合文件中并删除临时图像。
从urllib导入请求导入bpy,os,io
def run(origin):
# Load image file from url.
try:
#make a temp filename that is valid on your machine
tmp_filename = "/tmp/temp.png"
#fetch the image in this file
request.urlretrieve("https://www.google.com/images/srpr/logo4w.png", tmp_filename)
#create a blender datablock of it
img = bpy.data.images.load(tmp_filename)
#pack the image in the blender file so...
img.pack()
#...we can delete the temp image
os.remove(tmp_filename)
except Exception as e:
raise NameError("Cannot load image: {0}".format(e))
# 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)
run((0,0,0))
输出:
只需使用urllib.urlretrieve(url, localfilename)
然后使用本地文件。
你可以复制像素数据,但我不确定这是否值得。 可能它不适用于所有文件格式。
import bpy, io, requests, PIL
def loadImageFromUrl(url,name=None):
frames = ()
# load image
image = PIL.Image.open(io.BytesIO(requests.get(url).content))
try:
while True:
# create new blender image of correct size
frame = bpy.data.images.new(name or url, image.width, image.height)
# copy the pixel data. apparently the lines have to be flipped.
frame.pixels = [
value / 255
for pixel in image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
.convert('RGBA')
.getdata()
for value in pixel
]
frames += frame,
image.seek(len(frames))
except EOFError:
return frames
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.