I'm writing a python script to create some simple objects in Blender. Outside of the script I've created UV maps (exported as PNGs) from objects with the exact same geometry as the objects . How can I, using Python, import those PNG file UV maps?
What I'm trying to accomplish is to create an image texture for the objects that I create via script. I'm using Cycles.
EDIT: Here's my exported UV File
Here's a code snippet of what I'm doing:
import bpy
# Use Cycles
bpy.data.scenes["Scene"].render.engine = 'CYCLES'
# Create Sphere
bpy.ops.mesh.primitive_uv_sphere_add(size=2, location=(0, 0, 3), segments=64, ring_count=32)
sphere01 = bpy.context.active_object
# Create new material
mat = bpy.data.materials.new(name="Test Material")
mat.use_nodes = True
# Assign Test Material to sphere
sphere01.data.materials.append(mat)
# Create the image texture node
try:
img = bpy.data.images.load("//somefile.jpg", check_existing=False)
except NameError:
print("Cannot load image %s" % "somefile.jpg")
img_node = mat.node_tree.nodes.new('ShaderNodeTexImage')
img_node.image = img
# Link the image texture node
mat.node_tree.links.new(mat.node_tree.nodes.get('Diffuse BSDF').inputs.get("Color"), img_node.outputs.get("Color"))
# ---------------------------------------------------------
# SOMEHOW IMPORT THE FILE UV_MAP.png AS A UV MAP
# ---------------------------------------------------------
# Save
bpy.ops.wm.save_as_mainfile(filepath="./testing.blend")