riskable / keycap_playground

The Keycap Playground is a parametric OpenSCAD keycap generator made for generating keycaps of all shapes and sizes (and profiles)
400 stars 47 forks source link

Struggling to generate material design icon legend keycaps using the python script #20

Open AeroSteveO opened 1 month ago

AeroSteveO commented 1 month ago

I've been trying to generate keycaps using material design icons font for the legends. I've installed the font on my system (running ubuntu on WSL), have installed the latest appimage for openscad, and grabbed the latest colorscad. I can generate the keycaps successfully using the keycap_playground.scad file, but i'm unable to do so using the gem_full python script. i've either had it output a square (unable to load icon) before i had the font correctly installed, or now, it outputs no legend at all. When viewing the output from colorscad, it claims one of the colors has no geometry related to it. Opening the .csg file generated in the colorscad process, i can see a bad icon, but at least there is one. i think its related to the font not being handled correctly when the python script runs, but i'm not sure why that would be.

1/2 [1, 1, 1, 1] Starting
2/2 [0.313726, 0.313726, 0.313726, 1] Starting
2/2 [0.313726, 0.313726, 0.313726, 1] Geometries in cache: 211
2/2 [0.313726, 0.313726, 0.313726, 1] Geometry cache size in bytes: 37913760
2/2 [0.313726, 0.313726, 0.313726, 1] CGAL Polyhedrons in cache: 7
2/2 [0.313726, 0.313726, 0.313726, 1] CGAL cache size in bytes: 55510320
2/2 [0.313726, 0.313726, 0.313726, 1] Total rendering time: 0:00:31.248
2/2 [0.313726, 0.313726, 0.313726, 1] Current top level object is empty.
2/2 [0.313726, 0.313726, 0.313726, 1] Warning: output is empty, removing it!
2/2 [0.313726, 0.313726, 0.313726, 1] rm: cannot remove './tmp.AODdBP/[0.313726, 0.313726, 0.313726, 1].3mf': No such file or directory
1/2 [1, 1, 1, 1] Geometries in cache: 538
1/2 [1, 1, 1, 1] Geometry cache size in bytes: 95170104
1/2 [1, 1, 1, 1] CGAL Polyhedrons in cache: 17
1/2 [1, 1, 1, 1] CGAL cache size in bytes: 98195824
1/2 [1, 1, 1, 1] Total rendering time: 0:01:33.709
1/2 [1, 1, 1, 1]    Top level object is a 3D object:
1/2 [1, 1, 1, 1]    Simple:        yes
1/2 [1, 1, 1, 1]    Vertices:     7328
1/2 [1, 1, 1, 1]    Halfedges:   35380
1/2 [1, 1, 1, 1]    Edges:       17690
1/2 [1, 1, 1, 1]    Halffacets:  20734
1/2 [1, 1, 1, 1]    Facets:      10367
1/2 [1, 1, 1, 1]    Volumes:         2
1/2 [1, 1, 1, 1] Finished at ./tmp.AODdBP/[1, 1, 1, 1].3mf

Generate a merged .3mf file
Trouble while processing '[0.313726, 0.313726, 0.313726, 1].3mf': Lib3MF Error 5 (The specified file could not be opened)
Will skip this file/color, and proceed anyway.
Warning: 1 input files were skipped!

Using python 3.10.12 openscad 2024.10.6

notes: using sublime text and openscad, i can see the icons correctly in the python script, or openscad file. still unsure why when running in python, i either get nothing as the icon, or a box.

AeroSteveO commented 1 month ago

for those struggling like me, the quote() function in keycap.py is to blame:

    def quote(self, legends):
        """
        Checks for the edge case of a single quote (') legend and converts it
        into `"'"'"'"` so that bash will pass it correclty to OpenSCAD via
        `getstatusoutput()`.  Also covers the slash (\\) legend for
        completeness.

        .. note::

            Example of what it should look like: `LEGENDS=["'"'"'", "", "\""];`
        """
        properly_escaped_quote = r'''"'"'"'"'''
        out = "["
        for i, legend in enumerate(legends):
            if legend == "'":
                out += properly_escaped_quote + ","
            elif legend == '"':
                out += r'"\""'
            else:
                out += json.dumps(legend, ensure_ascii=False) + ","
        out = out.rstrip(',') # Get rid of trailing comma
        return out + "]"

it needs the ensure_ascii=False flag in the when calling json.dumps