fix(export_gltf): update for Blender 5.0 API (wm.stl_import, shade_smooth_by_angle)
- bpy.ops.import_mesh.stl → bpy.ops.wm.stl_import (removed in Blender 4.0) - mesh.use_auto_smooth = True → bpy.ops.object.shade_smooth_by_angle() (use_auto_smooth removed in Blender 4.1) - Apply smooth shading to all objects unconditionally after scale, so GLB is smooth-shaded even when no sharp edge midpoints are present Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -48,12 +48,19 @@ def mark_sharp_edges_by_proximity(midpoints_mm: list, threshold_mm: float = 1.0)
|
|||||||
return
|
return
|
||||||
|
|
||||||
import bpy # type: ignore[import]
|
import bpy # type: ignore[import]
|
||||||
|
import math
|
||||||
|
|
||||||
for obj in bpy.data.objects:
|
for obj in bpy.data.objects:
|
||||||
if obj.type != "MESH":
|
if obj.type != "MESH":
|
||||||
continue
|
continue
|
||||||
mesh = obj.data
|
mesh = obj.data
|
||||||
mesh.use_auto_smooth = True
|
# Blender 4.1+ removed use_auto_smooth — use shade_smooth_by_angle instead
|
||||||
|
bpy.context.view_layer.objects.active = obj
|
||||||
|
obj.select_set(True)
|
||||||
|
try:
|
||||||
|
bpy.ops.object.shade_smooth_by_angle(angle=math.radians(30))
|
||||||
|
except Exception:
|
||||||
|
pass # fallback: stay flat-shaded
|
||||||
mw = obj.matrix_world
|
mw = obj.matrix_world
|
||||||
for edge in mesh.edges:
|
for edge in mesh.edges:
|
||||||
v1 = mw @ mesh.vertices[edge.vertices[0]].co
|
v1 = mw @ mesh.vertices[edge.vertices[0]].co
|
||||||
@@ -81,8 +88,8 @@ def main() -> None:
|
|||||||
# Clean scene
|
# Clean scene
|
||||||
bpy.ops.wm.read_factory_settings(use_empty=True)
|
bpy.ops.wm.read_factory_settings(use_empty=True)
|
||||||
|
|
||||||
# Import STL
|
# Import STL (bpy.ops.wm.stl_import is the Blender 4.0+ API)
|
||||||
bpy.ops.import_mesh.stl(filepath=args.stl_path)
|
bpy.ops.wm.stl_import(filepath=args.stl_path)
|
||||||
|
|
||||||
# Scale mm → m
|
# Scale mm → m
|
||||||
for obj in bpy.context.selected_objects:
|
for obj in bpy.context.selected_objects:
|
||||||
@@ -90,6 +97,17 @@ def main() -> None:
|
|||||||
bpy.context.view_layer.objects.active = obj
|
bpy.context.view_layer.objects.active = obj
|
||||||
bpy.ops.object.transform_apply(scale=True)
|
bpy.ops.object.transform_apply(scale=True)
|
||||||
|
|
||||||
|
# Apply smooth shading with 30° angle threshold (Blender 4.1+ API)
|
||||||
|
import math as _math
|
||||||
|
for obj in bpy.data.objects:
|
||||||
|
if obj.type == "MESH":
|
||||||
|
bpy.context.view_layer.objects.active = obj
|
||||||
|
obj.select_set(True)
|
||||||
|
try:
|
||||||
|
bpy.ops.object.shade_smooth_by_angle(angle=_math.radians(30))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# Mark sharp edges for better UV seams
|
# Mark sharp edges for better UV seams
|
||||||
if sharp_edge_midpoints:
|
if sharp_edge_midpoints:
|
||||||
mark_sharp_edges_by_proximity(sharp_edge_midpoints)
|
mark_sharp_edges_by_proximity(sharp_edge_midpoints)
|
||||||
|
|||||||
Reference in New Issue
Block a user