Blender V4.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
9#include "BLI_math_matrix.h"
10#include "BLI_math_vector.h"
11#include "BLI_utildefines.h"
12
13#include "BKE_action.hh"
14#include "BKE_context.hh"
15#include "BKE_layer.hh"
16#include "BKE_object.hh"
17
18#include "DNA_armature_types.h"
19#include "DNA_object_types.h"
20
21#include "ED_gizmo_library.hh"
22
23#include "UI_resources.hh"
24
25#include "MEM_guardedalloc.h"
26
27#include "RNA_access.hh"
28
29#include "WM_types.hh"
30
31#include "view3d_intern.hh" /* own include */
32
33/* -------------------------------------------------------------------- */
37/*
38 * TODO(@ideasman42): Current conversion is a approximation (usable not correct),
39 * we'll need to take the next/previous bones into account to get the tangent directions.
40 * First last matrices from 'BKE_pchan_bbone_spline_setup' are close but also not quite accurate
41 * since they're not at either end-points on the curve.
42 *
43 * Likely we'll need a function especially to get the first/last orientations.
44 */
45
46#define BBONE_SCALE_Y 3.0f
47
51 /* We could remove, keep since at the moment for checking the conversion. */
52 float co[3];
53 int index;
54};
55
59
60static void gizmo_bbone_offset_get(const wmGizmo * /*gz*/, wmGizmoProperty *gz_prop, void *value_p)
61{
62 BoneSplineHandle *bh = static_cast<BoneSplineHandle *>(gz_prop->custom_func.user_data);
63 bPoseChannel *pchan = bh->pchan;
64
65 float *value = static_cast<float *>(value_p);
66 BLI_assert(gz_prop->type->array_length == 3);
67
68 if (bh->index == 0) {
69 bh->co[1] = pchan->bone->ease1 / BBONE_SCALE_Y;
70 bh->co[0] = pchan->curve_in_x;
71 bh->co[2] = pchan->curve_in_z;
72 }
73 else {
74 bh->co[1] = -pchan->bone->ease2 / BBONE_SCALE_Y;
75 bh->co[0] = pchan->curve_out_x;
76 bh->co[2] = pchan->curve_out_z;
77 }
78 copy_v3_v3(value, bh->co);
79}
80
81static void gizmo_bbone_offset_set(const wmGizmo * /*gz*/,
82 wmGizmoProperty *gz_prop,
83 const void *value_p)
84{
85 BoneSplineHandle *bh = static_cast<BoneSplineHandle *>(gz_prop->custom_func.user_data);
86 bPoseChannel *pchan = bh->pchan;
87
88 const float *value = static_cast<const float *>(value_p);
89
90 BLI_assert(gz_prop->type->array_length == 3);
91 copy_v3_v3(bh->co, value);
92
93 if (bh->index == 0) {
94 pchan->bone->ease1 = max_ff(0.0f, bh->co[1] * BBONE_SCALE_Y);
95 pchan->curve_in_x = bh->co[0];
96 pchan->curve_in_z = bh->co[2];
97 }
98 else {
99 pchan->bone->ease2 = max_ff(0.0f, -bh->co[1] * BBONE_SCALE_Y);
100 pchan->curve_out_x = bh->co[0];
101 pchan->curve_out_z = bh->co[2];
102 }
103}
104
106{
107 View3D *v3d = CTX_wm_view3d(C);
109 return false;
110 }
111
112 const Scene *scene = CTX_data_scene(C);
113 ViewLayer *view_layer = CTX_data_view_layer(C);
114 BKE_view_layer_synced_ensure(scene, view_layer);
115 Base *base = BKE_view_layer_active_base_get(view_layer);
116 if (base && BASE_SELECTABLE(v3d, base)) {
118 if (ob) {
119 const bArmature *arm = static_cast<const bArmature *>(ob->data);
120 if (arm->drawtype == ARM_B_BONE) {
122 if (pchan && pchan->bone->segments > 1) {
123 return true;
124 }
125 }
126 }
127 }
128 return false;
129}
130
132{
133 const Scene *scene = CTX_data_scene(C);
134 ViewLayer *view_layer = CTX_data_view_layer(C);
135 BKE_view_layer_synced_ensure(scene, view_layer);
138
139 const wmGizmoType *gzt_move = WM_gizmotype_find("GIZMO_GT_move_3d", true);
140
141 BoneSplineWidgetGroup *bspline_group = static_cast<BoneSplineWidgetGroup *>(
142 MEM_callocN(sizeof(BoneSplineWidgetGroup), __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, nullptr);
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 const Scene *scene = CTX_data_scene(C);
169 ViewLayer *view_layer = CTX_data_view_layer(C);
170 BKE_view_layer_synced_ensure(scene, view_layer);
172
173 if (!gzgroup->customdata) {
174 return;
175 }
176
177 BoneSplineWidgetGroup *bspline_group = static_cast<BoneSplineWidgetGroup *>(gzgroup->customdata);
179
180 /* Handles */
181 for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
182 wmGizmo *gz = bspline_group->handles[i].gizmo;
183 bspline_group->handles[i].pchan = pchan;
184 bspline_group->handles[i].index = i;
185
186 float mat[4][4];
188 mat, ob->object_to_world().ptr(), (i == 0) ? pchan->disp_mat : pchan->disp_tail_mat);
189 copy_m4_m4(gz->matrix_space, mat);
190
191 /* need to set property here for undo. TODO: would prefer to do this in _init. */
193 params.value_get_fn = gizmo_bbone_offset_get;
194 params.value_set_fn = gizmo_bbone_offset_set;
195 params.range_get_fn = nullptr;
196 params.user_data = &bspline_group->handles[i];
198 }
199}
200
202{
203 gzgt->name = "Armature Spline Widgets";
204 gzgt->idname = "VIEW3D_GGT_armature_spline";
205
207
212}
213
Blender kernel action and pose functionality.
bPoseChannel * BKE_pose_channel_active_if_bonecoll_visible(Object *ob) ATTR_WARN_UNUSED_RESULT
Scene * CTX_data_scene(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
General operations, lookup, etc. for blender objects.
Object * BKE_object_pose_armature_get(Object *ob)
#define BLI_assert(a)
Definition BLI_assert.h:50
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])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define ARRAY_SIZE(arr)
@ ARM_B_BONE
Object is a sort of wrapper for general info.
#define BASE_SELECTABLE(v3d, base)
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_CONTEXT
@ ED_GIZMO_MOVE_DRAW_FLAG_FILL
@ ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW
@ ED_GIZMO_MOVE_STYLE_RING_2D
Read Guarded memory(de)allocation.
void UI_GetThemeColor3fv(int colorid, float col[3])
@ TH_GIZMO_HI
@ TH_GIZMO_PRIMARY
@ WM_GIZMO_DRAW_VALUE
@ WM_GIZMOGROUPTYPE_3D
@ WM_GIZMOGROUPTYPE_PERSISTENT
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
struct Object * object
BoneSplineHandle handles[2]
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 wmGizmoPropertyType * type
struct wmGizmoProperty::@1373 custom_func
float matrix_basis[4][4]
float color_hi[4]
float color[4]
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 *, wmGizmoProperty *gz_prop, void *value_p)
static void WIDGETGROUP_armature_spline_setup(const bContext *C, wmGizmoGroup *gzgroup)
static bool WIDGETGROUP_armature_spline_poll(const bContext *C, wmGizmoGroupType *)
#define BBONE_SCALE_Y
static void gizmo_bbone_offset_set(const wmGizmo *, wmGizmoProperty *gz_prop, const void *value_p)
void VIEW3D_GGT_armature_spline(wmGizmoGroupType *gzgt)
wmGizmo * WM_gizmo_new_ptr(const wmGizmoType *gzt, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:81
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
wmKeyMap * WM_gizmogroup_setup_keymap_generic_maybe_drag(const wmGizmoGroupType *, 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)