Make a mesh.-
Your example has 4 vertices and two faces making a rectangle of two triangles, going to need a bit more geometry than that to have rounded corners. The same method applies here to making a mesh.
Given blender has a built in tool to make meshes, let's make a plane with bmesh, bevel the corners, triangulate it, and scale to suit.
Bevel & triangulate.
Test script, makes a 2 x 1 rounded corner rectangle. The corner radius is 0.2 and 5 segments create the bevel. The bmesh is later translated and scaled to match our bgl coordinates. Moved to 100, 100 and scaled by 100 (Could also be saved to a mesh. See other bgl manual examples re using mesh coordinates)
import bmesh
import bpy
from mathutils import Matrix
dim = (2, 1)
cnr = 0.2
bm = bmesh.new()
bmesh.ops.create_grid(
bm,
size=0.5,
matrix=Matrix.Diagonal((dim[0], dim[1], 1, 1)) @
Matrix.Translation((0.5, 0.5, 0)),
)
bmesh.ops.bevel(
bm,
geom=bm.verts,
#loop_slide=True,
affect='VERTICES',
profile=0.5, # round
offset=cnr,
segments=5,
)
bmesh.ops.triangulate(
bm,
faces=bm.faces,
)
# scale by 100
bm.transform(
Matrix.Diagonal([100] * 4)
)
# translate by 100, 100
bm.transform(
Matrix.Translation((100, 100, 0))
)
print("vertices = (")
for v in bm.verts:
print(f"\t{v.co.xy[:]},")
print(")")
print("faces = (")
for f in bm.faces:
print(f"\t{[v.index for v in f.verts]},")
bm.to_mesh(bpy.context.object.data)
print(")")
Produces
Color (0,1, 0.5, 1.0)
rounded corner result of using data below (faces for indices), overlayed on pre-run rectangle of question script
vertices = (
(120.0, 100.0),
(100.0, 120.0),
(113.81965637207031, 100.97886657714844),
(108.24429321289062, 103.81965637207031),
(103.81965637207031, 108.24429321289062),
(100.97886657714844, 113.81965637207031),
(300.0, 120.0),
(280.0, 100.0),
(299.0211181640625, 113.81965637207031),
(296.18035888671875, 108.24429321289062),
(291.7557067871094, 103.81965637207031),
(286.18035888671875, 100.97886657714844),
(120.0, 200.0),
(100.0, 180.0),
(113.81965637207031, 199.0211181640625),
(108.24429321289062, 196.18032836914062),
(103.81965637207031, 191.75570678710938),
(100.97886657714844, 186.18032836914062),
(280.0, 200.0),
(300.0, 180.0),
(286.18035888671875, 199.0211181640625),
(291.7557067871094, 196.18032836914062),
(296.18035888671875, 191.75570678710938),
(299.0211181640625, 186.18032836914062),
)
faces = (
[13, 1, 0],
[19, 21, 18],
[1, 5, 0],
[5, 4, 3],
[3, 2, 0],
[0, 7, 18],
[7, 11, 6],
[18, 7, 6],
[11, 10, 9],
[9, 8, 11],
[8, 6, 11],
[6, 19, 18],
[19, 23, 21],
[23, 22, 21],
[21, 20, 18],
[18, 12, 0],
[12, 14, 13],
[0, 12, 13],
[14, 15, 13],
[15, 16, 13],
[16, 17, 13],
[5, 3, 0],
)
Triangulate with Poke instead
Result of replacing bmesh
triangulate operator with poke
bmesh.ops.poke(
bm,
faces=bm.faces,
)
Block with rounded edges in python