Blender  V2.93
view3d_gizmo_armature.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_blenlib.h"
22 #include "BLI_math.h"
23 #include "BLI_utildefines.h"
24 
25 #include "BKE_action.h"
26 #include "BKE_armature.h"
27 #include "BKE_context.h"
28 #include "BKE_layer.h"
29 #include "BKE_object.h"
30 
31 #include "DNA_armature_types.h"
32 #include "DNA_object_types.h"
33 
34 #include "ED_armature.h"
35 #include "ED_gizmo_library.h"
36 #include "ED_screen.h"
37 
38 #include "UI_resources.h"
39 
40 #include "MEM_guardedalloc.h"
41 
42 #include "RNA_access.h"
43 
44 #include "WM_api.h"
45 #include "WM_types.h"
46 
47 #include "view3d_intern.h" /* own include */
48 
49 /* -------------------------------------------------------------------- */
53 /*
54  * TODO(campbell): Current conversion is a approximation (usable not correct),
55  * we'll need to take the next/previous bones into account to get the tangent directions.
56  * First last matrices from 'BKE_pchan_bbone_spline_setup' are close but also not quite accurate
57  * since they're not at either end-points on the curve.
58  *
59  * Likely we'll need a function especially to get the first/last orientations.
60  */
61 
62 #define BBONE_SCALE_Y 3.0f
63 
67  /* We could remove, keep since at the moment for checking the conversion. */
68  float co[3];
69  int index;
70 };
71 
73  struct BoneSplineHandle handles[2];
74 };
75 
76 static void gizmo_bbone_offset_get(const wmGizmo *UNUSED(gz),
77  wmGizmoProperty *gz_prop,
78  void *value_p)
79 {
80  struct BoneSplineHandle *bh = gz_prop->custom_func.user_data;
81  bPoseChannel *pchan = bh->pchan;
82 
83  float *value = value_p;
84  BLI_assert(gz_prop->type->array_length == 3);
85 
86  if (bh->index == 0) {
87  bh->co[1] = pchan->bone->ease1 / BBONE_SCALE_Y;
88  bh->co[0] = pchan->curve_in_x;
89  bh->co[2] = pchan->curve_in_y;
90  }
91  else {
92  bh->co[1] = -pchan->bone->ease2 / BBONE_SCALE_Y;
93  bh->co[0] = pchan->curve_out_x;
94  bh->co[2] = pchan->curve_out_y;
95  }
96  copy_v3_v3(value, bh->co);
97 }
98 
99 static void gizmo_bbone_offset_set(const wmGizmo *UNUSED(gz),
100  wmGizmoProperty *gz_prop,
101  const void *value_p)
102 {
103  struct BoneSplineHandle *bh = gz_prop->custom_func.user_data;
104  bPoseChannel *pchan = bh->pchan;
105 
106  const float *value = value_p;
107 
108  BLI_assert(gz_prop->type->array_length == 3);
109  copy_v3_v3(bh->co, value);
110 
111  if (bh->index == 0) {
112  pchan->bone->ease1 = max_ff(0.0f, bh->co[1] * BBONE_SCALE_Y);
113  pchan->curve_in_x = bh->co[0];
114  pchan->curve_in_y = bh->co[2];
115  }
116  else {
117  pchan->bone->ease2 = max_ff(0.0f, -bh->co[1] * BBONE_SCALE_Y);
118  pchan->curve_out_x = bh->co[0];
119  pchan->curve_out_y = bh->co[2];
120  }
121 }
122 
124 {
125  View3D *v3d = CTX_wm_view3d(C);
127  return false;
128  }
129 
130  ViewLayer *view_layer = CTX_data_view_layer(C);
131  Base *base = BASACT(view_layer);
132  if (base && BASE_SELECTABLE(v3d, base)) {
134  if (ob) {
135  const bArmature *arm = ob->data;
136  if (arm->drawtype == ARM_B_BONE) {
138  if (pchan && pchan->bone->segments > 1) {
139  return true;
140  }
141  }
142  }
143  }
144  return false;
145 }
146 
148 {
149  ViewLayer *view_layer = CTX_data_view_layer(C);
150  Object *ob = BKE_object_pose_armature_get(OBACT(view_layer));
152 
153  const wmGizmoType *gzt_move = WM_gizmotype_find("GIZMO_GT_move_3d", true);
154 
155  struct BoneSplineWidgetGroup *bspline_group = MEM_callocN(sizeof(struct BoneSplineWidgetGroup),
156  __func__);
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, NULL);
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  ViewLayer *view_layer = CTX_data_view_layer(C);
183  Object *ob = BKE_object_pose_armature_get(OBACT(view_layer));
184 
185  if (!gzgroup->customdata) {
186  return;
187  }
188 
189  struct BoneSplineWidgetGroup *bspline_group = gzgroup->customdata;
191 
192  /* Handles */
193  for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
194  wmGizmo *gz = bspline_group->handles[i].gizmo;
195  bspline_group->handles[i].pchan = pchan;
196  bspline_group->handles[i].index = i;
197 
198  float mat[4][4];
199  mul_m4_m4m4(mat, ob->obmat, (i == 0) ? pchan->disp_mat : pchan->disp_tail_mat);
200  copy_m4_m4(gz->matrix_space, mat);
201 
202  /* need to set property here for undo. TODO would prefer to do this in _init */
204  "offset",
205  &(const struct wmGizmoPropertyFnParams){
206  .value_get_fn = gizmo_bbone_offset_get,
207  .value_set_fn = gizmo_bbone_offset_set,
208  .range_get_fn = NULL,
209  .user_data = &bspline_group->handles[i],
210  });
211  }
212 }
213 
215 {
216  gzgt->name = "Armature Spline Widgets";
217  gzgt->idname = "VIEW3D_GGT_armature_spline";
218 
220 
225 }
226 
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_active(struct Object *ob)
Definition: action.c:708
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_pose_armature_get(struct Object *ob)
Definition: object.c:2487
#define BLI_assert(a)
Definition: BLI_assert.h:58
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:262
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
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:39
void UI_GetThemeColor3fv(int colorid, float col[3])
Definition: resources.c:1191
@ TH_GIZMO_HI
Definition: UI_resources.h:320
@ TH_GIZMO_PRIMARY
Definition: UI_resources.h:321
@ WM_GIZMO_DRAW_VALUE
@ WM_GIZMOGROUPTYPE_3D
@ WM_GIZMOGROUPTYPE_PERSISTENT
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:6413
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
struct wmGizmoProperty::@1149 custom_func
const struct wmGizmoPropertyType * type
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:96
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:339
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:58