I am trying to create a mesh via a Python script and set a texture for it.
My code so far:
def add_mesh(self, coords, uvs, faces, texture, texHeight, texWidth): me = bpy.data.meshes.new("myMesh") ob = bpy.data.objects.new("myObject", me) me.from_pydata(parsedCoords, , parsedFaces) me.update(calc_edges=True) mat = self._make_material("myMaterial", (1,1,1), (1,1,1), 1) tex = self._make_texture("myTexture", texture, texWidth, texHeight) mTex = mat.texture_slots.add() mTex.texture_coods = 'UV' mTex.use_map_color_diffuse = True mTex.diffuse_color_factor = 1.0 mTex.blend_type = 'MULTIPLY' # Missing: Set the uv-coords for the mesh me.materials.append(mat)
_make_texture which are self-defined. But I think they work.
For the missing part I found using the API-documentation following
uv_layer = me.uv_layers.active.data for poly in me.polygons: for loop_ind in range(poly.loop_start, poly.loop_start+ poly.loop_total): vInd = me.loops[loop_index].vertex_index uv_layer[loop_index].uv = tuple( uvs[vertex_index] )
The passed uvs is a list uv-coordinates as lists. They are in the same order as the vertices.
My problem now is that the
uv_layer = me.uv_layers.active.data does not work. It results in
AttributeError: 'NoneType' object has no attribute 'data'
I think my mesh has no uv_layer. How do I add one? Is my code correct if I add a uv_layer to the mesh? I am really unsure about it.
Thank you for your help!