__author__ = 'thoth'
# http://blender.stackexchange.com/questions/53947/how-to-import-image-data-into-blender-from-file-importer-script/53991
import bpy
def image_for(fname):
for img in bpy.data.images:
if img.filepath==fname:
return img;
return bpy.data.images.load(fname)
def node_of_type(nodes, type):
for node in nodes:
if node.type == type:
return node
return None
def mission1(fname):
mat = bpy.data.materials.get("material")
if mat is None:
mat = bpy.data.materials.new("material")
mat.use_nodes = True
nodes = mat.node_tree.nodes
n1 = node_of_type(nodes, "TEX_IMAGE")
if n1 is None:
n1 = nodes.new("ShaderNodeTexImage")
n1.location = (-200,0)
n1.image = image_for(fname)
bsdf = node_of_type(nodes, 'BSDF_DIFFUSE')
mat.node_tree.links.new(bsdf.inputs[0], n1.outputs[0])
coord = node_of_type(nodes, 'TEX_COORD')
if coord is None:
coord = nodes.new('ShaderNodeTexCoord')
coord.location = (-400,0)
mat.node_tree.links.new(n1.inputs['Vector'], coord.outputs['UV'])
return mat
mat = mission1("/home/thoth/art/dancing-bear/bear384.png")
bpy.context.active_object.data.materials[0] = mat
|
Blender python API quick-start
Syntax highlighting by Pygments.