Blender V5.3
view3d_gizmo_armature.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_math_vector_c.hh"
11#include "BLI_utildefines.hh"
12
13#include "BKE_action.hh"
14#include "BKE_context.hh"
15#include "BKE_layer.hh"
16#include "BKE_lib_id.hh"
17#include "BKE_object.hh"
18#include "BKE_pose.hh"
19
20#include "DNA_armature_types.h"
21#include "DNA_object_types.h"
22
23#include "ED_gizmo_library.hh"
24
25#include "UI_resources.hh"
26
27#include "MEM_guardedalloc.h"
28
29#include "RNA_access.hh"
30
31#include "WM_types.hh"
32
33#include "view3d_intern.hh" /* own include */
34
35namespace blender {
36
37/* -------------------------------------------------------------------- */
40
41/*
42 * TODO(@ideasman42): Current conversion is a approximation (usable not correct),
43 * we'll need to take the next/previous bones into account to get the tangent directions.
44 * First last matrices from #BKE_pchan_bbone_spline_setup are close but also not quite accurate
45 * since they're not at either end-points on the curve.
46 *
47 * Likely we'll need a function especially to get the first/last orientations.
48 */
49
50#define BBONE_SCALE_Y 3.0f
51
54
55 /* These cannot be packed into a bke::PChanBone, as this struct is allocated with
56 * MEM_new_zeroed() and therefore must be trivial. */
59
60 /* We could remove, keep since at the moment for checking the conversion. */
61 float co[3];
62 int index;
63};
64
68
69static void gizmo_bbone_offset_get(const wmGizmo * /*gz*/, wmGizmoProperty *gz_prop, void *value_p)
70{
71 BoneSplineHandle *bh = static_cast<BoneSplineHandle *>(gz_prop->custom_func.user_data);
72 bPoseChannel *pchan = bh->pchan;
73 Bone *pchan_bone = bh->pchan_bone;
74
75 float *value = static_cast<float *>(value_p);
76 BLI_assert(gz_prop->type->array_length == 3);
77
78 if (bh->index == 0) {
79 bh->co[1] = pchan_bone->ease1 / BBONE_SCALE_Y;
80 bh->co[0] = pchan->curve_in_x;
81 bh->co[2] = pchan->curve_in_z;
82 }
83 else {
84 bh->co[1] = -pchan_bone->ease2 / BBONE_SCALE_Y;
85 bh->co[0] = pchan->curve_out_x;
86 bh->co[2] = pchan->curve_out_z;
87 }
88 copy_v3_v3(value, bh->co);
89}
90
91static void gizmo_bbone_offset_set(const wmGizmo * /*gz*/,
92 wmGizmoProperty *gz_prop,
93 const void *value_p)
94{
95 BoneSplineHandle *bh = static_cast<BoneSplineHandle *>(gz_prop->custom_func.user_data);
96 bPoseChannel *pchan = bh->pchan;
97 Bone *pchan_bone = bh->pchan_bone;
98
99 const float *value = static_cast<const float *>(value_p);
100
101 BLI_assert(gz_prop->type->array_length == 3);
102 copy_v3_v3(bh->co, value);
103
104 if (bh->index == 0) {
105 pchan_bone->ease1 = max_ff(0.0f, bh->co[1] * BBONE_SCALE_Y);
106 pchan->curve_in_x = bh->co[0];
107 pchan->curve_in_z = bh->co[2];
108 }
109 else {
110 pchan_bone->ease2 = max_ff(0.0f, -bh->co[1] * BBONE_SCALE_Y);
111 pchan->curve_out_x = bh->co[0];
112 pchan->curve_out_z = bh->co[2];
113 }
114}
115
117{
118 View3D *v3d = CTX_wm_view3d(C);
120 return false;
121 }
122
123 const Main *bmain = CTX_data_main(C);
124 const Scene *scene = CTX_data_scene(C);
125 ViewLayer *view_layer = CTX_data_view_layer(C);
126 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
127 Base *base = BKE_view_layer_active_base_get(view_layer);
128 if (base && BASE_SELECTABLE(v3d, base)) {
130 if (ob) {
131 const bArmature *arm = id_cast<const bArmature *>(ob->data);
132 if (arm->drawtype == ARM_DRAW_TYPE_B_BONE) {
134 if (pchan && pchan->bone_get(*ob)->segments > 1) {
135 if (BKE_id_is_editable(CTX_data_main(C), &arm->id)) {
136 return true;
137 }
138 }
139 }
140 }
141 }
142 return false;
143}
144
146{
147 const Main *bmain = CTX_data_main(C);
148 const Scene *scene = CTX_data_scene(C);
149 ViewLayer *view_layer = CTX_data_view_layer(C);
150 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
153
154 const wmGizmoType *gzt_move = WM_gizmotype_find("GIZMO_GT_move_3d", true);
155
157 gzgroup->customdata = bspline_group;
158
159 /* Handles */
160 for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
161 wmGizmo *gz;
162 gz = bspline_group->handles[i].gizmo = WM_gizmo_new_ptr(gzt_move, gzgroup, nullptr);
163 RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_MOVE_STYLE_RING_2D);
164 RNA_enum_set(gz->ptr,
165 "draw_options",
168
171
172 gz->scale_basis = 0.06f;
173
174 if (i == 0) {
175 copy_v3_v3(gz->matrix_basis[3], pchan->loc);
176 }
177 }
178}
179
181{
182 const Main *bmain = CTX_data_main(C);
183 const Scene *scene = CTX_data_scene(C);
184 ViewLayer *view_layer = CTX_data_view_layer(C);
185 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
187
188 if (!gzgroup->customdata) {
189 return;
190 }
191
192 BoneSplineWidgetGroup *bspline_group = static_cast<BoneSplineWidgetGroup *>(gzgroup->customdata);
194 Bone *pchan_bone = pchan ? pchan->bone_get(*ob) : nullptr;
195
196 /* Handles */
197 for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
198 wmGizmo *gz = bspline_group->handles[i].gizmo;
199 bspline_group->handles[i].pchan = pchan;
200 bspline_group->handles[i].pchan_bone = pchan_bone;
201 bspline_group->handles[i].index = i;
202
203 float mat[4][4];
205 mat, ob->object_to_world().ptr(), (i == 0) ? pchan->disp_mat : pchan->disp_tail_mat);
206 copy_m4_m4(gz->matrix_space, mat);
207
208 /* need to set property here for undo. TODO: would prefer to do this in _init. */
212 params.range_get_fn = nullptr;
213 params.user_data = &bspline_group->handles[i];
214 WM_gizmo_target_property_def_func(gz, "offset", &params);
215 }
216}
217
219{
220 gzgt->name = "Armature Spline Widgets";
221 gzgt->idname = "VIEW3D_GGT_armature_spline";
222
224
229}
230
232
233} // namespace blender
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
void get_color_3fv(int colorid, float col[3])
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
const wmGizmoType * WM_gizmotype_find(const StringRef idname, bool quiet)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
static void gizmo_bbone_offset_set(const wmGizmo *, wmGizmoProperty *gz_prop, const void *value_p)
ViewLayer * CTX_data_view_layer(const bContext *C)
static void WIDGETGROUP_armature_spline_setup(const bContext *C, wmGizmoGroup *gzgroup)
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
Object * BKE_object_pose_armature_get(Object *ob)
Definition object.cc:2507
@ ED_GIZMO_MOVE_DRAW_FLAG_FILL
@ ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
@ ED_GIZMO_MOVE_STYLE_RING_2D
static bool WIDGETGROUP_armature_spline_poll(const bContext *C, wmGizmoGroupType *)
@ WM_GIZMOGROUPTYPE_PERSISTENT
@ WM_GIZMO_DRAW_VALUE
void VIEW3D_GGT_armature_spline(wmGizmoGroupType *gzgt)
Main * CTX_data_main(const bContext *C)
static void gizmo_bbone_offset_get(const wmGizmo *, wmGizmoProperty *gz_prop, void *value_p)
MINLINE float max_ff(float a, float b)
bPoseChannel * BKE_pose_channel_active_if_bonecoll_visible(Object *ob) ATTR_WARN_UNUSED_RESULT
Scene * CTX_data_scene(const bContext *C)
wmKeyMap * WM_gizmogroup_setup_keymap_generic_maybe_drag(const wmGizmoGroupType *, wmKeyConfig *kc)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
wmGizmo * WM_gizmo_new_ptr(const wmGizmoType *gzt, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:84
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
static void WIDGETGROUP_armature_spline_refresh(const bContext *C, wmGizmoGroup *gzgroup)
View3D * CTX_wm_view3d(const bContext *C)
struct Object * object
eView3D_GizmoFlag gizmo_flag
eArmature_Drawtype drawtype
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnSetupKeymap setup_keymap
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoPropertyFnSet value_set_fn
wmGizmoPropertyFnRangeGet range_get_fn
wmGizmoPropertyFnGet value_get_fn
const wmGizmoPropertyType * type
struct blender::wmGizmoProperty::@216011055122204070127377170176223170047224321165 custom_func
float matrix_basis[4][4]
float matrix_space[4][4]
#define BBONE_SCALE_Y