Blender  V2.93
rna_pose_api.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  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 
29 #include "BLI_utildefines.h"
30 
31 #include "RNA_define.h"
32 
33 #include "DNA_object_types.h"
34 
35 /* #include "BLI_sys_types.h" */
36 
37 #include "rna_internal.h" /* own include */
38 
39 #ifdef RNA_RUNTIME
40 
41 # include "BKE_animsys.h"
42 # include "BKE_armature.h"
43 # include "BKE_context.h"
44 
45 # include "DNA_action_types.h"
46 # include "DNA_anim_types.h"
47 
48 # include "BLI_ghash.h"
49 
50 static float rna_PoseBone_do_envelope(bPoseChannel *chan, float *vec)
51 {
52  Bone *bone = chan->bone;
53 
54  float scale = (bone->flag & BONE_MULT_VG_ENV) == BONE_MULT_VG_ENV ? bone->weight : 1.0f;
55 
56  return distfactor_to_bone(vec,
57  chan->pose_head,
58  chan->pose_tail,
59  bone->rad_head * scale,
60  bone->rad_tail * scale,
61  bone->dist * scale);
62 }
63 
64 static void rna_PoseBone_bbone_segment_matrix(
65  bPoseChannel *pchan, ReportList *reports, float mat_ret[16], int index, bool rest)
66 {
67  if (!pchan->bone || pchan->bone->segments <= 1) {
68  BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
69  return;
70  }
71  if (pchan->runtime.bbone_segments != pchan->bone->segments) {
72  BKE_reportf(reports, RPT_ERROR, "Bone '%s' has out of date B-Bone segment data!", pchan->name);
73  return;
74  }
75  if (index < 0 || index > pchan->runtime.bbone_segments) {
77  reports, RPT_ERROR, "Invalid index %d for B-Bone segments of '%s'!", index, pchan->name);
78  return;
79  }
80 
81  if (rest) {
82  copy_m4_m4((float(*)[4])mat_ret, pchan->runtime.bbone_rest_mats[index].mat);
83  }
84  else {
85  copy_m4_m4((float(*)[4])mat_ret, pchan->runtime.bbone_pose_mats[index].mat);
86  }
87 }
88 
89 static void rna_PoseBone_compute_bbone_handles(bPoseChannel *pchan,
90  ReportList *reports,
91  float ret_h1[3],
92  float *ret_roll1,
93  float ret_h2[3],
94  float *ret_roll2,
95  bool rest,
96  bool ease,
97  bool offsets)
98 {
99  if (!pchan->bone || pchan->bone->segments <= 1) {
100  BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
101  return;
102  }
103 
105 
108  &params, ret_h1, ret_roll1, ret_h2, ret_roll2, ease || offsets, offsets);
109 }
110 
111 static void rna_Pose_apply_pose_from_action(ID *pose_owner,
112  bContext *C,
113  bAction *action,
114  const float evaluation_time)
115 {
116  BLI_assert(GS(pose_owner->name) == ID_OB);
117  Object *pose_owner_ob = (Object *)pose_owner;
118 
119  AnimationEvalContext anim_eval_context = {CTX_data_depsgraph_pointer(C), evaluation_time};
120  BKE_pose_apply_action(pose_owner_ob, action, &anim_eval_context);
121 
122  /* Do NOT tag with ID_RECALC_ANIMATION, as that would overwrite the just-applied pose. */
124  WM_event_add_notifier(C, NC_OBJECT | ND_POSE, pose_owner);
125 }
126 
127 #else
128 
130 {
131  FunctionRNA *func;
132  PropertyRNA *parm;
133 
134  func = RNA_def_function(srna, "apply_pose_from_action", "rna_Pose_apply_pose_from_action");
137  func,
138  "Apply the given action to this pose by evaluating it at a specific time. Only updates the "
139  "pose of selected bones, or all bones if none are selected.");
140 
141  parm = RNA_def_pointer(func, "action", "Action", "Action", "The Action containing the pose");
143 
144  parm = RNA_def_float(func,
145  "evaluation_time",
146  0.0f,
147  -FLT_MAX,
148  FLT_MAX,
149  "Evaluation Time",
150  "Time at which the given action is evaluated to obtain the pose",
151  -FLT_MAX,
152  FLT_MAX);
153 }
154 
156 {
157  PropertyRNA *parm;
158  FunctionRNA *func;
159 
160  func = RNA_def_function(srna, "evaluate_envelope", "rna_PoseBone_do_envelope");
161  RNA_def_function_ui_description(func, "Calculate bone envelope at given point");
162  parm = RNA_def_float_vector_xyz(func,
163  "point",
164  3,
165  NULL,
166  -FLT_MAX,
167  FLT_MAX,
168  "Point",
169  "Position in 3d space to evaluate",
170  -FLT_MAX,
171  FLT_MAX);
173  /* return value */
174  parm = RNA_def_float(
175  func, "factor", 0, -FLT_MAX, FLT_MAX, "Factor", "Envelope factor", -FLT_MAX, FLT_MAX);
176  RNA_def_function_return(func, parm);
177 
178  /* B-Bone segment matrices */
179  func = RNA_def_function(srna, "bbone_segment_matrix", "rna_PoseBone_bbone_segment_matrix");
181  func, "Retrieve the matrix of the joint between B-Bone segments if available");
183  parm = RNA_def_property(func, "matrix_return", PROP_FLOAT, PROP_MATRIX);
185  RNA_def_property_ui_text(parm, "", "The resulting matrix in bone local space");
186  RNA_def_function_output(func, parm);
187  parm = RNA_def_int(func, "index", 0, 0, INT_MAX, "", "Index of the segment endpoint", 0, 10000);
189  parm = RNA_def_boolean(func, "rest", false, "", "Return the rest pose matrix");
190 
191  /* B-Bone custom handle positions */
192  func = RNA_def_function(srna, "compute_bbone_handles", "rna_PoseBone_compute_bbone_handles");
194  func, "Retrieve the vectors and rolls coming from B-Bone custom handles");
196  parm = RNA_def_property(func, "handle1", PROP_FLOAT, PROP_XYZ);
197  RNA_def_property_array(parm, 3);
199  parm, "", "The direction vector of the start handle in bone local space");
200  RNA_def_function_output(func, parm);
201  parm = RNA_def_float(
202  func, "roll1", 0, -FLT_MAX, FLT_MAX, "", "Roll of the start handle", -FLT_MAX, FLT_MAX);
203  RNA_def_function_output(func, parm);
204  parm = RNA_def_property(func, "handle2", PROP_FLOAT, PROP_XYZ);
205  RNA_def_property_array(parm, 3);
206  RNA_def_property_ui_text(parm, "", "The direction vector of the end handle in bone local space");
207  RNA_def_function_output(func, parm);
208  parm = RNA_def_float(
209  func, "roll2", 0, -FLT_MAX, FLT_MAX, "", "Roll of the end handle", -FLT_MAX, FLT_MAX);
210  RNA_def_function_output(func, parm);
211  parm = RNA_def_boolean(func, "rest", false, "", "Return the rest pose state");
212  parm = RNA_def_boolean(func, "ease", false, "", "Apply scale from ease values");
213  parm = RNA_def_boolean(
214  func, "offsets", false, "", "Apply roll and curve offsets from bone properties");
215 }
216 
217 #endif
void BKE_pchan_bbone_spline_params_get(struct bPoseChannel *pchan, const bool rest, struct BBoneSplineParameters *r_param)
Definition: armature.c:948
float distfactor_to_bone(const float vec[3], const float b1[3], const float b2[3], float rad1, float rad2, float rdist)
void BKE_pose_apply_action(struct Object *ob, struct bAction *action, struct AnimationEvalContext *anim_eval_context)
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:1147
struct Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Definition: context.c:1401
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition: BLI_assert.h:58
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
@ ID_OB
Definition: DNA_ID_enums.h:59
@ BONE_MULT_VG_ENV
Object is a sort of wrapper for general info.
@ PARM_REQUIRED
Definition: RNA_types.h:337
@ FUNC_USE_REPORTS
Definition: RNA_types.h:578
@ FUNC_NO_SELF
Definition: RNA_types.h:571
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:577
@ FUNC_USE_SELF_ID
Definition: RNA_types.h:565
@ PROP_FLOAT
Definition: RNA_types.h:75
@ PROP_MATRIX
Definition: RNA_types.h:144
@ PROP_XYZ
Definition: RNA_types.h:148
#define C
Definition: RandGen.cpp:39
#define ND_POSE
Definition: WM_types.h:359
#define NC_OBJECT
Definition: WM_types.h:280
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define GS(x)
Definition: iris.c:241
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:3825
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4159
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4302
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1676
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4262
void RNA_def_function_output(FunctionRNA *UNUSED(func), PropertyRNA *ret)
Definition: rna_define.c:4327
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
Definition: rna_define.c:1629
void RNA_def_property_array(PropertyRNA *prop, int length)
Definition: rna_define.c:1568
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4337
const int rna_matrix_dimsize_4x4[]
Definition: rna_define.c:1626
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1279
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4332
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:3883
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:3585
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1547
void RNA_api_pose(StructRNA *srna)
Definition: rna_pose_api.c:129
void RNA_api_pose_channel(StructRNA *srna)
Definition: rna_pose_api.c:155
short segments
float rad_head
float rad_tail
float dist
float weight
Definition: DNA_ID.h:273
char name[66]
Definition: DNA_ID.h:283
float mat[4][4]
Definition: BKE_armature.h:280
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)