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
|
||||
|
||||
import bpy # type: ignore[import]
|
||||
import math
|
||||
|
||||
for obj in bpy.data.objects:
|
||||
if obj.type != "MESH":
|
||||
continue
|
||||
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
|
||||
for edge in mesh.edges:
|
||||
v1 = mw @ mesh.vertices[edge.vertices[0]].co
|
||||
@@ -81,8 +88,8 @@ def main() -> None:
|
||||
# Clean scene
|
||||
bpy.ops.wm.read_factory_settings(use_empty=True)
|
||||
|
||||
# Import STL
|
||||
bpy.ops.import_mesh.stl(filepath=args.stl_path)
|
||||
# Import STL (bpy.ops.wm.stl_import is the Blender 4.0+ API)
|
||||
bpy.ops.wm.stl_import(filepath=args.stl_path)
|
||||
|
||||
# Scale mm → m
|
||||
for obj in bpy.context.selected_objects:
|
||||
@@ -90,6 +97,17 @@ def main() -> None:
|
||||
bpy.context.view_layer.objects.active = obj
|
||||
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
|
||||
if sharp_edge_midpoints:
|
||||
mark_sharp_edges_by_proximity(sharp_edge_midpoints)
|
||||
|
||||
Reference in New Issue
Block a user