Script to create Mesh faces from mesh edges

Hi All :smiley:

first of all, i’m sorry for my bad english, i’m french so forgive me if i make some mistakes.

Theb i’m quite noob in blender and python, but i’m very motivated to learn.

i’m working on blender 249.2 and pyhton 2.6

i need some help to build a script that allow me to create a planar mesh from a list of verts.

I have a list of verts

VERTS=[[vert1][vert2][vert3][vert4][vert5]…]
for each vert_i , vert_i=[x_i,y_i]

i can add to my new mesh the edges
Edges=[[edge1][edge2][edge3],…]
egde_i=[vert_i,vert_i+1]

is it possible to create easyly the surfaces of my mesh ??

or i have to work on my point an créate surface by surface ?

Thanks all for your help.

Pendra

If your verts are co-planar, you can use some equivalent of CTRL+F to cover the area… if not - you should group the verts and process these group by group.

Regards,

Hi Abidos,

thanks for your answer.

Yes all my verts of an object are coplanar

i’ve tried, it works but with SHIFT+F.

But i did it manualy.

How to put this command in my python script ?
because i have a lot of objects to create and i want my script to do the famous “SHIFT+F”

This is my nooby script to create my objects edges:

import Blender
from Blender import NMesh

truc = [[0,0],[2,0],[3,2],[4,0],[5,0],[6,1],[7,0],[5,-2],[2,-2],[1.5,-3],[2,-3],[3,-2.5],[6,-3],[8,-1],[8,0],[9,1],[8,4],[6,5],[3,6],[-1,4]]

me=NMesh.New(‘poly’)

for coord in truc :
v=NMesh.Vert(coord[0],coord[1],0.0)
me.verts.append(v)

print “Il y a”, len(me.verts), “vertices dans le poly”

nbre_pt=len(me.verts)
j=0
while j< (nbre_pt-1):
me.addEdge(me.verts[j],me.verts[j+1])
j=j+1

me.addEdge(me.verts[nbre_pt-1],me.verts[0])

poly=NMesh.PutRaw(me)
Blender.Redraw()

THANKS all for your answers

Regards

Pendra

Hi all :yes:

that it i’ve found !!!

PolyFill(polylines)

thanks for your help …

And rendez_vous at my next problem :smiley:

Pendra

you should definitively go with 2.53 latest Beta version

la serie 2.49 fait partie de l’histoire maintenant
vive 2.5 !

a la prochaine

happy 2.5

Yeah RickyBlender you right i should !!!

je commence à peine a m’habituer à blender 2.49, mais tu as certainement raison vaut mieux que je change de version !!!

Thanks all

@ pendra972 - You’re right - SHIFT+F… I wrote it by memory so I put CTRL instead of SHIFT.

As per the python implementation - as soon as you’re heading to 2.53, I wont be of help to you cause i work with 2.49… BUT you’ll have serious problems with your approach using NMesh which is deprecated since 2.45 or berofe that…

Regards,

Yeah Thanks Abidos,

finally i’ll still with 2.49 blender version.

What is the problem with NMESH ??

I’m just beggining in python and the main tuto i’ve found have been done with NMESH.
What can i use other ? MESH?
what u suggest?

thanks

NMesh is in the list of obsolete structures so being a novice you should try to avoid such code cause you may get problems with it in a moment you less wish it. Try to find alternatives of the deprecated functions too… I know it is NOT always easy but soon you will think in a manner of a working code rather than wondering for days why certain problems exists in your code due (or not due) to deprecation and/or lack of future function support or may be a different reason. :cool:

You may also wish to put CODE-/CODE tags when publishing code (right-most button of the buttons row above that text-box).

Now, here is a translation of your code using non-deprecated API for 2.4x. I added also a function to covering the contour that is already available after completion of the first function. In your case, there is no need to check if your contour is a planar 2D one cause it is such by construction. BUT if you are to apply the Fill_2d_contour function to an unknown mesh, you should check it first (not a subject of the discussion here) and only if you’re positive on that you should apply it in order to work properly.

from Blender import *

truc = [[0,0],[2,0],[3,2],[4,0],[5,0],[6,1],[7,0],[5,-2],[2,-2],[1.5,-3],[2,-3],
[3,-2.5],[6,-3],[8,-1],[8,0],[9,1],[8,4],[6,5],[3,6],[-1,4]]

def Make_poly_obj(lst_coords_2D):
    verts_list = []
    edges_list = []
    faces_list = []
    
    for coord in lst_coords_2D:
        verts_list.append([coord[0],coord[1],0.0])
    
    n = len(verts_list)
    for i in range(n-1):
        edges_list.append([i,i+1])
    edges_list.append([n-1,0])
    
    me = Mesh.New("Poly")
    me.verts.extend(verts_list)
    me.edges.extend(edges_list)
    me.faces.extend(faces_list)
    
    ob = Object.New("Mesh")
    ob.link(me)
    sce = Scene.GetCurrent()
    sce.objects.link(ob)
    sce.objects.selected = []
    ob.sel = 1
    ob.name = "Poly"
    print "Il y a", len(me.verts), "vertices dans le poly"
    return ob

def Fill_2d_contour(oName,fl_TrianglesToQuad):
    ob = Object.Get(oName)
    me = ob.getData(mesh=1)
    for v in me.verts:
        v.sel = 1
    Mesh.Mode(1)
    me.fill()
    if fl_TrianglesToQuad:
        me.triangleToQuad()
    return

############################################

editmode = Window.EditMode()
Window.EditMode(0)
Window.WaitCursor(1)
    
ob = Make_poly_obj(truc)
Fill_2d_contour(ob.name,True)
    
Redraw()
Window.WaitCursor(0)
Window.EditMode(editmode)

You may say that some code is not really needed for the purposes of your task… And you will be right… For example, regarding face_list which remains practically unused above. BUT based on the code above you may easily produce a new function that in object’s creation process deals with mesh faces too and you wont need to think much about where/how/when to put certain code about this keeping the same style.

Regards,

Hi all, Hi Abidos i’ve just seen your reply .

thanks for your advices and for your “correction”.i’m looking on it …

Hope, i’ll do better for my next script

thanks very much

That is a nice goal of yours… :yes:

Regards,