49 defgroups.
add(dg.name);
56 for (; md; md = md->
next) {
75 bone_deformed_vgroups.
add(channel.
name);
80 return bone_deformed_vgroups;
88 const int vertex_groups_num,
89 const Span<bool> vertex_group_is_bone_deformed,
90 const FunctionRef<
bool(
int)> vertex_group_is_locked)
98 float sum_weights_total = 0.0f;
99 float sum_weights_locked = 0.0f;
100 float sum_weights_unlocked = 0.0f;
102 int unlocked_num = 0;
107 if (dw.
def_nr >= vertex_groups_num || !vertex_group_is_bone_deformed[dw.
def_nr] ||
113 sum_weights_total += dw.
weight;
115 if (vertex_group_is_locked(dw.
def_nr)) {
117 sum_weights_locked += dw.
weight;
121 sum_weights_unlocked += dw.
weight;
126 if (sum_weights_total == 1.0f) {
131 if (unlocked_num == 0) {
133 return (locked_num == 1);
142 if (dw.
def_nr < vertex_groups_num && vertex_group_is_bone_deformed[dw.
def_nr] &&
143 !vertex_group_is_locked(dw.
def_nr))
149 return (sum_weights_locked == 1.0f);
154 if (sum_weights_unlocked != 0.0f) {
155 const float normalize_factor = (1.0f - sum_weights_locked) / sum_weights_unlocked;
159 if (dw.
def_nr < vertex_groups_num && vertex_group_is_bone_deformed[dw.
def_nr] &&
171 (1.0f - sum_weights_locked) / unlocked_num, 0.0f, 1.0f);
175 if (dw.
def_nr < vertex_groups_num && vertex_group_is_bone_deformed[dw.
def_nr] &&
178 dw.
weight = weight_remainder;
186 const int active_vertex_group,
188 const Span<bool> vertex_group_is_bone_deformed)
192 const auto active_vertex_group_is_locked = [&](
const int vertex_group_index) {
193 return vertex_group_is_locked[vertex_group_index] || vertex_group_index == active_vertex_group;
196 vertex_group_is_locked.
size(),
197 vertex_group_is_bone_deformed,
198 active_vertex_group_is_locked);
205 const auto active_vertex_group_is_unlocked = [&](
const int vertex_group_index) {
206 return vertex_group_is_locked[vertex_group_index];
209 vertex_group_is_locked.
size(),
210 vertex_group_is_bone_deformed,
211 active_vertex_group_is_unlocked);
219 if (bone !=
nullptr) {
221 count += bone_callback(ob, bone) ? 1 : 0;
240 const bArmature &armature = *id_cast<const bArmature *>(ob_armature.
data);
243 object, armature, [&](
Object &
object,
const Bone *bone) {
255 return added_vertex_groups > 0;
264 object, armature, [&](
Object &
object,
const Bone *bone) {
273 r_skinnable_bones.
append(bone);
279 if (added_vertex_groups <= 0) {
291 for (const int i : range) {
292 const Bone *bone = bones[i];
293 roots[i] = math::transform_point(transform, float3(bone->arm_head));
294 tips[i] = math::transform_point(transform, float3(bone->arm_tail));
305 bDeformGroup *defgroup = MEM_new<bDeformGroup>(__func__);
308 def_nr =
curves.vertex_group_names.count() - 1;
319 const bArmature &armature = *id_cast<const bArmature *>(ob_armature.
data);
320 const float4x4 armature_to_world = ob_armature.object_to_world();
326 armature,
object, skinnable_bones, deform_group_names))
341 const float4x4 layer_to_world =
layer.to_world_space(
object);
349 for (
const int bone_i : skinnable_bones.
index_range()) {
350 const Bone *bone = skinnable_bones[bone_i];
351 const char *deform_group_name = deform_group_names[bone_i].c_str();
352 const float3 bone_root = roots[bone_i];
353 const float3 bone_tip = tips[bone_i];
359 for (
const int point_i :
curves.points_range()) {
366 if (weight != 0.0f) {
367 weights.
set(point_i, weight);
378 const bArmature &armature = *id_cast<const bArmature *>(ob_armature.
data);
379 const float4x4 armature_to_world = ob_armature.object_to_world();
381 const float default_ratio = 0.1f;
382 const float default_decay = 0.8f;
387 armature,
object, skinnable_bones, deform_group_names))
399 const auto get_weight = [](
const float dist,
const float decay_rad,
const float diff_rad) {
400 return (dist < decay_rad) ? 1.0f :
408 const float4x4 layer_to_world =
layer.to_world_space(
object);
416 for (
const int bone_i : skinnable_bones.
index_range()) {
417 const char *deform_group_name = deform_group_names[bone_i].c_str();
418 const float3 bone_root = roots[bone_i];
419 const float3 bone_tip = tips[bone_i];
422 const float decay_rad = radius_squared - (radius_squared * default_decay);
423 const float diff_rad = radius_squared - decay_rad;
429 for (
const int point_i :
curves.points_range()) {
432 const float weight = (dist_to_bone > radius_squared) ?
434 get_weight(dist_to_bone, decay_rad, diff_rad);
435 if (weight != 0.0f) {
436 weights.
set(point_i, weight);
458 if (object_defgroup_nr == -1) {
476 for (
const int i : range) {
483 if (drawing_defgroup_nr == -1) {
497 const float4x4 layer_to_world =
layer.to_world_space(*ob_eval);
501 std::optional<ed::curves::FindClosestData> new_closest_elem =
511 if (new_closest_elem) {
512 new_closest.
elem = *new_closest_elem;
529 closest.drawing->strokes().deform_verts(),
closest.active_defgroup_index);
546 ot->
name =
"Weight Paint Sample Weight";
547 ot->
idname =
"GREASE_PENCIL_OT_weight_sample";
549 "Set the weight of the Draw tool to the weight of the vertex under the mouse cursor";
581 if (
paint ==
nullptr) {
585 if (
brush ==
nullptr) {
594 ot->
name =
"Weight Paint Toggle Direction";
595 ot->
idname =
"GREASE_PENCIL_OT_weight_toggle_direction";
596 ot->
description =
"Toggle Add/Subtract for the weight paint draw tool";
614 if (active_index == -1) {
632 active_defgroup->
name);
633 if (drawing_vgroup_index == -1) {
638 curves.deform_verts_for_write(), drawing_vgroup_index);
639 if (weights.
size() == 0) {
644 const float invert_weight = 1.0f - weights[i];
645 weights.
set(i, invert_weight);
671 ot->
name =
"Invert Weight";
672 ot->
idname =
"GREASE_PENCIL_OT_weight_invert";
673 ot->
description =
"Invert the weight of active vertex group";
688 if (object_defgroup_nr == -1) {
707 for (const int drawing : drawing_range) {
708 bke::CurvesGeometry &curves = drawings[drawing].drawing.strokes_for_write();
709 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
712 if (!attributes.contains(object_defgroup->name)) {
716 bke::SpanAttributeWriter<float> weights = attributes.lookup_for_write_span<float>(
717 object_defgroup->name);
718 geometry::smooth_curve_attribute(curves.curves_range(),
719 curves.points_by_curve(),
720 VArray<bool>::from_single(true, curves.points_num()),
740 ot->
name =
"Smooth Vertex Group";
741 ot->
idname =
"GREASE_PENCIL_OT_vertex_group_smooth";
742 ot->
description =
"Smooth the weights of the active vertex group";
753 RNA_def_int(ot->
srna,
"repeat", 1, 1, 10000,
"Iterations",
"", 1, 200);
761 if (object_defgroup_nr == -1) {
778 bool changed =
false;
779 for (
const int frame_i : drawings_per_frame.
index_range()) {
786 [&](
const IndexRange drawing_range,
const float &drawing_weight_init) {
787 float max_weight_in_drawing = drawing_weight_init;
788 for (const int drawing_i : drawing_range) {
789 const bke::CurvesGeometry &curves = drawings[drawing_i].drawing.strokes();
790 const bke::AttributeAccessor attributes = curves.attributes();
793 if (!attributes.contains(object_defgroup->name)) {
798 const VArray<float> weights = *curves.attributes().lookup_or_default<float>(
799 object_defgroup->name, bke::AttrDomain::Point, 0.0f);
800 const float max_weight_in_points = threading::parallel_reduce(
801 weights.index_range(),
803 max_weight_in_drawing,
804 [&](const IndexRange point_range, const float &init) {
805 float max_weight = init;
806 for (const int point_i : point_range) {
807 max_weight = math::max(max_weight, weights[point_i]);
811 [](const float a, const float b) { return math::max(a, b); });
812 max_weight_in_drawing = math::max(max_weight_in_drawing, max_weight_in_points);
814 return max_weight_in_drawing;
816 [](
const float a,
const float b) {
return math::max(a,
b); });
818 if (
ELEM(max_weight_in_frame, 0.0f, 1.0f)) {
826 for (const int drawing_i : drawing_range) {
827 bke::CurvesGeometry &curves = drawings[drawing_i].drawing.strokes_for_write();
828 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
831 if (!attributes.contains(object_defgroup->name)) {
835 bke::SpanAttributeWriter<float> weights = attributes.lookup_for_write_span<float>(
836 object_defgroup->name);
837 threading::parallel_for(
838 weights.span.index_range(), 1024, [&](const IndexRange point_range) {
839 for (const int point_i : point_range) {
840 weights.span[point_i] /= max_weight_in_frame;
853 return OPERATOR_FINISHED;
859 ot->
name =
"Normalize Vertex Group";
860 ot->
idname =
"GREASE_PENCIL_OT_vertex_group_normalize";
861 ot->
description =
"Normalize weights of the active vertex group";
884 object_locked_defgroups.
add(dg.name);
896 for (const int drawing_i : drawing_range) {
897 bke::CurvesGeometry &curves = drawings[drawing_i].drawing.strokes_for_write();
900 int active_vertex_group = -1;
901 if (object_defgroup && lock_active_group) {
902 active_vertex_group = BKE_defgroup_name_index(&curves.vertex_group_names,
903 object_defgroup->name);
907 Vector<bool> vertex_group_is_locked;
908 Vector<bool> vertex_group_is_included;
909 for (bDeformGroup &dg : curves.vertex_group_names) {
910 vertex_group_is_locked.append(object_locked_defgroups.contains(dg.name));
912 vertex_group_is_included.append(true);
917 MutableSpan<MDeformVert> deform_verts = curves.deform_verts_for_write();
918 threading::parallel_for(curves.points_range(), 1024, [&](const IndexRange point_range) {
919 for (const int point_i : point_range) {
920 normalize_vertex_weights(deform_verts[point_i],
922 vertex_group_is_locked,
923 vertex_group_is_included);
938 ot->
name =
"Normalize All Vertex Groups";
939 ot->
idname =
"GREASE_PENCIL_OT_vertex_group_normalize_all";
941 "Normalize the weights of all vertex groups, so that for each vertex, the sum of all "
956 "Keep the values of the active group while normalizing others");
Low-level operations for grease pencil.
Object is a sort of wrapper for general info.
bool closest(btVector3 &v)
MutableSpan< T > as_mutable_span()
IndexRange index_range() const
constexpr IndexRange()=default
bool contains(const Key &key) const
constexpr int64_t size() const
constexpr IndexRange index_range() const
IndexRange index_range() const
void set(const int64_t index, T value)
void append(const T &value)
IndexRange index_range() const
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
float3x3 scale(float3x3 mat, float2 scale)
GeometryDeformation get_evaluated_grease_pencil_drawing_deformation(const Object *ob_eval, const Object &ob_orig, const bke::greasepencil::Drawing &drawing_orig)
VMutableArray< float > varray_for_mutable_deform_verts(MutableSpan< MDeformVert > dverts, int defgroup_index)
VArray< float > varray_for_deform_verts(Span< MDeformVert > dverts, int defgroup_index)
std::optional< FindClosestData > closest_elem_find_screen_space(const ViewContext &vc, const OffsetIndices< int > points_by_curve, const Span< float3 > positions, const VArray< bool > &cyclic, const float4x4 &projection, const IndexMask &mask, const bke::AttrDomain domain, const int2 coord, const FindClosestData &initial_closest)
static bool normalize_vertex_weights_try(MDeformVert &dvert, const int vertex_groups_num, const Span< bool > vertex_group_is_bone_deformed, const FunctionRef< bool(int)> vertex_group_is_locked)
static wmOperatorStatus grease_pencil_weight_invert_exec(bContext *C, wmOperator *op)
Set< std::string > get_bone_deformed_vertex_group_names(const Object &object)
static wmOperatorStatus vertex_group_smooth_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_vertex_group_smooth(wmOperatorType *ot)
static int foreach_bone_in_armature(Object &ob, const bArmature &armature, const FunctionRef< bool(Object &, const Bone *)> bone_callback)
static void GREASE_PENCIL_OT_vertex_group_normalize(wmOperatorType *ot)
void add_armature_automatic_weights(Scene &scene, Object &object, const Object &ob_armature)
IndexMask retrieve_visible_points(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
Vector< DrawingInfo > retrieve_visible_drawings(const Scene &scene, const GreasePencil &grease_pencil, const bool do_onion_skinning)
static wmOperatorStatus vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
static bool toggle_weight_tool_direction_poll(bContext *C)
static wmOperatorStatus vertex_group_normalize_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_weight_sample(wmOperatorType *ot)
void normalize_vertex_weights(MDeformVert &dvert, const int active_vertex_group, const Span< bool > vertex_group_is_locked, const Span< bool > vertex_group_is_bone_deformed)
static void GREASE_PENCIL_OT_vertex_group_normalize_all(wmOperatorType *ot)
static void GREASE_PENCIL_OT_weight_invert(wmOperatorType *ot)
static wmOperatorStatus toggle_weight_tool_direction_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_weight_toggle_direction(wmOperatorType *ot)
static wmOperatorStatus weight_sample_invoke(bContext *C, wmOperator *, const wmEvent *event)
void add_armature_envelope_weights(Scene &scene, Object &object, const Object &ob_armature)
bool grease_pencil_weight_painting_poll(bContext *C)
bool add_armature_vertex_groups(Object &object, const Object &ob_armature)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
static int foreach_bone_in_armature_ex(Object &ob, const Bone *bone, const FunctionRef< bool(Object &, const Bone *)> bone_callback)
static bool get_skinnable_bones_and_deform_group_names(const bArmature &armature, Object &object, Vector< const Bone * > &r_skinnable_bones, Vector< std::string > &r_deform_group_names)
Array< Vector< MutableDrawingInfo > > retrieve_editable_drawings_grouped_per_frame(const Scene &scene, GreasePencil &grease_pencil)
static void get_root_and_tips_of_bones(Span< const Bone * > bones, const float4x4 &transform, MutableSpan< float3 > roots, MutableSpan< float3 > tips)
static bool grease_pencil_vertex_group_weight_poll(bContext *C)
static int lookup_or_add_deform_group_index(CurvesGeometry &curves, const StringRef name)
T clamp(const T &a, const T &min, const T &max)
T interpolate(const T &a, const T &b, const FactorT &t)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
void parallel_for_each(Range &&range, const Function &function)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
const ListBaseT< bDeformGroup > * BKE_object_defgroup_list(const Object *ob)
@ GPWEIGHT_BRUSH_TYPE_DRAW
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
bDeformGroup * BKE_object_defgroup_find_name(const Object *ob, StringRef name)
float mat4_to_scale(const float mat[4][4])
@ eModifierType_GreasePencilArmature
int RNA_int_get(PointerRNA *ptr, const char *name)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
MatBase< float, 4, 4 > float4x4
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
float distfactor_to_bone(const float3 &position, const float3 &head, const float3 &tail, float radius_head, float radius_tail, float falloff_distance)
void BKE_brush_tag_unsaved_changes(Brush *brush)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
void ED_operatortypes_grease_pencil_weight_paint()
struct bDeformGroup * BKE_object_defgroup_add_name(struct Object *ob, const char *name)
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Paint * BKE_paint_get_active_from_context(const bContext *C)
void BKE_brush_weight_set(Paint *paint, Brush *brush, float value)
float4x4 ED_view3d_ob_project_mat_get_from_obmat(const RegionView3D *rv3d, const float4x4 &obmat)
Brush * BKE_paint_brush(Paint *paint)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_DEPENDS_ON_CURSOR
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
int BKE_object_defgroup_active_index_get(const Object *ob)
ListBaseT< Bone > childbase
ListBaseT< struct bDeformGroup > vertex_group_names
const c_style_mat & ptr() const
struct ModifierData * next
struct ToolSettings * toolsettings
ListBaseT< Bone > bonebase
int active_defgroup_index
ed::curves::FindClosestData elem
const bke::greasepencil::Drawing * drawing
const bke::greasepencil::Drawing & drawing
bke::greasepencil::Drawing & drawing
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
struct ReportList * reports