4

我需要一个 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 使用限制性模块还是什么?

谢谢您的帮助。

在此处输入图像描述

4

3 回答 3

3

您似乎使用 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>

[编辑2]

好的,似乎搅拌机只能从文件加载。这是您的脚本的修改,它下载一个 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))

输出: 在此处输入图像描述

于 2013-09-29T09:13:30.687 回答
1

只需使用urllib.urlretrieve(url, localfilename)然后使用本地文件。

于 2013-09-29T08:52:52.830 回答
0

您可以复制像素数据,但我不确定这是否值得。可能它不适用于所有文件格式。

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
于 2017-02-03T00:57:59.577 回答