From ee40b7008437714643efbc06a2931e600751bf80 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 18 May 2023 16:13:12 -0400 Subject: [PATCH 1/5] Mesh: Update addons for auto smooth removal --- add_mesh_BoltFactory/Boltfactory.py | 2 -- .../add_mesh_rocks/rockgen.py | 3 --- io_mesh_stl/blender_utils.py | 3 --- io_scene_3ds/import_3ds.py | 3 +-- io_scene_fbx/export_fbx_bin.py | 3 --- io_scene_fbx/import_fbx.py | 7 ------- .../gltf2_blender_gather_primitives_extract.py | 3 --- .../blender/imp/gltf2_blender_mesh.py | 5 +---- io_scene_obj/export_obj.py | 4 ---- io_scene_obj/import_obj.py | 2 -- io_scene_x3d/export_x3d.py | 4 ---- io_scene_x3d/import_x3d.py | 3 +-- materials_utils/functions.py | 3 +-- mesh_tissue/tessellate_numpy.py | 1 - object_carver/carver_utils.py | 3 +-- render_povray/model_primitives.py | 7 +------ render_povray/model_primitives_topology.py | 8 ++------ space_view3d_spacebar_menu/edit_mesh.py | 17 ----------------- 18 files changed, 8 insertions(+), 73 deletions(-) diff --git a/add_mesh_BoltFactory/Boltfactory.py b/add_mesh_BoltFactory/Boltfactory.py index 0c56e9b78..b625ccbff 100644 --- a/add_mesh_BoltFactory/Boltfactory.py +++ b/add_mesh_BoltFactory/Boltfactory.py @@ -416,7 +416,6 @@ class add_mesh_bolt(Operator, AddObjectHelper): (context.active_object.data is not None) and ('Bolt' in context.active_object.data.keys()) and \ (self.change == True): obj = context.active_object - use_auto_smooth = bool(obj.data.use_auto_smooth) # Copy value, do not take a reference use_smooth = bool(obj.data.polygons[0].use_smooth) # Copy value, do not take a reference mesh = createMesh.Create_New_Mesh(self, context) @@ -428,7 +427,6 @@ class add_mesh_bolt(Operator, AddObjectHelper): bm.free() # Preserve flat/smooth choice. New mesh is flat by default - obj.data.use_auto_smooth = use_auto_smooth if use_smooth: bpy.ops.object.shade_smooth() diff --git a/add_mesh_extra_objects/add_mesh_rocks/rockgen.py b/add_mesh_extra_objects/add_mesh_rocks/rockgen.py index 8269e126c..1036de229 100644 --- a/add_mesh_extra_objects/add_mesh_rocks/rockgen.py +++ b/add_mesh_extra_objects/add_mesh_rocks/rockgen.py @@ -148,9 +148,6 @@ def createMeshObject(context, verts, edges, faces, name): # Make a mesh from a list of verts/edges/faces. mesh.from_pydata(verts, edges, faces) - # Set mesh to use auto smoothing: - mesh.use_auto_smooth = True - # Update mesh geometry after adding stuff. mesh.update() diff --git a/io_mesh_stl/blender_utils.py b/io_mesh_stl/blender_utils.py index 5c84152e9..77d3f3d51 100644 --- a/io_mesh_stl/blender_utils.py +++ b/io_mesh_stl/blender_utils.py @@ -17,7 +17,6 @@ def create_and_link_mesh(name, faces, face_nors, points, global_matrix): if face_nors: # Note: we store 'temp' normals in loops, since validate() may alter final mesh, # we can only set custom lnors *after* calling it. - mesh.create_normals_split() lnors = tuple(chain(*chain(*zip(face_nors, face_nors, face_nors)))) mesh.loops.foreach_set("normal", lnors) @@ -33,8 +32,6 @@ def create_and_link_mesh(name, faces, face_nors, points, global_matrix): mesh.polygons.foreach_set("use_smooth", [True] * len(mesh.polygons)) mesh.normals_split_custom_set(tuple(zip(*(iter(clnors),) * 3))) - mesh.use_auto_smooth = True - mesh.free_normals_split() mesh.update() diff --git a/io_scene_3ds/import_3ds.py b/io_scene_3ds/import_3ds.py index 000d10aa4..93ab7164d 100644 --- a/io_scene_3ds/import_3ds.py +++ b/io_scene_3ds/import_3ds.py @@ -1076,11 +1076,10 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, CONSTRAI pivot_list[len(pivot_list) - 1] = mathutils.Vector(pivot) elif new_chunk.ID == MORPH_SMOOTH and child.type == 'MESH': # Smooth angle - child.data.use_auto_smooth = True temp_data = file.read(SZ_FLOAT) smooth_angle = struct.unpack(' pi. - fw(ident_step + 'creaseAngle="%.4f"\n' % (mesh.auto_smooth_angle if mesh.use_auto_smooth else 4.0)) if use_normals: # currently not optional, could be made so: diff --git a/io_scene_x3d/import_x3d.py b/io_scene_x3d/import_x3d.py index 6b2c7afc3..aae6487b8 100644 --- a/io_scene_x3d/import_x3d.py +++ b/io_scene_x3d/import_x3d.py @@ -3011,8 +3011,7 @@ def importShape_ProcessObject( # solid=false, we don't support it yet. creaseAngle = geom.getFieldAsFloat('creaseAngle', None, ancestry) if creaseAngle is not None: - bpydata.auto_smooth_angle = creaseAngle - bpydata.use_auto_smooth = True + bpydata.set_sharp_from_angle(creaseAngle) # Only ever 1 material per shape if bpymat: diff --git a/materials_utils/functions.py b/materials_utils/functions.py index e771fc6b1..68a94a93e 100644 --- a/materials_utils/functions.py +++ b/materials_utils/functions.py @@ -675,8 +675,7 @@ def mu_set_auto_smooth(self, angle, affect, set_smooth_shading): #bpy.ops.object.shade_smooth() - object.data.use_auto_smooth = 1 - object.data.auto_smooth_angle = angle # 35 degrees as radians + object.data.set_sharp_from_angle(angle) # 35 degrees as radians objects_affected += 1 diff --git a/mesh_tissue/tessellate_numpy.py b/mesh_tissue/tessellate_numpy.py index 504f06544..2616fe141 100644 --- a/mesh_tissue/tessellate_numpy.py +++ b/mesh_tissue/tessellate_numpy.py @@ -800,7 +800,6 @@ def tessellate_patch(props): n2 = n2[masked_faces][:,None,:] else: if normals_mode == 'CUSTOM': - me0.calc_normals_split() normals_split = [0]*len(me0.loops)*3 vertex_indexes = [0]*len(me0.loops) me0.loops.foreach_get('normal', normals_split) diff --git a/object_carver/carver_utils.py b/object_carver/carver_utils.py index c605111fb..891d98ae4 100644 --- a/object_carver/carver_utils.py +++ b/object_carver/carver_utils.py @@ -331,8 +331,7 @@ def CreateBevel(context, CurrentObject): bpy.ops.object.shade_smooth() - context.object.data.use_auto_smooth = True - context.object.data.auto_smooth_angle = 1.0471975 + context.object.data.set_sharp_from_angle(1.0471975) # Restore the active object context.view_layer.objects.active = SavActive diff --git a/render_povray/model_primitives.py b/render_povray/model_primitives.py index 104aa3b48..511d44183 100644 --- a/render_povray/model_primitives.py +++ b/render_povray/model_primitives.py @@ -178,7 +178,6 @@ def pov_cylinder_define(context, op, ob, radius, loc, loc_cap): ob.name = ob.data.name = "PovCylinder" ob.pov.cylinder_radius = radius ob.pov.cylinder_location_cap = vec - ob.data.use_auto_smooth = True ob.pov.object_as = "CYLINDER" ob.update_tag() # as prop set via python not updated in depsgraph @@ -324,7 +323,6 @@ def pov_sphere_define(context, op, ob, loc): bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.hide(unselected=False) bpy.ops.object.mode_set(mode="OBJECT") - ob.data.use_auto_smooth = True bpy.ops.object.shade_smooth() ob.pov.object_as = "SPHERE" ob.update_tag() # as prop set via python not updated in depsgraph @@ -469,7 +467,6 @@ def pov_cone_define(context, op, ob): ob.pov.cone_height = height ob.pov.cone_base_z = zb ob.pov.cone_cap_z = zc - ob.data.use_auto_smooth = True bpy.ops.object.shade_smooth() ob.pov.object_as = "CONE" ob.update_tag() # as prop set via python not updated in depsgraph @@ -657,9 +654,7 @@ def pov_torus_define(context, op, ob): bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.hide(unselected=False) bpy.ops.object.mode_set(mode="OBJECT") - ob.data.use_auto_smooth = True - ob.data.auto_smooth_angle = 0.6 - bpy.ops.object.shade_smooth() + ob.data.set_sharp_from_angle(0.6) ob.pov.object_as = "TORUS" ob.update_tag() # as prop set via python not updated in depsgraph diff --git a/render_povray/model_primitives_topology.py b/render_povray/model_primitives_topology.py index 9870fc46d..7513b2695 100644 --- a/render_povray/model_primitives_topology.py +++ b/render_povray/model_primitives_topology.py @@ -169,8 +169,7 @@ def pov_superellipsoid_define(context, op, ob): bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.hide(unselected=False) bpy.ops.object.mode_set(mode="OBJECT") - ob.data.auto_smooth_angle = 1.3 - bpy.ops.object.shade_smooth() + ob.data.set_sharp_from_angle(1.3) ob.pov.object_as = "SUPERELLIPSOID" ob.update_tag() # as prop set via python not updated in depsgraph @@ -1049,8 +1048,7 @@ def pov_parametric_define(context, op, ob): bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.hide(unselected=False) bpy.ops.object.mode_set(mode="OBJECT") - ob.data.auto_smooth_angle = 0.6 - bpy.ops.object.shade_smooth() + ob.data.set_sharp_from_angle(0.6) ob.pov.object_as = "PARAMETRIC" ob.update_tag() # as prop set via python not updated in depsgraph return{'FINISHED'} @@ -1178,8 +1176,6 @@ class POV_OT_polygon_to_circle_add(Operator): bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.hide(unselected=False) bpy.ops.object.mode_set(mode="OBJECT") - #ob.data.auto_smooth_angle = 0.1 - #bpy.ops.object.shade_smooth() ob.pov.object_as = "POLYCIRCLE" ob.update_tag() # as prop set via python not updated in depsgraph return {"FINISHED"} diff --git a/space_view3d_spacebar_menu/edit_mesh.py b/space_view3d_spacebar_menu/edit_mesh.py index f40e8b9ad..b530e4bbb 100644 --- a/space_view3d_spacebar_menu/edit_mesh.py +++ b/space_view3d_spacebar_menu/edit_mesh.py @@ -180,21 +180,6 @@ class VIEW3D_OT_selecteditVertsEdgesFaces(Operator): return {'FINISHED'} -# ********** Normals / Auto Smooth Menu ********** -# Thanks to marvin.k.breuer for the Autosmooth part of the menu - -def menu_func(self, context): - layout = self.layout - obj = context.object - obj_data = context.active_object.data - layout.separator() - layout.prop(obj_data, "use_auto_smooth", text="Normals: Auto Smooth") - - # Auto Smooth Angle - two tab spaces to align it with the rest of the menu - layout.prop(obj_data, "auto_smooth_angle", - text=" Auto Smooth Angle") - - # List The Classes # classes = ( @@ -215,7 +200,6 @@ def register(): for cls in classes: bpy.utils.register_class(cls) - bpy.types.VIEW3D_MT_edit_mesh_normals.append(menu_func) # Unregister Classes & Hotkeys # def unregister(): @@ -223,7 +207,6 @@ def unregister(): for cls in reversed(classes): bpy.utils.unregister_class(cls) - bpy.types.VIEW3D_MT_edit_mesh_normals.remove(menu_func) if __name__ == "__main__": register() -- 2.30.2 From e017fa7c544b5c4d02d0fd49aa3dd9d9170b4c5a Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 28 Sep 2023 21:07:33 -0400 Subject: [PATCH 2/5] Fix stl import --- io_mesh_stl/blender_utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/io_mesh_stl/blender_utils.py b/io_mesh_stl/blender_utils.py index e7049ca2c..1b1fe55b0 100644 --- a/io_mesh_stl/blender_utils.py +++ b/io_mesh_stl/blender_utils.py @@ -17,10 +17,12 @@ def create_and_link_mesh(name, faces, face_nors, points, global_matrix): mesh.from_pydata(points, [], faces) if face_nors: - # Note: we store 'temp' normals in loops, since validate() may alter final mesh, - # we can only set custom lnors *after* calling it. + # Write imported normals to a temporary attribute so they are interpolated by #mesh.validate(). + # It's important to validate before calling #mesh.normals_split_custom_set() which expects a + # valid mesh. lnors = tuple(chain(*chain(*zip(face_nors, face_nors, face_nors)))) - mesh.loops.foreach_set("normal", lnors) + mesh.attributes.new("temp_custom_normals", 'FLOAT_VECTOR', 'CORNER') + mesh.attributes["temp_custom_normals"].data.foreach_set("vector", lnors) mesh.transform(global_matrix) @@ -29,11 +31,12 @@ def create_and_link_mesh(name, faces, face_nors, points, global_matrix): if face_nors: clnors = array.array('f', [0.0] * (len(mesh.loops) * 3)) - mesh.loops.foreach_get("normal", clnors) + mesh.attributes["temp_custom_normals"].data.foreach_get("vector", clnors) mesh.polygons.foreach_set("use_smooth", [True] * len(mesh.polygons)) mesh.normals_split_custom_set(tuple(zip(*(iter(clnors),) * 3))) + mesh.attributes.remove(mesh.attributes["temp_custom_normals"]) mesh.update() -- 2.30.2 From fdb96411b14630392a9c018197230c4b01ad1e00 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Fri, 29 Sep 2023 12:27:12 -0400 Subject: [PATCH 3/5] Fix FBX normal import --- io_scene_fbx/import_fbx.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py index 2261ad8b4..80c20d01b 100644 --- a/io_scene_fbx/import_fbx.py +++ b/io_scene_fbx/import_fbx.py @@ -1735,23 +1735,23 @@ def blen_read_geom_layer_normal(fbx_obj, mesh, xform=None): bl_norm_dtype = np.single item_size = 3 # try loops, then polygons, then vertices. - tries = ((mesh.loops, "Loops", False, blen_read_geom_array_mapped_polyloop), + tries = ((mesh.attributes["temp_custom_normals"].data, "Loops", False, blen_read_geom_array_mapped_polyloop), (mesh.polygons, "Polygons", True, blen_read_geom_array_mapped_polygon), (mesh.vertices, "Vertices", True, blen_read_geom_array_mapped_vert)) for blen_data, blen_data_type, is_fake, func in tries: bdata = np.zeros((len(blen_data), item_size), dtype=bl_norm_dtype) if is_fake else blen_data - if func(mesh, bdata, "normal", bl_norm_dtype, + if func(mesh, bdata, "vector", bl_norm_dtype, fbx_layer_data, fbx_layer_index, fbx_layer_mapping, fbx_layer_ref, 3, item_size, layer_id, xform, True): if blen_data_type == "Polygons": # To expand to per-loop normals, repeat each per-polygon normal by the number of loops of each polygon. poly_loop_totals = np.empty(len(mesh.polygons), dtype=np.uintc) mesh.polygons.foreach_get("loop_total", poly_loop_totals) loop_normals = np.repeat(bdata, poly_loop_totals, axis=0) - mesh.loops.foreach_set("normal", loop_normals.ravel()) + mesh.attributes["temp_custom_normals"].data.foreach_set("normal", loop_normals.ravel()) elif blen_data_type == "Vertices": # We have to copy vnors to lnors! Far from elegant, but simple. loop_vertex_indices = MESH_ATTRIBUTE_CORNER_VERT.to_ndarray(mesh.attributes) - mesh.loops.foreach_set("normal", bdata[loop_vertex_indices].ravel()) + mesh.attributes["temp_custom_normals"].data.foreach_set("normal", bdata[loop_vertex_indices].ravel()) return True blen_read_geom_array_error_mapping("normal", fbx_layer_mapping) @@ -1875,6 +1875,7 @@ def blen_read_geom(fbx_tmpl, fbx_obj, settings): if settings.use_custom_normals: # Note: we store 'temp' normals in loops, since validate() may alter final mesh, # we can only set custom lnors *after* calling it. + mesh.attributes.new("temp_custom_normals", 'FLOAT_VECTOR', 'CORNER') if geom_mat_no is None: ok_normals = blen_read_geom_layer_normal(fbx_obj, mesh) else: @@ -1886,7 +1887,7 @@ def blen_read_geom(fbx_tmpl, fbx_obj, settings): if ok_normals: bl_nors_dtype = np.single clnors = np.empty(len(mesh.loops) * 3, dtype=bl_nors_dtype) - mesh.loops.foreach_get("normal", clnors) + mesh.attributes["temp_custom_normals"].data.foreach_get("vector", clnors) if not ok_smooth: sharp_face = MESH_ATTRIBUTE_SHARP_FACE.get(attributes) @@ -1897,6 +1898,8 @@ def blen_read_geom(fbx_tmpl, fbx_obj, settings): # Iterating clnors into a nested tuple first is faster than passing clnors.reshape(-1, 3) directly into # normals_split_custom_set. We use clnors.data since it is a memoryview, which is faster to iterate than clnors. mesh.normals_split_custom_set(tuple(zip(*(iter(clnors.data),) * 3))) + if settings.use_custom_normals: + mesh.attributes.remove(mesh.attributes["temp_custom_normals"]) if not ok_smooth: sharp_face = MESH_ATTRIBUTE_SHARP_FACE.get(attributes) -- 2.30.2 From 36b7bc6274943175cb6f3e4b290b0539a0db609b Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Fri, 29 Sep 2023 12:32:07 -0400 Subject: [PATCH 4/5] Remove uses of auto smooth from addons --- io_scene_3ds/export_3ds.py | 13 ++----------- materials_utils/menus.py | 7 ------- materials_utils/preferences.py | 19 ------------------- 3 files changed, 2 insertions(+), 37 deletions(-) diff --git a/io_scene_3ds/export_3ds.py b/io_scene_3ds/export_3ds.py index a19d57b30..b01c54470 100644 --- a/io_scene_3ds/export_3ds.py +++ b/io_scene_3ds/export_3ds.py @@ -1298,12 +1298,9 @@ def make_object_node(ob, translation, rotation, scale, name_id): obj_node_header_chunk.add_variable("name", _3ds_string(sane_name(name))) obj_node_header_chunk.add_variable("flags1", _3ds_ushort(0x0040)) - """Flags2 defines 0x01 for display path, 0x02 use autosmooth, 0x04 object frozen, + """Flags2 defines 0x01 for display path, 0x04 object frozen, 0x10 for motion blur, 0x20 for material morph and bit 0x40 for mesh morph.""" - if ob.type == 'MESH' and ob.data.use_auto_smooth: - obj_node_header_chunk.add_variable("flags2", _3ds_ushort(0x02)) - else: - obj_node_header_chunk.add_variable("flags2", _3ds_ushort(0)) + obj_node_header_chunk.add_variable("flags2", _3ds_ushort(0)) obj_node_header_chunk.add_variable("parent", _3ds_ushort(ROOT_OBJECT)) ''' @@ -1343,12 +1340,6 @@ def make_object_node(ob, translation, rotation, scale, name_id): obj_boundbox.add_variable("max", _3ds_point_3d(ob.bound_box[6])) obj_node.add_subchunk(obj_boundbox) - # Add smooth angle if autosmooth is used - if ob.type == 'MESH' and ob.data.use_auto_smooth: - obj_morph_smooth = _3ds_chunk(OBJECT_MORPH_SMOOTH) - obj_morph_smooth.add_variable("angle", _3ds_float(round(ob.data.auto_smooth_angle, 6))) - obj_node.add_subchunk(obj_morph_smooth) - # Add track chunks for position, rotation, size ob_scale = scale[name] # and collect masterscale if parent is None or (parent.name not in name_id): diff --git a/materials_utils/menus.py b/materials_utils/menus.py index b9ae1ce1e..2f432b4d8 100644 --- a/materials_utils/menus.py +++ b/materials_utils/menus.py @@ -171,13 +171,6 @@ class VIEW3D_MT_materialutilities_specials(bpy.types.Menu): text = "Join by material", icon = "OBJECT_DATAMODE") - layout.separator() - - op = layout.operator(MATERIAL_OT_materialutilities_auto_smooth_angle.bl_idname, - text = "Set Auto Smooth", - icon = "SHADING_SOLID") - op.affect = mu_prefs.set_smooth_affect - op.angle = mu_prefs.auto_smooth_angle class VIEW3D_MT_materialutilities_main(bpy.types.Menu): """Main menu for Material Utilities""" diff --git a/materials_utils/preferences.py b/materials_utils/preferences.py index 0a689c3e7..14fb00d0b 100644 --- a/materials_utils/preferences.py +++ b/materials_utils/preferences.py @@ -68,20 +68,6 @@ class VIEW3D_MT_materialutilities_preferences(AddonPreferences): default = 0 ) - set_smooth_affect: EnumProperty( - name = "Set Auto Smooth Affect", - description = "Which objects to affect", - items = mu_affect_enums, - default = 'SELECTED' - ) - auto_smooth_angle: FloatProperty( - name = "Auto Smooth Angle", - description = "Maximum angle between face normals that will be considered as smooth", - subtype = 'ANGLE', - min = 0, - max = radians(180), - default = radians(35) - ) def draw(self, context): layout = self.layout @@ -105,11 +91,6 @@ class VIEW3D_MT_materialutilities_preferences(AddonPreferences): c.row().prop(self, "link_to", expand = False) c.row().prop(self, "link_to_affect", expand = False) - d = box.box() - d.label(text = "Set Auto Smooth") - d.row().prop(self, "auto_smooth_angle", expand = False) - d.row().prop(self, "set_smooth_affect", expand = False) - box = layout.box() box.label(text = "Miscellaneous") -- 2.30.2 From 1fcde0fe2350cb772759958b7febe7c585b3ecbe Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 19 Oct 2023 17:45:47 +0200 Subject: [PATCH 5/5] Fix use of removed panel --- render_povray/model_gui.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/render_povray/model_gui.py b/render_povray/model_gui.py index 0530bc8bd..8c0849245 100644 --- a/render_povray/model_gui.py +++ b/render_povray/model_gui.py @@ -108,12 +108,6 @@ class PovDataButtonsPanel(properties_data_mesh.MeshButtonsPanel): # We cannot inherit from RNA classes (like e.g. properties_data_mesh.DATA_PT_vertex_groups). # Complex py/bpy/rna interactions (with metaclass and all) simply do not allow it to work. # So we simply have to explicitly copy here the interesting bits. ;) -class DATA_PT_POV_normals(PovDataButtonsPanel, Panel): - bl_label = properties_data_mesh.DATA_PT_normals.bl_label - - draw = properties_data_mesh.DATA_PT_normals.draw - - class DATA_PT_POV_texture_space(PovDataButtonsPanel, Panel): bl_label = properties_data_mesh.DATA_PT_texture_space.bl_label bl_options = properties_data_mesh.DATA_PT_texture_space.bl_options @@ -1066,7 +1060,6 @@ class VIEW_WT_POV_blobcube_add(WorkSpaceTool): classes = ( # ObjectButtonsPanel, # PovDataButtonsPanel, - DATA_PT_POV_normals, DATA_PT_POV_texture_space, DATA_PT_POV_vertex_groups, DATA_PT_POV_shape_keys, -- 2.30.2