mirror of
https://github.com/Cian-H/simple_blender_server.git
synced 2025-12-22 21:41:56 +00:00
Switched from stl to glb for models and improved logging
This commit is contained in:
39
main.go
39
main.go
@@ -26,7 +26,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ModelPath = "./model.stl"
|
const ModelPath = "./model.glb"
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
ModelCode string `json:"model_code"`
|
ModelCode string `json:"model_code"`
|
||||||
@@ -44,7 +44,7 @@ func handleCreateModel(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Received request body (first 100 chars): %s", truncateString(string(bodyBytes), 100))
|
log.Printf("Received request body: %s", string(bodyBytes))
|
||||||
|
|
||||||
// Close the original body and create a new one with the same data
|
// Close the original body and create a new one with the same data
|
||||||
r.Body.Close()
|
r.Body.Close()
|
||||||
@@ -58,7 +58,7 @@ func handleCreateModel(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Successfully parsed request: ModelCode (first 50 chars)=%s", truncateString(req.ModelCode, 50))
|
log.Printf("Successfully parsed request: ModelCode=\n```\n%s\n```\n", req.ModelCode)
|
||||||
|
|
||||||
absModelPath, err := filepath.Abs(ModelPath)
|
absModelPath, err := filepath.Abs(ModelPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -69,13 +69,13 @@ func handleCreateModel(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
_ = os.Remove(absModelPath)
|
_ = os.Remove(absModelPath)
|
||||||
|
|
||||||
model_expression, err := create_stl(req.ModelCode, absModelPath)
|
model_expression, err := create_glb(req.ModelCode, absModelPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Error parsing templates: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Error parsing templates: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Python expression generated (first 100 chars): %s", truncateString(model_expression, 100))
|
log.Printf("Python expression generated:\n```\n%s\n```\n", model_expression)
|
||||||
|
|
||||||
tmpFile, err := os.CreateTemp("", "blender_*.py")
|
tmpFile, err := os.CreateTemp("", "blender_*.py")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -101,31 +101,31 @@ func handleCreateModel(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Blender command executed: output (first 200 chars): %s", truncateString(string(output), 200))
|
log.Printf("Blender command executed; output: %s", string(output))
|
||||||
|
|
||||||
if _, err := os.Stat(absModelPath); os.IsNotExist(err) {
|
if _, err := os.Stat(absModelPath); os.IsNotExist(err) {
|
||||||
log.Printf("Error: STL file was not created at %s", absModelPath)
|
log.Printf("Error: GLB file was not created at %s", absModelPath)
|
||||||
http.Error(w, "Failed to generate STL file", http.StatusInternalServerError)
|
http.Error(w, "Failed to generate GLB file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
stlData, err := os.ReadFile(absModelPath)
|
glbData, err := os.ReadFile(absModelPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading STL file: %v", err)
|
log.Printf("Error reading GLB file: %v", err)
|
||||||
http.Error(w, "Error reading generated STL file", http.StatusInternalServerError)
|
http.Error(w, "Error reading generated GLB file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("STL file read successfully, size: %d bytes", len(stlData))
|
log.Printf("GLB file read successfully, size: %d bytes", len(glbData))
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/octet-stream")
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filepath.Base(ModelPath)))
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filepath.Base(ModelPath)))
|
||||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(stlData)))
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(glbData)))
|
||||||
|
|
||||||
if _, err := w.Write(stlData); err != nil {
|
if _, err := w.Write(glbData); err != nil {
|
||||||
log.Printf("Failed to write response: %v", err)
|
log.Printf("Failed to write response: %v", err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("STL file sent successfully")
|
log.Printf("GLB file sent successfully")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ type ModelTemplateVars struct {
|
|||||||
Filename string
|
Filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
func create_stl(model_code string, filename string) (string, error) {
|
func create_glb(model_code string, filename string) (string, error) {
|
||||||
t := template.New("main.py.tmpl")
|
t := template.New("main.py.tmpl")
|
||||||
|
|
||||||
t, err := t.ParseFiles("main.py.tmpl")
|
t, err := t.ParseFiles("main.py.tmpl")
|
||||||
@@ -158,10 +158,3 @@ func create_stl(model_code string, filename string) (string, error) {
|
|||||||
result := buf.String()
|
result := buf.String()
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func truncateString(s string, maxLen int) string {
|
|
||||||
if len(s) <= maxLen {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return s[:maxLen] + "..."
|
|
||||||
}
|
|
||||||
|
|||||||
16
main.py.tmpl
16
main.py.tmpl
@@ -14,15 +14,12 @@ def guarded_model() -> bpy.types.Object:
|
|||||||
raise NotImplementedError("No function named `model` was provided!")
|
raise NotImplementedError("No function named `model` was provided!")
|
||||||
|
|
||||||
|
|
||||||
def export_to_stl(obj: bpy.types.Object):
|
def export_to_glb(obj: bpy.types.Object):
|
||||||
"""
|
"""
|
||||||
Export a Blender object as an STL binary blob.
|
Export a Blender object as a GLB binary blob.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
obj (bpy.types.Object): The object to export
|
obj (bpy.types.Object): The object to export
|
||||||
|
|
||||||
Returns:
|
|
||||||
bytes: Binary data of the STL file
|
|
||||||
"""
|
"""
|
||||||
# Ensure the object is the only object, is selected, and is active
|
# Ensure the object is the only object, is selected, and is active
|
||||||
bpy.ops.object.select_all(action="SELECT")
|
bpy.ops.object.select_all(action="SELECT")
|
||||||
@@ -31,11 +28,16 @@ def export_to_stl(obj: bpy.types.Object):
|
|||||||
obj.select_set(True)
|
obj.select_set(True)
|
||||||
bpy.context.view_layer.objects.active = obj
|
bpy.context.view_layer.objects.active = obj
|
||||||
|
|
||||||
bpy.ops.wm.stl_export(filepath="{{.Filename}}")
|
bpy.ops.export_scene.gltf(
|
||||||
|
filepath="{{.Filename}}",
|
||||||
|
export_format="GLB",
|
||||||
|
export_draco_mesh_compression_enable=True,
|
||||||
|
export_apply=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
export_to_stl(guarded_model())
|
export_to_glb(guarded_model())
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user