0
$\begingroup$

I am trying to learn how to create new objects from vertices and face data and also include a uv map. I used the following code:

    def BuildRectangle(self):
    objName="TMP"
    mesh = bpy.data.meshes.new(objName)
    material = None    
    ob = bpy.data.objects.new(objName, mesh) 
    bpy.context.scene.collection.objects.link(ob)

    verts=[]
    verts.append(Vector((1,1,0)))
    verts.append(Vector((-1,1,0)))
    verts.append(Vector((-1,-1,0)))
    verts.append(Vector((1,-1,0)))
    verts.append(Vector((1,1,4)))
    verts.append(Vector((-1,1,4)))
    verts.append(Vector((-1,-1,4)))
    verts.append(Vector((1,-1,4)))

    faces=[]
    faces.append([0,4,7,3])
    faces.append([0,1,5,4])
    faces.append([1,5,6,2])
    faces.append([2,3,7,6])

    uvLocations=[]
    uvLocations.append(Vector((0.25,0))) #0
    uvLocations.append(Vector((0.25,1))) #1
    uvLocations.append(Vector((0,1))) #2
    uvLocations.append(Vector((0,0))) #3
    uvLocations.append(Vector((0.25,0))) #4
    uvLocations.append(Vector((0.5,0))) #5
    uvLocations.append(Vector((0.5,1))) #6
    uvLocations.append(Vector((0.25,1))) #7
    uvLocations.append(Vector((0.5,0))) #8
    uvLocations.append(Vector((0.5,1))) #9
    uvLocations.append(Vector((0.75,1))) #10
    uvLocations.append(Vector((0.75,0))) #11
    uvLocations.append(Vector((0.75,0))) #12
    uvLocations.append(Vector((1,0))) #13
    uvLocations.append(Vector((1,1))) #14
    uvLocations.append(Vector((0.75,1))) #15

    mesh.from_pydata(verts, [], faces)

    smoothings = np.ones(len(faces), dtype=bool)
    mesh.polygons.foreach_set("use_smooth", smoothings)
    # bm = bmesh.new()
    # bm.from_mesh(mesh)
    bm = bmesh.from_edit_mesh(mesh)  
    bm.faces.ensure_lookup_table()  
    bm.to_mesh(mesh)

    uv_layer = bm.loops.layers.uv.verify()
    i=0
    for face in bm.faces:
            for loop in face.loops:
                loop[uv_layer].uv=uvLocations[i]
                i+=1


    bmesh.update_edit_mesh(mesh, False, False)
    bm.free()
    return ob 

I am getting an error at bm = bmesh.from_edit_mesh(mesh) that says "The mesh must be in editmode". How do I put the mesh into edit mode?

$\endgroup$

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.