Mesh: Add Auto Smooth option to Shade Smooth operator

Add a property to the **Shade Smooth** operator to quickly enable the Mesh `use_auto_smooth` option.

The `Angle` property is exposed in the **Adjust Last Operation** panel to make it easy to tweak on multiple objects without having to go to the Properties editor.

The operator is exposed in the `Object` menu and `Object Context Menu`.

=== Demo ===

{F13066173, size=full}

Regarding the implementation, there are multiple ways to go about this (like making a whole new operator altogether), but I think a property is the cleanest/simplest.

I imagine there are simpler ways to achieve this without duplicating the `use_auto_smooth` property in the operator itself (getting it from the Mesh props?), but I couldn't find other operators doing something similar.

Reviewed By: #modeling, mont29

Differential Revision: https://developer.blender.org/D14894
This commit is contained in:
Pablo Vazquez 2022-05-11 11:54:14 +02:00 committed by Pablo Vazquez
parent 6bd270f3af
commit eef98e66cf
Notes: blender-bot 2023-03-24 17:05:22 +01:00
Referenced by commit 3eb3d363e1, Fix: Build error on windows.
4 changed files with 41 additions and 1 deletions

View File

@ -2346,6 +2346,7 @@ class VIEW3D_MT_object(Menu):
layout.separator()
layout.operator("object.shade_smooth")
layout.operator("object.shade_smooth", text="Shade Auto Smooth").use_auto_smooth = True
layout.operator("object.shade_flat")
layout.separator()
@ -2588,7 +2589,8 @@ class VIEW3D_MT_object_context_menu(Menu):
# Shared among some object types.
if obj is not None:
if obj.type in {'MESH', 'CURVE', 'SURFACE'}:
layout.operator("object.shade_smooth", text="Shade Smooth")
layout.operator("object.shade_smooth")
layout.operator("object.shade_smooth", text="Shade Auto Smooth").use_auto_smooth = True
layout.operator("object.shade_flat", text="Shade Flat")
layout.separator()

View File

@ -204,6 +204,7 @@ bool BKE_mesh_material_index_used(struct Mesh *me, short index);
void BKE_mesh_material_index_clear(struct Mesh *me);
void BKE_mesh_material_remap(struct Mesh *me, const unsigned int *remap, unsigned int remap_len);
void BKE_mesh_smooth_flag_set(struct Mesh *me, bool use_smooth);
void BKE_mesh_auto_smooth_flag_set(struct Mesh *me, bool use_auto_smooth, float auto_smooth_angle);
/**
* Needed after converting a mesh with subsurf optimal display to mesh.

View File

@ -1577,6 +1577,19 @@ void BKE_mesh_smooth_flag_set(Mesh *me, const bool use_smooth)
}
}
void BKE_mesh_auto_smooth_flag_set(Mesh *me,
const bool use_auto_smooth,
const float auto_smooth_angle)
{
if (use_auto_smooth) {
me->flag |= ME_AUTOSMOOTH;
me->smoothresh = auto_smooth_angle;
}
else {
me->flag &= ~ME_AUTOSMOOTH;
}
}
int poly_find_loop_from_vert(const MPoly *poly, const MLoop *loopstart, uint vert)
{
for (int j = 0; j < poly->totloop; j++, loopstart++) {

View File

@ -17,6 +17,7 @@
#include "BLI_blenlib.h"
#include "BLI_ghash.h"
#include "BLI_math_rotation.h"
#include "BLI_utildefines.h"
#include "BLT_translation.h"
@ -86,6 +87,7 @@
#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"
#include "RNA_types.h"
#include "UI_interface_icons.h"
@ -1461,6 +1463,8 @@ void OBJECT_OT_paths_clear(wmOperatorType *ot)
static int shade_smooth_exec(bContext *C, wmOperator *op)
{
const bool use_smooth = STREQ(op->idname, "OBJECT_OT_shade_smooth");
const bool use_auto_smooth = RNA_boolean_get(op->ptr, "use_auto_smooth");
const float auto_smooth_angle = RNA_float_get(op->ptr, "auto_smooth_angle");
bool changed_multi = false;
bool has_linked_data = false;
@ -1508,6 +1512,7 @@ static int shade_smooth_exec(bContext *C, wmOperator *op)
bool changed = false;
if (ob->type == OB_MESH) {
BKE_mesh_smooth_flag_set(ob->data, use_smooth);
BKE_mesh_auto_smooth_flag_set(ob->data, use_auto_smooth, auto_smooth_angle);
BKE_mesh_batch_cache_dirty_tag(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
changed = true;
}
@ -1577,6 +1582,25 @@ void OBJECT_OT_shade_smooth(wmOperatorType *ot)
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
PropertyRNA *prop;
prop = RNA_def_boolean(
ot->srna,
"use_auto_smooth",
false,
"Auto Smooth",
"Enable automatic smooth based on smooth/sharp faces/edges and angle between faces");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
prop = RNA_def_property(ot->srna, "auto_smooth_angle", PROP_FLOAT, PROP_ANGLE);
RNA_def_property_range(prop, 0.0f, DEG2RADF(180.0f));
RNA_def_property_float_default(prop, DEG2RADF(30.0f));
RNA_def_property_ui_text(prop,
"Angle",
"Maximum angle between face normals that will be considered as smooth"
"(unused if custom split normals data are available)");
}
/** \} */