best way of centering text

6 views
Skip to first unread message

Matthieu Hendriks

unread,
Jan 7, 2026, 5:57:15 AM (6 days ago) Jan 7
to PythonSCAD
What is the best/easiest approach to center text on x and y ?


W W

unread,
Jan 7, 2026, 12:32:05 PM (6 days ago) Jan 7
to PythonSCAD
On a solid's surface, or just the axis?

For the former: use bounding boxes. You can find the center of a bounding box of a solid, and move the text to that location.

For axis: you can find the bounding box of the text itself, and move the text to the origin by taking the negation of the center of the bounding box.

Matthieu Hendriks

unread,
Jan 8, 2026, 12:48:12 AM (5 days ago) Jan 8
to PythonSCAD
It's for a solid surface, this was the solution for me:

def createText(string="text",h=2,angle=-180,size=4,valign="center", halign="center",font="Liberation Sans:style=Bold"):
    print(f"Hoek: {angle}")
    o1=roty(linear_extrude(text(string,size,font=font,valign=valign,halign=halign,script="latin",language="en"),h),angle)
    if angle<0:
        return o1.translate([0,0,h])
    return o1
Op woensdag 7 januari 2026 om 18:32:05 UTC+1 schreef willy...@gmail.com:

W W

unread,
Jan 8, 2026, 1:24:17 AM (5 days ago) Jan 8
to PythonSCAD
Don't you need to specify h and angle manually, depending on the solid?

My motivation behind recommending bounding box is that, it should largely work with any solids. Worst case you may need to orient the solid first, but after that it will always center.

Matthieu Hendriks

unread,
Jan 8, 2026, 4:11:37 AM (5 days ago) Jan 8
to W W, PythonSCAD
My designs are merely used for 3d printing, so I'm "always" centering objects on the x/y axis and start with z=0.
(When the object is  part of a greater object I move the complete sub-object to the desired position.)

For example when I need a lid for a box with some multicolor printing on it I need for printing to separate objects:
- o1=  difference(lid(),createText("lidText") 
- o2 = createText("lidText") 

The export to a 3mf file supports multiple objects in one file so in Bambu Slicer I can assign different colors to it.

p.s. 
- Of course I can do a translate(createText("This is some text"),[0,0,ObjectHeight]) 
- I added the angle for printing text on the bottom in stead of on top, so I don't need support for printing.



Op do 8 jan 2026 om 07:24 schreef W W <willy...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "PythonSCAD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonscad+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/pythonscad/2f920009-113f-489f-bd7a-f47b248ccb93n%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages