4

指定された URL から動的に作成された画像ファイルを取得し、その画像ファイルでマテリアルを作成するPython スクリプトが必要です。

次に、そのマテリアルをブレンダー オブジェクトに適用します。

以下の 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 を使用しているようですcStringIOio.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 に答える