Help with creating a mesh

I am fairly experienced with python but new to blender, and am working with blender 2.5.
What i am trying to do is create a mesh object using python code. The function I came up with is:

def plotdem(dem):
    #function to take dem and plot in blender    
    verts=[]
    #nested for loops createing a list of all verts
    for y in range(0,len(dem[0])-1):
        for x in range(0,len(dem)-1):
            z=dem[x][y]
            verts.append((x,y,z))
    faces=[]
    
    leny=len(dem[0])
    lenx=len(dem)

    #nested for loops to create faces
    for y in range(1,leny-1):
        for x in range(1,lenx-1):            
            
                       
            pt00=(y-1)*leny+x-1
            pt01=(y-1)*leny+x
            pt10=y*leny+x-1
            pt11=y*leny+x           
            faces.append((pt00,pt01, pt11, pt10))        #add face to list of faces
               
    land = bpy.data.meshes.new("LandSurface")        
    ob = bpy.data.objects.new("LandSurface", land)
    ob.location=bpy.context.scene.cursor_location
    bpy.context.scene.objects.link(ob)                    
    land.from_pydata(verts,[],faces)                    #create object land
    land.update(calc_edges=True)                        #calculate mesh

dem is a 2 dimensional list object with the z values at a given xy location (ie z=dem[x][y]), this is created elsewhere in my code, and I know this part is working properly.

When I try to run the code blender crashes each time. If I run the code commenting out the last line it creates an object, but crashes if I try to enter edit mode, and as a weird (ie should not exist) “wall” along the y=0 line, and i am getting additional faces connecting to 0,0.

So what am I doing wrong? Thanks in advance

I am not exactly sure what you mean, but here is an example of the list dem

[[-2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0], [-2.0, -1.8706639375232588, -2.0, -1.9177709241145673, -1.5943104895667393, -2.0, -1.9914762217271043, -1.2562987052227887, -1.9059903338837776, -1.180425028395511, -1.681983666268006, -1.6910357919320622, -1.4582290999931606, -1.5320262633759145, -1.8618471451223175, -1.9347092932016854, -2.0, -1.9980760610736037, -1.9174313840342487, -2.0], [-2.0, -1.9359677951637817, -1.8982144892450823, -1.9910056292019884, -2.0, -1.5283347301198766, -1.2944984336850267, -1.2191534010855796, -0.975582746740528, -1.9319757959351032, -1.4323533376686077, -0.7867066076528723, -1.2680274791181776, -1.7170776721834018, -1.4769727340330379, -1.5351772931367462, -1.4760753588237856, -1.9246350655630302, -1.6256810451359505, -2.0], [-2.0, -1.9094156406807252, -2.0, -1.967488638771805, -1.5032529532211936, -0.9452760817668189, -0.8754850644992418, -0.762595227461413, -0.4423153893894067, -0.17897824075141872, -0.13810545896743132, -1.4381824620698194, -0.18985019135337217, -2.0, -0.8960798327948827, -0.8276709923960688, -1.7211686730193156, -1.4845940025086326, -1.8978274704679754, -2.0], [-2.0, -1.8650626114067308, -1.6421321579955652, -1.2966203609342877, -1.1786631957735958, -0.7413490716708933, -0.33896952652386286, -0.593803399522124, 0.13475051075465486, 0.47402237764436406, 0.6403152864695527, 0.5147100080807065, -0.24238653445442448, 0.12959671169172246, -0.1830385980653095, -0.7384859844264046, -0.8265553336613161, -1.7484117129343, -1.879038682894694, -2.0], [-2.0, -1.9683509646289916, -1.5228552448289656, -1.9394547371148319, -1.0486254281886007, 0.27858468339514003, 0.1606374993803691, 1.5919551796116975, 2.088054626278093, 1.1647216193145111, 2.2573467999454477, 1.2159995920536986, 1.4946781253289232, 0.8634952842081782, 0.5931664053141338, 0.11170836514677365, -0.8461153904984823, -0.9858153208132797, -0.9867117320025031, -2.0], [-2.0, -1.5415702604887684, -1.3880657651315373, -1.2026774955605726, -0.9629071424755964, -0.2292906636361971, 1.2225526704336378, 5.481835502147829, 1.9314036008790854, 3.6969857981021983, 3.6635752056955906, 2.9928376294188075, 2.885913961223082, 3.9700871060857024, 1.548178424252181, 1.0682489945570928, 0.1583602257754357, -1.3434997910378081, -1.3587001420693017, -2.0], [-2.0, -1.868329399385283, -1.1471473614934393, -0.9677065140820706, -0.6062153333250262, 0.18913875096475347, 1.434127646132253, 3.2692956965719024, 6.186764432692605, 2.6286829933483054, 3.6481523003437935, 4.587238518826684, 4.729279615339711, 3.7775361052414955, 3.3200129629757384, 1.7517101065794465, 0.5695091017251928, -0.3347097775622931, -0.2751335713484187, -2.0], [-2.0, -1.3197558835477872, -1.9816184893095545, -0.782919439159434, -0.24012747762536668, 1.705605928089364, 1.6556527410263786, 2.351782436749227, 5.06999591649486, 3.657324365500068, 5.583006928968977, 7.52999381802478, 3.990348512923793, 3.437318479198933, 3.3397920363408367, 3.097521488375092, 1.3747755354011482, 0.3498964151887495, -0.7789794699848069, -2.0], [-2.0, -1.8601480680219058, -1.0198593531957099, -0.927095797577735, -0.21303468363127326, 1.213554974529682, 1.2329088345818797, 2.334324570624877, 3.371643549702465, 11.293806282971376, 5.191194186907433, 5.302429923882369, 11.594189371062836, 5.463193964819342, 3.5733267495802346, 3.5341078823044367, 1.5473095121812082, 0.709054919138841, -1.1954703568238443, -2.0], [-2.0, -2.0, -1.067203247188803, -0.9935706162490854, -0.2707787918583821, 1.2251268795507304, 3.0220397949657203, 3.2678841572995716, 4.198049353100094, 4.2266257958401985, 5.510597793841073, 8.18439724285164, 5.003752259392373, 7.845958912362676, 3.8249530008186734, 3.169559984463609, 1.0363986028210679, 1.1811377826419727, 0.1462790767051326, -2.0], [-2.0, -1.941631640847955, -1.3196253621066607, -1.234837323562796, -0.4175717954651354, 0.49695888829092455, 1.2784011421975758, 3.4367855691838893, 3.772872715569071, 7.234052403510925, 7.8664617415509674, 4.970782923690901, 6.254551882896405, 8.019712234436149, 4.594357628311834, 3.0088576717981184, 1.0468123680221633, -1.783669444841597, 0.8112932827794084, -2.0], [-2.0, -2.0, -2.0, -1.8033349770027658, -0.34958680556240895, -0.3655909224309125, 1.7803381845548407, 4.113458755973308, 3.199358750387245, 5.521248893581313, 5.1877717445710525, 5.542052355393956, 11.382763890579286, 4.387543971654503, 4.628154368898873, 3.333026542964885, 1.5566516889185509, 0.9409078889386208, -1.6165642303526715, -2.0], [-2.0, -2.0, -2.0, -1.0963814516919748, -0.6737547227869943, -0.08740365290956573, 0.9744542905186766, 1.6363034507000858, 2.766844570668412, 3.7004192875771698, 11.433292492103067, 6.67308223266634, 1.8881995991183065, 4.110418944496971, 4.03459512550746, 2.762602856722033, 1.9878745563555213, 1.1756433185681594, 0.20325984586430138, -2.0], [-2.0, -2.0, -1.3344295878755659, -2.0, -1.9392376957644542, -0.09164513259794185, 0.597193411413879, 1.2635446483900536, 2.116302739269181, 4.188581517024461, 3.440691703362375, 3.577224714755262, 4.699139380524943, 3.7708435854197715, 2.8593220392574086, 2.552663066469908, 1.3984155602812678, 0.8048038304253238, 0.03634101205820557, -2.0], [-2.0, -1.7105989020295738, -1.6260454129699604, -1.6086982128950646, -1.050363113973172, -1.1310521513615028, 0.0565111007459015, 1.36962266794378, 1.9894628721652685, 1.9600344975945068, 2.053136009636224, 3.704841705290413, 3.44710560409843, 2.84359891023833, 2.9801361875572168, 1.6767944542926851, 1.1750553557322163, 0.4943172779602172, 0.16248483599556107, -2.0], [-2.0, -1.9373832559802726, -1.7035807432839416, -1.5081848863055987, -1.228371221763549, -1.053092716254403, -0.4545480267597594, -0.2958395204140891, -0.3787400349624512, 0.5233767761203251, 1.2663556689435125, 0.5593929100304266, 1.9419516338884886, 1.8894776448217565, 1.3063287366727756, 1.276734968866831, -1.1004991821156493, 0.10530797679108117, -0.26599620911814703, -2.0], [-2.0, -2.0, -1.725485418632833, -1.6203830726812811, -1.3486080866854055, -1.3061646521482222, -0.9926590016370451, -0.4618775483103885, 0.21855634071004715, 0.4098098086557155, 0.22207743360894258, 0.9711767465211174, -1.9002605560172232, 1.1247259121637112, 0.9571555907300336, 0.45916916603149016, 0.08868692460424316, -0.594915243501883, -0.35475934830261857, -2.0], [-2.0, -1.9327249948845446, -1.7283483363495278, -2.0, -1.8218735249729123, -1.452006931092301, -1.0039775081228002, -1.2590119423111439, -0.3811491876536563, -0.10190145868980645, 0.07032128482451519, -1.1449711777550533, -0.784012825959388, 0.1391470280598081, 0.22738294685617894, -0.03291376986715735, -0.12820960994290567, -0.4870294666790773, -0.9484130058462262, -2.0], [-2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0]]

The above list is much shorter than in practice to meet post size limit requirements.

It looks like your problem is that you don’t in fact have functional x,y coordinates for every z.


import bpy

# Constants, for testing.
ZOOM_RES = 1e4
XY_SPREAD = 1

# assumes a square matrix of Z values
def CreateMeshUsingMatrix(VertIndices, Verts):

    Faces = []
    dims = len(VertIndices) # this assumes x,y ratio is 1:1

    # going to use current row and next row all the way down
    for row in range(dims-1):
        #now take row and row+1
        for col in range(dims-1):
            #we generate faces clockwise from 4 Verts
            val1 = VertIndices[row][col]
            val2 = VertIndices[row][col+1]
            val3 = VertIndices[row+1][col+1]
            val4 = VertIndices[row+1][col]
            face = [val1, val2, val3, val4]
            Faces.append(face)
        

    ## TAKEN FROM USER    ValterVB 
    # create new mesh structure
    mesh = bpy.data.meshes.new("Relief")
    mesh.from_pydata(Verts, [], Faces)
    mesh.update()
    # create an object from this mesh
    new_object = bpy.data.objects.new("Ascii Data", mesh)
    new_object.data = mesh
    # adding object called "Ascii Data" to the scene
    scene = bpy.context.scene
    scene.objects.link(new_object)
    # deals with selecting
    scene.objects.active = new_object
    new_object.select = True



def start_mesh_creation(heightMatrix):
    VertIndices = []
    
    # this supposes that X, Y separation are going to be the same.
    xy_val = []
    for i in range(len(heightMatrix)): # NOTE: or len(heightMatrix[0]) whichever is longer.
        xy_val.append(i*XY_SPREAD)

    # Generates the (x,y,height) matrix, no need for Vector(...) 
    yVal = 0
    vertNum = 0
    rawVertCollection = []
    for height_x in heightMatrix:
        xVal = 0
        vertRow = []
        for item in height_x:
            t_vertice = (xy_val[xVal], -xy_val[yVal], heightMatrix[yVal][xVal])
            rawVertCollection.append(t_vertice)
            
            vertRow.append(vertNum)
            xVal+=1
            vertNum+=1
        yVal+=1
        VertIndices.append(vertRow)

    # done here, lets make a mesh! 
    CreateMeshUsingMatrix(VertIndices, rawVertCollection)


start_mesh_creation(dem)

you might tweak this if you have a input data that isn’t n*n
http://img3.imageshack.us/img3/5347/easyym.png

you might have to flip normals afterwards, if you want to texture this.