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 FileUV Map PNG

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

# Create the image texture node
    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"))

# ---------------------------------------------------------


# ---------------------------------------------------------

# Save
  • $\begingroup$ Could you please add a pic of your png UV map to the post. $\endgroup$ – batFINGER Mar 28 '18 at 18:28
  • $\begingroup$ Updated with pic of the PNG map. $\endgroup$ – Ian Vera Mar 28 '18 at 19:22
  • $\begingroup$ Ian, if I understand well, UV maps saved as png cannot be used as real UV maps. They are just some guide to see how a texture can correspond to a concrete UV map. So (again if I understand well), this approach can not work. Please ask in comments if this is not clear. $\endgroup$ – lemon Mar 28 '18 at 19:29
  • 1
    $\begingroup$ Ian no, I cannot provide a full answer for that (too much time to do it). But UV maps are accessible via obj.data.uv_layers where obj is the target or original object (say this is called uvMap). From that use the polygons of the object (obj.data.polygons) and get for each polygon "polygon.loop_indices" so you can reach uvMap.data[Index].uv which is the uv data which corresponds to x and y value (uvMap.data[index].x or y). Here is an answer using uv maps: blender.stackexchange.com/questions/76497/…. In hope this helps $\endgroup$ – lemon Mar 28 '18 at 20:09
  • 1
    $\begingroup$ Related 1 2 $\endgroup$ – batFINGER Mar 29 '18 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.