0 votes

Here is a basic script that export any mesh generated in Godot into STL (text format).

It only does vertices, not normals, but it's easy to add, and allows you to set the UP axis of the export. For blender, i'm using Z_UP.


extends Node

enum UP_AXIS {
    X_UP, Y_UP, Z_UP,

export(Mesh) var _mesh:Mesh = null
export(UP_AXIS) var _up_axis:int = UP_AXIS.Z_UP
export(String) var _name:String = "export"
export(String) var _path:String = "export.stl"
export(bool) var _export:bool = false setget set_export

func set_export(b:bool) -> void:
    _export = false
    if b and _mesh != null:
        var f:File = File.new()
        if f.open( _path, File.WRITE ) != OK:
        f.store_string( "solid " + _name + "\n" )
        var faces:PoolVector3Array = _mesh.get_faces()
        var v3:Vector3
        var basis:Basis = Basis.IDENTITY
        match _up_axis:
                basis = Quat(Vector3(0,0,1),PI*0.5)
                basis = Quat(Vector3(0,0,1),PI*-0.5)
                basis = Quat(Vector3(1,0,0),PI)
                basis = Quat(Vector3(1,0,0),PI*0.5)
                basis = Quat(Vector3(1,0,0),PI*-0.5)
        for i in range(0,faces.size(),3):
            f.store_string( "\tfacet\n" )
            f.store_string( "\t\touter loop\n" )
            for j in range(0,3):
                v3 = faces[i+j]
                v3 = basis * v3
                f.store_string( "\t\t\tvertex " + str(v3.x) + " " + str(v3.y) + " " + str(v3.z) + "\n" )
            f.store_string( "\t\tendloop\n" )
            f.store_string( "\tendfacet\n" )
        f.store_string( "endsolid " + _name + "\n" )

This was easy thanks to the excellent article of Dan Scott (2014) https://danbscott.ghost.io/writing-an-stl-file-from-scratch/

structure must be:

solid [mesh's name]
     facet normal [x] [y] [z]
        outer loop
            vertex [x0] [y0] [z0]
            vertex [x1] [y1] [z1]
            vertex [x2] [y2] [z2]
endsolid [mesh's name]

note that it works fine if you skip "normal ..." in each face, blender will recompute them

source: https://gitlab.com/polymorphcool/addons/-/blob/master/stl/export_stl.gd

Godot version 3.4
in Engine by (115 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.