Blender V5.0
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
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_lib_id.hh"
17#include "BKE_object.hh"
18
19#include "DNA_armature_types.h"
20#include "DNA_object_types.h"
21
22#include "ED_gizmo_library.hh"
23
24#include "UI_resources.hh"
25
26#include "MEM_guardedalloc.h"
27
28#include "RNA_access.hh"
29
30#include "WM_types.hh"
31
32#include "view3d_intern.hh" /* own include */
33
34/* -------------------------------------------------------------------- */
37
38/*
39 * TODO(@ideasman42): Current conversion is a approximation (usable not correct),
40 * we'll need to take the next/previous bones into account to get the tangent directions.
41 * First last matrices from #BKE_pchan_bbone_spline_setup are close but also not quite accurate
42 * since they're not at either end-points on the curve.
43 *
44 * Likely we'll need a function especially to get the first/last orientations.
45 */
46
47#define BBONE_SCALE_Y 3.0f
48
52 /* We could remove, keep since at the moment for checking the conversion. */
53 float co[3];
54 int index;
55};
56
60
61static void gizmo_bbone_offset_get(const wmGizmo * /*gz*/, wmGizmoProperty *gz_prop, void *value_p)
62{
63 BoneSplineHandle *bh = static_cast<BoneSplineHandle *>(gz_prop->custom_func.user_data);
64 bPoseChannel *pchan = bh->pchan;
65
66 float *value = static_cast<float *>(value_p);
67 BLI_assert(gz_prop->type->array_length == 3);
68
69 if (bh->index == 0) {
70 bh->co[1] = pchan->bone->ease1 / BBONE_SCALE_Y;
71 bh->co[0] = pchan->curve_in_x;
72 bh->co[2] = pchan->curve_in_z;
73 }
74 else {
75 bh->co[1] = -pchan->bone->ease2 / BBONE_SCALE_Y;
76 bh->co[0] = pchan->curve_out_x;
77 bh->co[2] = pchan->curve_out_z;
78 }
79 copy_v3_v3(value, bh->co);
80}
81
82static void gizmo_bbone_offset_set(const wmGizmo * /*gz*/,
83 wmGizmoProperty *gz_prop,
84 const void *value_p)
85{
86 BoneSplineHandle *bh = static_cast<BoneSplineHandle *>(gz_prop->custom_func.user_data);
87 bPoseChannel *pchan = bh->pchan;
88
89 const float *value = static_cast<const float *>(value_p);
90
91 BLI_assert(gz_prop->type->array_length == 3);
92 copy_v3_v3(bh->co, value);
93
94 if (bh->index == 0) {
95 pchan->bone->ease1 = max_ff(0.0f, bh->co[1] * BBONE_SCALE_Y);
96 pchan->curve_in_x = bh->co[0];
97 pchan->curve_in_z = bh->co[2];
98 }
99 else {
100 pchan->bone->ease2 = max_ff(0.0f, -bh->co[1] * BBONE_SCALE_Y);
101 pchan->curve_out_x = bh->co[0];
102 pchan->curve_out_z = bh->co[2];
103 }
104}
105
107{
108 View3D *v3d = CTX_wm_view3d(C);
110 return false;
111 }
112
113 const Scene *scene = CTX_data_scene(C);
114 ViewLayer *view_layer = CTX_data_view_layer(C);
115 BKE_view_layer_synced_ensure(scene, view_layer);
116 Base *base = BKE_view_layer_active_base_get(view_layer);
117 if (base && BASE_SELECTABLE(v3d, base)) {
119 if (ob) {
120 const bArmature *arm = static_cast<const bArmature *>(ob->data);
121 if (arm->drawtype == ARM_DRAW_TYPE_B_BONE) {
123 if (pchan && pchan->bone->segments > 1) {
124 if (BKE_id_is_editable(CTX_data_main(C), &arm->id)) {
125 return true;
126 }
127 }
128 }
129 }
130 }
131 return false;
132}
133
135{
136 const Scene *scene = CTX_data_scene(C);
137 ViewLayer *view_layer = CTX_data_view_layer(C);
138 BKE_view_layer_synced_ensure(scene, view_layer);
141
142 const wmGizmoType *gzt_move = WM_gizmotype_find("GIZMO_GT_move_3d", true);
143
145 gzgroup->customdata = bspline_group;
146
147 /* Handles */
148 for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
149 wmGizmo *gz;
150 gz = bspline_group->handles[i].gizmo = WM_gizmo_new_ptr(gzt_move, gzgroup, nullptr);
151 RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_MOVE_STYLE_RING_2D);
152 RNA_enum_set(gz->ptr,
153 "draw_options",
156
159
160 gz->scale_basis = 0.06f;
161
162 if (i == 0) {
163 copy_v3_v3(gz->matrix_basis[3], pchan->loc);
164 }
165 }
166}
167
169{
170 const Scene *scene = CTX_data_scene(C);
171 ViewLayer *view_layer = CTX_data_view_layer(C);
172 BKE_view_layer_synced_ensure(scene, view_layer);
174
175 if (!gzgroup->customdata) {
176 return;
177 }
178
179 BoneSplineWidgetGroup *bspline_group = static_cast<BoneSplineWidgetGroup *>(gzgroup->customdata);
181
182 /* Handles */
183 for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
184 wmGizmo *gz = bspline_group->handles[i].gizmo;
185 bspline_group->handles[i].pchan = pchan;
186 bspline_group->handles[i].index = i;
187
188 float mat[4][4];
190 mat, ob->object_to_world().ptr(), (i == 0) ? pchan->disp_mat : pchan->disp_tail_mat);
191 copy_m4_m4(gz->matrix_space, mat);
192
193 /* need to set property here for undo. TODO: would prefer to do this in _init. */
195 params.value_get_fn = gizmo_bbone_offset_get;
196 params.value_set_fn = gizmo_bbone_offset_set;
197 params.range_get_fn = nullptr;
198 params.user_data = &bspline_group->handles[i];
200 }
201}
202
204{
205 gzgt->name = "Armature Spline Widgets";
206 gzgt->idname = "VIEW3D_GGT_armature_spline";
207
209
214}
215
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)
Main * CTX_data_main(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)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2523
General operations, lookup, etc. for blender objects.
Object * BKE_object_pose_armature_get(Object *ob)
#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])
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_DRAW_TYPE_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_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:29
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:118
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::@331027022007232055216276241130041346111314317052 custom_func
float matrix_basis[4][4]
float color_hi[4]
float color[4]
PointerRNA * ptr
float scale_basis
float matrix_space[4][4]
i
Definition text_draw.cc:230
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:85
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:307
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 StringRef idname, bool quiet)