Blender  V3.3
view3d_gizmo_armature.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_blenlib.h"
8 #include "BLI_math.h"
9 #include "BLI_utildefines.h"
10 
11 #include "BKE_action.h"
12 #include "BKE_armature.h"
13 #include "BKE_context.h"
14 #include "BKE_layer.h"
15 #include "BKE_object.h"
16 
17 #include "DNA_armature_types.h"
18 #include "DNA_object_types.h"
19 
20 #include "ED_armature.h"
21 #include "ED_gizmo_library.h"
22 #include "ED_screen.h"
23 
24 #include "UI_resources.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "RNA_access.h"
29 
30 #include "WM_api.h"
31 #include "WM_types.h"
32 
33 #include "view3d_intern.h" /* own include */
34 
35 /* -------------------------------------------------------------------- */
39 /*
40  * TODO(campbell): Current conversion is a approximation (usable not correct),
41  * we'll need to take the next/previous bones into account to get the tangent directions.
42  * First last matrices from 'BKE_pchan_bbone_spline_setup' are close but also not quite accurate
43  * since they're not at either end-points on the curve.
44  *
45  * Likely we'll need a function especially to get the first/last orientations.
46  */
47 
48 #define BBONE_SCALE_Y 3.0f
49 
53  /* We could remove, keep since at the moment for checking the conversion. */
54  float co[3];
55  int index;
56 };
57 
59  struct BoneSplineHandle handles[2];
60 };
61 
62 static void gizmo_bbone_offset_get(const wmGizmo *UNUSED(gz),
63  wmGizmoProperty *gz_prop,
64  void *value_p)
65 {
66  struct BoneSplineHandle *bh = gz_prop->custom_func.user_data;
67  bPoseChannel *pchan = bh->pchan;
68 
69  float *value = value_p;
70  BLI_assert(gz_prop->type->array_length == 3);
71 
72  if (bh->index == 0) {
73  bh->co[1] = pchan->bone->ease1 / BBONE_SCALE_Y;
74  bh->co[0] = pchan->curve_in_x;
75  bh->co[2] = pchan->curve_in_z;
76  }
77  else {
78  bh->co[1] = -pchan->bone->ease2 / BBONE_SCALE_Y;
79  bh->co[0] = pchan->curve_out_x;
80  bh->co[2] = pchan->curve_out_z;
81  }
82  copy_v3_v3(value, bh->co);
83 }
84 
85 static void gizmo_bbone_offset_set(const wmGizmo *UNUSED(gz),
86  wmGizmoProperty *gz_prop,
87  const void *value_p)
88 {
89  struct BoneSplineHandle *bh = gz_prop->custom_func.user_data;
90  bPoseChannel *pchan = bh->pchan;
91 
92  const float *value = value_p;
93 
94  BLI_assert(gz_prop->type->array_length == 3);
95  copy_v3_v3(bh->co, value);
96 
97  if (bh->index == 0) {
98  pchan->bone->ease1 = max_ff(0.0f, bh->co[1] * BBONE_SCALE_Y);
99  pchan->curve_in_x = bh->co[0];
100  pchan->curve_in_z = bh->co[2];
101  }
102  else {
103  pchan->bone->ease2 = max_ff(0.0f, -bh->co[1] * BBONE_SCALE_Y);
104  pchan->curve_out_x = bh->co[0];
105  pchan->curve_out_z = bh->co[2];
106  }
107 }
108 
110 {
111  View3D *v3d = CTX_wm_view3d(C);
113  return false;
114  }
115 
116  ViewLayer *view_layer = CTX_data_view_layer(C);
117  Base *base = BASACT(view_layer);
118  if (base && BASE_SELECTABLE(v3d, base)) {
120  if (ob) {
121  const bArmature *arm = ob->data;
122  if (arm->drawtype == ARM_B_BONE) {
124  if (pchan && pchan->bone->segments > 1) {
125  return true;
126  }
127  }
128  }
129  }
130  return false;
131 }
132 
134 {
135  ViewLayer *view_layer = CTX_data_view_layer(C);
136  Object *ob = BKE_object_pose_armature_get(OBACT(view_layer));
138 
139  const wmGizmoType *gzt_move = WM_gizmotype_find("GIZMO_GT_move_3d", true);
140 
141  struct BoneSplineWidgetGroup *bspline_group = MEM_callocN(sizeof(struct BoneSplineWidgetGroup),
142  __func__);
143  gzgroup->customdata = bspline_group;
144 
145  /* Handles */
146  for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
147  wmGizmo *gz;
148  gz = bspline_group->handles[i].gizmo = WM_gizmo_new_ptr(gzt_move, gzgroup, NULL);
149  RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_MOVE_STYLE_RING_2D);
150  RNA_enum_set(gz->ptr,
151  "draw_options",
154 
157 
158  gz->scale_basis = 0.06f;
159 
160  if (i == 0) {
161  copy_v3_v3(gz->matrix_basis[3], pchan->loc);
162  }
163  }
164 }
165 
167 {
168  ViewLayer *view_layer = CTX_data_view_layer(C);
169  Object *ob = BKE_object_pose_armature_get(OBACT(view_layer));
170 
171  if (!gzgroup->customdata) {
172  return;
173  }
174 
175  struct BoneSplineWidgetGroup *bspline_group = gzgroup->customdata;
177 
178  /* Handles */
179  for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
180  wmGizmo *gz = bspline_group->handles[i].gizmo;
181  bspline_group->handles[i].pchan = pchan;
182  bspline_group->handles[i].index = i;
183 
184  float mat[4][4];
185  mul_m4_m4m4(mat, ob->obmat, (i == 0) ? pchan->disp_mat : pchan->disp_tail_mat);
186  copy_m4_m4(gz->matrix_space, mat);
187 
188  /* need to set property here for undo. TODO: would prefer to do this in _init. */
190  "offset",
191  &(const struct wmGizmoPropertyFnParams){
192  .value_get_fn = gizmo_bbone_offset_get,
193  .value_set_fn = gizmo_bbone_offset_set,
194  .range_get_fn = NULL,
195  .user_data = &bspline_group->handles[i],
196  });
197  }
198 }
199 
201 {
202  gzgt->name = "Armature Spline Widgets";
203  gzgt->idname = "VIEW3D_GGT_armature_spline";
204 
206 
211 }
212 
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_active_if_layer_visible(struct Object *ob)
Definition: action.c:720
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_pose_armature_get(struct Object *ob)
Definition: object.cc:2511
#define BLI_assert(a)
Definition: BLI_assert.h:46
MINLINE float max_ff(float a, float b)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:259
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define ARRAY_SIZE(arr)
#define UNUSED(x)
@ ARM_B_BONE
@ BASE_SELECTABLE
Object is a sort of wrapper for general info.
#define BASACT(_view_layer)
#define OBACT(_view_layer)
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_CONTEXT
@ ED_GIZMO_MOVE_STYLE_RING_2D
@ ED_GIZMO_MOVE_DRAW_FLAG_FILL
@ ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void UI_GetThemeColor3fv(int colorid, float col[3])
Definition: resources.c:1165
@ TH_GIZMO_HI
Definition: UI_resources.h:304
@ TH_GIZMO_PRIMARY
Definition: UI_resources.h:305
@ WM_GIZMO_DRAW_VALUE
@ WM_GIZMOGROUPTYPE_3D
@ WM_GIZMOGROUPTYPE_PERSISTENT
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:5015
struct Object * object
struct BoneSplineHandle handles[2]
float ease1
short segments
float ease2
float obmat[4][4]
void * data
char gizmo_flag
struct Bone * bone
float disp_mat[4][4]
float disp_tail_mat[4][4]
wmGizmoGroupFnSetupKeymap setup_keymap
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
const char * name
const struct wmGizmoPropertyType * type
struct wmGizmoProperty::@1185 custom_func
float matrix_basis[4][4]
float color_hi[4]
float color[4]
struct PointerRNA * ptr
float scale_basis
float matrix_space[4][4]
static void WIDGETGROUP_armature_spline_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_bbone_offset_get(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, void *value_p)
static bool WIDGETGROUP_armature_spline_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
static void WIDGETGROUP_armature_spline_setup(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_bbone_offset_set(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, const void *value_p)
#define BBONE_SCALE_Y
void VIEW3D_GGT_armature_spline(wmGizmoGroupType *gzgt)
wmGizmo * WM_gizmo_new_ptr(const wmGizmoType *gzt, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:81
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:304
wmKeyMap * WM_gizmogroup_setup_keymap_generic_maybe_drag(const wmGizmoGroupType *UNUSED(gzgt), wmKeyConfig *kc)
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
const wmGizmoType * WM_gizmotype_find(const char *idname, bool quiet)
Definition: wm_gizmo_type.c:45