cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
GDL
About building parametric objects with GDL.

Easy gdl mesh

rudl
Contributor

Easy mesh:

! No more VERT EDGE PGON ! (because its a bit of a pain)

just a vertex array

and a face array with vert indices

Face direction should be automatic with the order of vertices, Edges are automatic

Needs more testing, may add some more features.

 

! ------------------------------------------------------------
! Geometry stored in vert and pgon arrays only Example: Pyramid
! ------------------------------------------------------------
! Important order of vertices in Pgon determine Normal / Face direction

! --- SETUP VARIABLES ---

dim points_s[][3]	!points start
dim edges_pi[][2]	!edges point index
dim faces_pi[][]	!faces point index
dim ppf[]			!points per face

! --- POPULATE ---

points_s[1][1] = -1 : points_s[1][2] = -1 : points_s[1][3] = 0	! base
points_s[2][1] =  1 : points_s[2][2] = -1 : points_s[2][3] = 0
points_s[3][1] =  1 : points_s[3][2] =  1 : points_s[3][3] = 0
points_s[4][1] = -1 : points_s[4][2] =  1 : points_s[4][3] = 0
points_s[5][1] =  0 : points_s[5][2] =  0 : points_s[5][3] = 2  	! apex

ppf[1] = 4 : faces_pi[1][1] = 1 : faces_pi[1][2] = 4 : faces_pi[1][3] = 3 : faces_pi[1][4] = 2 	! base (quad)
ppf[2] = 3 : faces_pi[2][1] = 1 : faces_pi[2][2] = 2 : faces_pi[2][3] = 5
ppf[3] = 3 : faces_pi[3][1] = 2 : faces_pi[3][2] = 3 : faces_pi[3][3] = 5
ppf[4] = 3 : faces_pi[4][1] = 3 : faces_pi[4][2] = 4 : faces_pi[4][3] = 5
ppf[5] = 3 : faces_pi[5][1] = 4 : faces_pi[5][2] = 1 : faces_pi[5][3] = 5

nPoints = 5 		! this should be known does not make sense to calc! optional with VARDIM1
nFaces = 5 		! this should be known does not make sense to calc! optional with VARDIM1

! ------------------------------------------------------------
! DO THE MAGIC THING:
! ------------------------------------------------------------

! --- Point index for edges --- edges_pi[][2] for use in EDGE

x=0	! the counter is important!
FOR i=1 TO nFaces
	FOR j=1 TO ppf[i]
		x = x+1
		IF j < ppf[i] THEN 
			edges_pi[x][1]=faces_pi[i][j]
			edges_pi[x][2]=faces_pi[i][j+1]
		ELSE 
			edges_pi[x][1]=faces_pi[i][j] !connect last point of face with first
			edges_pi[x][2]=faces_pi[i][1]		
		ENDIF
	NEXT j
NEXT i

lengthedge = VARDIM1 (edges_pi) ! total edges with duplicates should be nEDGE

! --- Create edge index --- edges[] for use in PGON
! There is some kind of "sorting" through to make the double edge indices negative
! could be optimised for larger meshes though

DIM edges[]
negcount = 0
FOR i = 1 TO lengthedge
	u = edges_pi[i][1]
	v = edges_pi[i][2] 
	edges[i]=i-negcount
		FOR j = 1 To i
			IF edges_pi[j][1]=v AND edges_pi[j][2] = u THEN
				edges[i]= edges[j]*(-1)
				negcount = negcount+1
			ENDIF
		NEXT J
NEXT i

! ------------------------------------------------------------
! BUILDING THE MESH:
! ------------------------------------------------------------

!PUT EVERYTHING TOGETHER

BASE
FOR i = 1 TO nPoints
	VERT points_s[i][1],points_s[i][2],points_s[i][3]
NEXT i

FOR i = 1 TO lengthedge
	IF edges[i]>=1 THEN
		EDGE edges_pi[i][1], edges_pi[i][2],-1,-1,0
	ENDIF
NEXT i

PUT edges
FOR i = 1 TO nFaces
	PGON ppf[i],0,0,GET(ppf[i])
NEXT i

BODY -1

! ------------------------------------------------------------
! DEBUG:
! ------------------------------------------------------------

!PRINT edges_pi
!PRINT edges

 

3 REPLIES 3
Barry Kelly
Moderator

Thanks for showing the method, but I am not sure I understand the purpose.

You did use VERT EDGE PGON, just in a loop.

And wouldn't it be easier to use the PYRAMID command?

 

Barry.

 

One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11

In the end of course i used Vert edge and pgons etc.

The pyramid is just a simple example  I'm thinking about much more complex things. Its more about how it is stored

Its way easier if you have indices than the order of creation 

especially if you want to create something parametric where the number of vertices edges pgons is not known

rudl
Contributor

Not a pyramid:
In conjunction with a blender script (I did us chatgpt for that)

import bpy

# Output file path
output_file = "C:/Users/potato/mesh_data.txt"  # <-- change this

with open(output_file, 'w') as f:
    vertex_count = 1
    face_count = 1

    for obj in bpy.context.selected_objects:  # or bpy.data.objects for all
        if obj.type != 'MESH':
            continue
        
        mesh = obj.data
        #f.write(f"// Object: {obj.name}\n\n")

        # Write vertices
        for v in mesh.vertices:
            f.write(f"points_s[{vertex_count}][1] = {v.co.x}\n")
            f.write(f"points_s[{vertex_count}][2] = {v.co.y}\n")
            f.write(f"points_s[{vertex_count}][3] = {v.co.z}\n")
            vertex_count += 1

        f.write("\n")

        # Write faces
        for p in mesh.polygons:
            # Ensure we only handle triangles or quads (can expand if needed)
            verts = p.vertices
            for i in range(len(verts)):
                f.write(f"faces_pi[{face_count}][{i+1}] = {verts[i]+1}\n")  # +1 for 1-based indexing
            face_count += 1

        f.write("\n")

print(f"Mesh data exported to {output_file}")




Screenshot 2025-10-14 225243.png