Python 从URL混合材料
我需要一个python脚本,它从给定的URL获取一个动态创建的图像文件,并使用该图像文件创建一个材质 然后我会将该材质应用于我的blender对象 下面的python代码适用于本地图像文件Python 从URL混合材料,python,blender,python-3.3,Python,Blender,Python 3.3,我需要一个python脚本,它从给定的URL获取一个动态创建的图像文件,并使用该图像文件创建一个材质 然后我会将该材质应用于我的blender对象 下面的python代码适用于本地图像文件 import bpy, os def run(origin): # Load image file from given path. realpath = os.path.expanduser('D:/color.png') try: img = bpy.data.i
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是否使用Restrictive模块
谢谢你的帮助
只需使用
urllib.urlretrieve(url,localfilename)
然后使用本地文件。您似乎使用的是Python 3.3,它没有cStringIO
。改用io.BytesIO
:
import io
data = io.BytesIO(urllib.urlopen(URL).read())
[编辑]
在osx上的搅拌器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>
输出:
你可以复制像素数据,但我不确定是否值得麻烦。可能它不适用于所有的文件格式
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
你能修好缩进吗?你在windows上吗?->是的,你是。可能有预先包装好的搅拌机。使用什么python版本和什么二进制文件?我在Blender 2.68a中使用python脚本,在Blenders控制台中显示python交互控制台3.3.0(默认,2012年11月26日,17:23:29)[MSC v.1500 32位(英特尔)],但我不知道Blender的python版本。你能做“导入系统”和“打印(系统版本\信息)”吗'从该脚本开始,并像使用普通脚本一样运行它?是否有任何方法可以不进行检索就执行它,因为此任务将是动态的和重复的。我不想保留临时图像文件。没有名为'io.BytesIO'的模块;io不是一个包装,嗯,很奇怪。您可以在该脚本中执行“print(dir(io))”和“print(dir(io.BytesIO))”并运行它。>>>print(dir(urllib))[“内置”、“缓存”、“文档”、“文件”、“初始化”、“加载程序”、“名称”、“包”、“路径”]>>>print(dir(io))回溯(最近一次调用):file“”,第1行,名称错误:名称“io”未定义我在问题末尾添加了搅拌机控制台的屏幕截图。
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