Armature: convert the length Python property of bones to a RNA property.

This allows accessing it from drivers and using it in UI, as
demonstrated by adding it to the transform panel of 3D View.

As an aside, properly mark transform-related properties of Bone
read-only, as they can only be changed correctly in edit mode.
This commit is contained in:
Alexander Gavrilov 2019-09-11 12:48:52 +03:00
parent 69e0f485c9
commit d4f8bc80a4
5 changed files with 52 additions and 20 deletions

View File

@ -251,18 +251,6 @@ class _GenericBone:
"""The midpoint between the head and the tail."""
return (self.head + self.tail) * 0.5
@property
def length(self):
"""
The distance from head to tail,
when set the head is moved to fit the length.
"""
return self.vector.length
@length.setter
def length(self, value):
self.tail = self.head + ((self.tail - self.head).normalized() * value)
@property
def vector(self):
"""

View File

@ -211,7 +211,7 @@ RNANodeIdentifier RNANodeQuery::construct_node_identifier(const PointerRNA *ptr,
}
/* Final transform properties go to the Done node for the exit. */
else if (STREQ(prop_name, "head") || STREQ(prop_name, "tail") ||
STRPREFIX(prop_name, "matrix")) {
STREQ(prop_name, "length") || STRPREFIX(prop_name, "matrix")) {
if (source == RNAPointerSource::EXIT) {
node_identifier.operation_code = OperationCode::BONE_DONE;
}

View File

@ -1466,6 +1466,7 @@ static void v3d_editarmature_buts(uiLayout *layout, Object *ob)
uiItemR(col, &eboneptr, "tail_radius", 0, IFACE_("Radius"), ICON_NONE);
uiItemR(col, &eboneptr, "roll", 0, NULL, ICON_NONE);
uiItemR(col, &eboneptr, "length", 0, NULL, ICON_NONE);
uiItemR(col, &eboneptr, "envelope_distance", 0, IFACE_("Envelope"), ICON_NONE);
}

View File

@ -36,6 +36,8 @@
#ifdef RNA_RUNTIME
# include "BLI_math_vector.h"
# include "BKE_action.h"
# include "BKE_context.h"
# include "BKE_global.h"
@ -483,6 +485,22 @@ static void rna_EditBone_matrix_set(PointerRNA *ptr, const float *values)
ED_armature_ebone_from_mat4(ebone, (float(*)[4])values);
}
static float rna_EditBone_length_get(PointerRNA *ptr)
{
EditBone *ebone = (EditBone *)(ptr->data);
return len_v3v3(ebone->head, ebone->tail);
}
static void rna_EditBone_length_set(PointerRNA *ptr, float length)
{
EditBone *ebone = (EditBone *)(ptr->data);
float delta[3];
sub_v3_v3v3(delta, ebone->tail, ebone->head);
normalize_v3(delta);
madd_v3_v3v3fl(ebone->tail, ebone->head, delta, length);
}
static void rna_Bone_bbone_handle_update(Main *bmain, Scene *scene, PointerRNA *ptr)
{
bArmature *arm = (bArmature *)ptr->owner_id;
@ -1141,27 +1159,28 @@ static void rna_def_bone(BlenderRNA *brna)
prop = RNA_def_property(srna, "matrix", PROP_FLOAT, PROP_MATRIX);
RNA_def_property_float_sdna(prop, NULL, "bone_mat");
RNA_def_property_multi_array(prop, 2, rna_matrix_dimsize_3x3);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Bone Matrix", "3x3 bone matrix");
prop = RNA_def_property(srna, "matrix_local", PROP_FLOAT, PROP_MATRIX);
RNA_def_property_float_sdna(prop, NULL, "arm_mat");
RNA_def_property_multi_array(prop, 2, rna_matrix_dimsize_4x4);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(
prop, "Bone Armature-Relative Matrix", "4x4 bone matrix relative to armature");
prop = RNA_def_property(srna, "tail", PROP_FLOAT, PROP_TRANSLATION);
RNA_def_property_float_sdna(prop, NULL, "tail");
RNA_def_property_array(prop, 3);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_ui_text(prop, "Tail", "Location of tail end of the bone");
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(
prop, "Tail", "Location of tail end of the bone relative to its parent");
RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
prop = RNA_def_property(srna, "tail_local", PROP_FLOAT, PROP_TRANSLATION);
RNA_def_property_float_sdna(prop, NULL, "arm_tail");
RNA_def_property_array(prop, 3);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(
prop, "Armature-Relative Tail", "Location of tail end of the bone relative to armature");
RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
@ -1169,7 +1188,7 @@ static void rna_def_bone(BlenderRNA *brna)
prop = RNA_def_property(srna, "head", PROP_FLOAT, PROP_TRANSLATION);
RNA_def_property_float_sdna(prop, NULL, "head");
RNA_def_property_array(prop, 3);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(
prop, "Head", "Location of head end of the bone relative to its parent");
RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
@ -1177,11 +1196,16 @@ static void rna_def_bone(BlenderRNA *brna)
prop = RNA_def_property(srna, "head_local", PROP_FLOAT, PROP_TRANSLATION);
RNA_def_property_float_sdna(prop, NULL, "arm_head");
RNA_def_property_array(prop, 3);
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(
prop, "Armature-Relative Head", "Location of head end of the bone relative to armature");
RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
prop = RNA_def_property(srna, "length", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_float_sdna(prop, NULL, "length");
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Length", "Length of the bone");
RNA_api_bone(srna);
}
@ -1227,6 +1251,14 @@ static void rna_def_edit_bone(BlenderRNA *brna)
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_update(prop, 0, "rna_Armature_editbone_transform_update");
prop = RNA_def_property(srna, "length", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_float_funcs(prop, "rna_EditBone_length_get", "rna_EditBone_length_set", NULL);
RNA_def_property_range(prop, 0, FLT_MAX);
RNA_def_property_ui_range(prop, 0, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
RNA_def_property_ui_text(prop, "Length", "Length of the bone. Changing moves the tail end");
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
RNA_def_property_update(prop, 0, "rna_Armature_editbone_transform_update");
rna_def_bone_common(srna, 1);
rna_def_bone_curved_common(srna, false, true);

View File

@ -274,6 +274,12 @@ static void rna_PoseChannel_rotation_mode_set(PointerRNA *ptr, int value)
pchan->rotmode = value;
}
static float rna_PoseChannel_length_get(PointerRNA *ptr)
{
bPoseChannel *pchan = ptr->data;
return len_v3v3(pchan->pose_head, pchan->pose_tail);
}
static void rna_PoseChannel_name_set(PointerRNA *ptr, const char *value)
{
Object *ob = (Object *)ptr->owner_id;
@ -1135,6 +1141,11 @@ static void rna_def_pose_channel(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Pose Tail Position", "Location of tail of the channel's bone");
RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
prop = RNA_def_property(srna, "length", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_float_funcs(prop, "rna_PoseChannel_length_get", NULL, NULL);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Length", "Length of the bone");
/* IK Settings */
prop = RNA_def_property(srna, "is_in_ik_chain", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_funcs(prop, "rna_PoseChannel_has_ik_get", NULL);