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 
20 /** \file
21  * \ingroup RNA
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 
29 #include "RNA_define.h"
30 
31 #include "rna_internal.h" /* own include */
32 
33 #ifdef RNA_RUNTIME
34 
35 #  include <stddef.h>
36 
37 #  include "DNA_armature_types.h"
38 
39 #  include "BKE_armature.h"
40 #  include "BLI_math_vector.h"
41 
rna_EditBone_align_roll(EditBone * ebo,float no[3])42 static void rna_EditBone_align_roll(EditBone *ebo, float no[3])
43 {
44   ebo->roll = ED_armature_ebone_roll_to_vector(ebo, no, false);
45 }
46 
rna_Bone_do_envelope(Bone * bone,float * vec)47 static float rna_Bone_do_envelope(Bone *bone, float *vec)
48 {
49   float scale = (bone->flag & BONE_MULT_VG_ENV) == BONE_MULT_VG_ENV ? bone->weight : 1.0f;
50   return distfactor_to_bone(vec,
51                             bone->arm_head,
52                             bone->arm_tail,
53                             bone->rad_head * scale,
54                             bone->rad_tail * scale,
55                             bone->dist * scale);
56 }
57 
rna_Bone_convert_local_to_pose(Bone * bone,float * r_matrix,float * matrix,float * matrix_local,float * parent_matrix,float * parent_matrix_local,bool invert)58 static void rna_Bone_convert_local_to_pose(Bone *bone,
59                                            float *r_matrix,
60                                            float *matrix,
61                                            float *matrix_local,
62                                            float *parent_matrix,
63                                            float *parent_matrix_local,
64                                            bool invert)
65 {
66   BoneParentTransform bpt;
67   float offs_bone[4][4];
68   float(*bone_arm_mat)[4] = (float(*)[4])matrix_local;
69   float(*parent_pose_mat)[4] = (float(*)[4])parent_matrix;
70   float(*parent_arm_mat)[4] = (float(*)[4])parent_matrix_local;
71 
72   if (is_zero_m4(parent_pose_mat) || is_zero_m4(parent_arm_mat)) {
73     /* No parent case. */
74     BKE_bone_parent_transform_calc_from_matrices(
75         bone->flag, bone->inherit_scale_mode, bone_arm_mat, NULL, NULL, &bpt);
76   }
77   else {
78     invert_m4_m4(offs_bone, parent_arm_mat);
79     mul_m4_m4m4(offs_bone, offs_bone, bone_arm_mat);
80 
81     BKE_bone_parent_transform_calc_from_matrices(
82         bone->flag, bone->inherit_scale_mode, offs_bone, parent_arm_mat, parent_pose_mat, &bpt);
83   }
84 
85   if (invert) {
86     BKE_bone_parent_transform_invert(&bpt);
87   }
88 
89   BKE_bone_parent_transform_apply(&bpt, (float(*)[4])matrix, (float(*)[4])r_matrix);
90 }
91 
rna_Bone_MatrixFromAxisRoll(float * axis,float roll,float * r_matrix)92 static void rna_Bone_MatrixFromAxisRoll(float *axis, float roll, float *r_matrix)
93 {
94   vec_roll_to_mat3(axis, roll, (float(*)[3])r_matrix);
95 }
96 
rna_Bone_AxisRollFromMatrix(float * matrix,float * axis_override,float * r_axis,float * r_roll)97 static void rna_Bone_AxisRollFromMatrix(float *matrix,
98                                         float *axis_override,
99                                         float *r_axis,
100                                         float *r_roll)
101 {
102   float mat[3][3];
103 
104   normalize_m3_m3(mat, (float(*)[3])matrix);
105 
106   if (normalize_v3_v3(r_axis, axis_override) != 0.0f) {
107     mat3_vec_to_roll(mat, r_axis, r_roll);
108   }
109   else {
110     mat3_to_vec_roll(mat, r_axis, r_roll);
111   }
112 }
113 #else
114 
RNA_api_armature_edit_bone(StructRNA * srna)115 void RNA_api_armature_edit_bone(StructRNA *srna)
116 {
117   FunctionRNA *func;
118   PropertyRNA *parm;
119 
120   func = RNA_def_function(srna, "align_roll", "rna_EditBone_align_roll");
121   RNA_def_function_ui_description(func,
122                                   "Align the bone to a localspace roll so the Z axis "
123                                   "points in the direction of the vector given");
124   parm = RNA_def_float_vector(
125       func, "vector", 3, NULL, -FLT_MAX, FLT_MAX, "Vector", "", -FLT_MAX, FLT_MAX);
126   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
127 }
128 
RNA_api_bone(StructRNA * srna)129 void RNA_api_bone(StructRNA *srna)
130 {
131   PropertyRNA *parm;
132   FunctionRNA *func;
133 
134   func = RNA_def_function(srna, "evaluate_envelope", "rna_Bone_do_envelope");
135   RNA_def_function_ui_description(func, "Calculate bone envelope at given point");
136   parm = RNA_def_float_vector_xyz(func,
137                                   "point",
138                                   3,
139                                   NULL,
140                                   -FLT_MAX,
141                                   FLT_MAX,
142                                   "Point",
143                                   "Position in 3d space to evaluate",
144                                   -FLT_MAX,
145                                   FLT_MAX);
146   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
147   /* return value */
148   parm = RNA_def_float(
149       func, "factor", 0, -FLT_MAX, FLT_MAX, "Factor", "Envelope factor", -FLT_MAX, FLT_MAX);
150   RNA_def_function_return(func, parm);
151 
152   func = RNA_def_function(srna, "convert_local_to_pose", "rna_Bone_convert_local_to_pose");
153   RNA_def_function_ui_description(func,
154                                   "Transform a matrix from Local to Pose space (or back), taking "
155                                   "into account options like Inherit Scale and Local Location. "
156                                   "Unlike Object.convert_space, this uses custom rest and pose "
157                                   "matrices provided by the caller. If the parent matrices are "
158                                   "omitted, the bone is assumed to have no parent.");
159   parm = RNA_def_property(func, "matrix_return", PROP_FLOAT, PROP_MATRIX);
160   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_4x4);
161   RNA_def_property_ui_text(parm, "", "The transformed matrix");
162   RNA_def_function_output(func, parm);
163   parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
164   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_4x4);
165   RNA_def_property_ui_text(parm, "", "The matrix to transform");
166   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
167   parm = RNA_def_property(func, "matrix_local", PROP_FLOAT, PROP_MATRIX);
168   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_4x4);
169   RNA_def_property_ui_text(parm, "", "The custom rest matrix of this bone (Bone.matrix_local)");
170   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
171   parm = RNA_def_property(func, "parent_matrix", PROP_FLOAT, PROP_MATRIX);
172   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_4x4);
173   RNA_def_property_ui_text(
174       parm, "", "The custom pose matrix of the parent bone (PoseBone.matrix)");
175   parm = RNA_def_property(func, "parent_matrix_local", PROP_FLOAT, PROP_MATRIX);
176   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_4x4);
177   RNA_def_property_ui_text(
178       parm, "", "The custom rest matrix of the parent bone (Bone.matrix_local)");
179   parm = RNA_def_boolean(func, "invert", false, "", "Convert from Pose to Local space");
180 
181   /* Conversions between Matrix and Axis + Roll representations. */
182   func = RNA_def_function(srna, "MatrixFromAxisRoll", "rna_Bone_MatrixFromAxisRoll");
183   RNA_def_function_ui_description(func, "Convert the axis + roll representation to a matrix");
184   RNA_def_function_flag(func, FUNC_NO_SELF);
185   parm = RNA_def_property(func, "axis", PROP_FLOAT, PROP_XYZ);
186   RNA_def_property_array(parm, 3);
187   RNA_def_property_ui_text(parm, "", "The main axis of the bone (tail - head)");
188   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
189   parm = RNA_def_property(func, "roll", PROP_FLOAT, PROP_NONE);
190   RNA_def_property_ui_text(parm, "", "The roll of the bone");
191   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
192   parm = RNA_def_property(func, "result_matrix", PROP_FLOAT, PROP_MATRIX);
193   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_3x3);
194   RNA_def_property_ui_text(parm, "", "The resulting orientation matrix");
195   RNA_def_function_output(func, parm);
196 
197   func = RNA_def_function(srna, "AxisRollFromMatrix", "rna_Bone_AxisRollFromMatrix");
198   RNA_def_function_ui_description(func,
199                                   "Convert a rotational matrix to the axis + roll representation");
200   RNA_def_function_flag(func, FUNC_NO_SELF);
201   parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
202   RNA_def_property_multi_array(parm, 2, rna_matrix_dimsize_3x3);
203   RNA_def_property_ui_text(parm, "", "The orientation matrix of the bone");
204   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
205   parm = RNA_def_property(func, "axis", PROP_FLOAT, PROP_MATRIX);
206   RNA_def_property_array(parm, 3);
207   RNA_def_property_ui_text(
208       parm, "", "The optional override for the axis (finds closest approximation for the matrix)");
209   parm = RNA_def_property(func, "result_axis", PROP_FLOAT, PROP_XYZ);
210   RNA_def_property_array(parm, 3);
211   RNA_def_property_ui_text(parm, "", "The main axis of the bone");
212   RNA_def_function_output(func, parm);
213   parm = RNA_def_property(func, "result_roll", PROP_FLOAT, PROP_NONE);
214   RNA_def_property_ui_text(parm, "", "The roll of the bone");
215   RNA_def_function_output(func, parm);
216 }
217 
218 #endif
219