Blender  V3.3
rna_pose_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Blender Foundation. All rights reserved. */
3 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 
13 #include "BLI_utildefines.h"
14 
15 #include "RNA_define.h"
16 
17 #include "DNA_object_types.h"
18 
19 /* #include "BLI_sys_types.h" */
20 
21 #include "rna_internal.h" /* own include */
22 
23 #ifdef RNA_RUNTIME
24 
25 # include "BKE_animsys.h"
26 # include "BKE_armature.h"
27 # include "BKE_context.h"
28 
29 # include "DNA_action_types.h"
30 # include "DNA_anim_types.h"
31 
32 # include "BLI_ghash.h"
33 
34 static float rna_PoseBone_do_envelope(bPoseChannel *chan, float vec[3])
35 {
36  Bone *bone = chan->bone;
37 
38  float scale = (bone->flag & BONE_MULT_VG_ENV) == BONE_MULT_VG_ENV ? bone->weight : 1.0f;
39 
40  return distfactor_to_bone(vec,
41  chan->pose_head,
42  chan->pose_tail,
43  bone->rad_head * scale,
44  bone->rad_tail * scale,
45  bone->dist * scale);
46 }
47 
48 static void rna_PoseBone_bbone_segment_matrix(
49  bPoseChannel *pchan, ReportList *reports, float mat_ret[16], int index, bool rest)
50 {
51  if (!pchan->bone || pchan->bone->segments <= 1) {
52  BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
53  return;
54  }
55  if (pchan->runtime.bbone_segments != pchan->bone->segments) {
56  BKE_reportf(reports, RPT_ERROR, "Bone '%s' has out of date B-Bone segment data!", pchan->name);
57  return;
58  }
59  if (index < 0 || index > pchan->runtime.bbone_segments) {
61  reports, RPT_ERROR, "Invalid index %d for B-Bone segments of '%s'!", index, pchan->name);
62  return;
63  }
64 
65  if (rest) {
66  copy_m4_m4((float(*)[4])mat_ret, pchan->runtime.bbone_rest_mats[index].mat);
67  }
68  else {
69  copy_m4_m4((float(*)[4])mat_ret, pchan->runtime.bbone_pose_mats[index].mat);
70  }
71 }
72 
73 static void rna_PoseBone_compute_bbone_handles(bPoseChannel *pchan,
74  ReportList *reports,
75  float ret_h1[3],
76  float *ret_roll1,
77  float ret_h2[3],
78  float *ret_roll2,
79  bool rest,
80  bool ease,
81  bool offsets)
82 {
83  if (!pchan->bone || pchan->bone->segments <= 1) {
84  BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
85  return;
86  }
87 
89 
92  &params, ret_h1, ret_roll1, ret_h2, ret_roll2, ease || offsets, offsets);
93 }
94 
95 static void rna_Pose_apply_pose_from_action(ID *pose_owner,
96  bContext *C,
97  bAction *action,
98  const float evaluation_time)
99 {
100  BLI_assert(GS(pose_owner->name) == ID_OB);
101  Object *pose_owner_ob = (Object *)pose_owner;
102 
103  AnimationEvalContext anim_eval_context = {CTX_data_depsgraph_pointer(C), evaluation_time};
104  BKE_pose_apply_action_selected_bones(pose_owner_ob, action, &anim_eval_context);
105 
106  /* Do NOT tag with ID_RECALC_ANIMATION, as that would overwrite the just-applied pose. */
108  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, pose_owner);
109 }
110 
111 #else
112 
114 {
115  FunctionRNA *func;
116  PropertyRNA *parm;
117 
118  func = RNA_def_function(srna, "apply_pose_from_action", "rna_Pose_apply_pose_from_action");
121  func,
122  "Apply the given action to this pose by evaluating it at a specific time. Only updates the "
123  "pose of selected bones, or all bones if none are selected.");
124 
125  parm = RNA_def_pointer(func, "action", "Action", "Action", "The Action containing the pose");
127 
128  parm = RNA_def_float(func,
129  "evaluation_time",
130  0.0f,
131  -FLT_MAX,
132  FLT_MAX,
133  "Evaluation Time",
134  "Time at which the given action is evaluated to obtain the pose",
135  -FLT_MAX,
136  FLT_MAX);
137 }
138 
140 {
141  PropertyRNA *parm;
142  FunctionRNA *func;
143 
144  func = RNA_def_function(srna, "evaluate_envelope", "rna_PoseBone_do_envelope");
145  RNA_def_function_ui_description(func, "Calculate bone envelope at given point");
146  parm = RNA_def_float_vector_xyz(func,
147  "point",
148  3,
149  NULL,
150  -FLT_MAX,
151  FLT_MAX,
152  "Point",
153  "Position in 3d space to evaluate",
154  -FLT_MAX,
155  FLT_MAX);
157  /* return value */
158  parm = RNA_def_float(
159  func, "factor", 0, -FLT_MAX, FLT_MAX, "Factor", "Envelope factor", -FLT_MAX, FLT_MAX);
160  RNA_def_function_return(func, parm);
161 
162  /* B-Bone segment matrices */
163  func = RNA_def_function(srna, "bbone_segment_matrix", "rna_PoseBone_bbone_segment_matrix");
165  func, "Retrieve the matrix of the joint between B-Bone segments if available");
167  parm = RNA_def_property(func, "matrix_return", PROP_FLOAT, PROP_MATRIX);
169  RNA_def_property_ui_text(parm, "", "The resulting matrix in bone local space");
170  RNA_def_function_output(func, parm);
171  parm = RNA_def_int(func, "index", 0, 0, INT_MAX, "", "Index of the segment endpoint", 0, 10000);
173  parm = RNA_def_boolean(func, "rest", false, "", "Return the rest pose matrix");
174 
175  /* B-Bone custom handle positions */
176  func = RNA_def_function(srna, "compute_bbone_handles", "rna_PoseBone_compute_bbone_handles");
178  func, "Retrieve the vectors and rolls coming from B-Bone custom handles");
180  parm = RNA_def_property(func, "handle1", PROP_FLOAT, PROP_XYZ);
181  RNA_def_property_array(parm, 3);
183  parm, "", "The direction vector of the start handle in bone local space");
184  RNA_def_function_output(func, parm);
185  parm = RNA_def_float(
186  func, "roll1", 0, -FLT_MAX, FLT_MAX, "", "Roll of the start handle", -FLT_MAX, FLT_MAX);
187  RNA_def_function_output(func, parm);
188  parm = RNA_def_property(func, "handle2", PROP_FLOAT, PROP_XYZ);
189  RNA_def_property_array(parm, 3);
190  RNA_def_property_ui_text(parm, "", "The direction vector of the end handle in bone local space");
191  RNA_def_function_output(func, parm);
192  parm = RNA_def_float(
193  func, "roll2", 0, -FLT_MAX, FLT_MAX, "", "Roll of the end handle", -FLT_MAX, FLT_MAX);
194  RNA_def_function_output(func, parm);
195  parm = RNA_def_boolean(func, "rest", false, "", "Return the rest pose state");
196  parm = RNA_def_boolean(func, "ease", false, "", "Apply scale from ease values");
197  parm = RNA_def_boolean(
198  func, "offsets", false, "", "Apply roll and curve offsets from bone properties");
199 }
200 
201 #endif
void BKE_pose_apply_action_selected_bones(struct Object *ob, struct bAction *action, struct AnimationEvalContext *anim_eval_context)
float distfactor_to_bone(const float vec[3], const float b1[3], const float b2[3], float rad1, float rad2, float rdist)
void BKE_pchan_bbone_spline_params_get(struct bPoseChannel *pchan, bool rest, struct BBoneSplineParameters *r_param)
Definition: armature.c:933
void BKE_pchan_bbone_handles_compute(const BBoneSplineParameters *param, float h1[3], float *r_roll1, float h2[3], float *r_roll2, bool ease, bool offsets)
Definition: armature.c:1190
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Definition: context.c:1505
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition: BLI_assert.h:46
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:791
@ ID_OB
Definition: DNA_ID_enums.h:47
@ BONE_MULT_VG_ENV
Object is a sort of wrapper for general info.
@ PARM_REQUIRED
Definition: RNA_types.h:352
@ FUNC_USE_REPORTS
Definition: RNA_types.h:663
@ FUNC_NO_SELF
Definition: RNA_types.h:656
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:662
@ FUNC_USE_SELF_ID
Definition: RNA_types.h:650
@ PROP_FLOAT
Definition: RNA_types.h:61
@ PROP_MATRIX
Definition: RNA_types.h:158
@ PROP_XYZ
Definition: RNA_types.h:162
#define C
Definition: RandGen.cpp:25
#define ND_POSE
Definition: WM_types.h:407
#define NC_OBJECT
Definition: WM_types.h:329
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define GS(x)
Definition: iris.c:225
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3836
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4170
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4312
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1645
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4273
void RNA_def_function_output(FunctionRNA *UNUSED(func), PropertyRNA *ret)
Definition: rna_define.c:4337
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
Definition: rna_define.c:1598
void RNA_def_property_array(PropertyRNA *prop, int length)
Definition: rna_define.c:1539
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4347
const int rna_matrix_dimsize_4x4[]
Definition: rna_define.c:1595
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1257
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4342
PropertyRNA * RNA_def_float_vector_xyz(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3894
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3597
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1518
void RNA_api_pose(StructRNA *srna)
Definition: rna_pose_api.c:113
void RNA_api_pose_channel(StructRNA *srna)
Definition: rna_pose_api.c:139
short segments
float rad_head
float rad_tail
float dist
float weight
Definition: DNA_ID.h:368
char name[66]
Definition: DNA_ID.h:378
float mat[4][4]
Definition: BKE_armature.h:464
struct Mat4 * bbone_pose_mats
struct Mat4 * bbone_rest_mats
struct Bone * bone
float pose_head[3]
float pose_tail[3]
struct bPoseChannel_Runtime runtime
void WM_event_add_notifier(const bContext *C, uint type, void *reference)