From a816dd50b9d876fbac2bf09dec5717df452c8391 Mon Sep 17 00:00:00 2001 From: megakite Date: Mon, 18 Mar 2024 20:18:52 +0800 Subject: [PATCH 1/2] Fix #119178: Possible to write non-normalized quaternion from Python A setter is added for the QuaternionAttribute.value RNA property, which provides additional check and normalization for non-normalized inputs. --- .../blender/makesrna/intern/rna_attribute.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/source/blender/makesrna/intern/rna_attribute.cc b/source/blender/makesrna/intern/rna_attribute.cc index 94eb9626b18..24660c61e0f 100644 --- a/source/blender/makesrna/intern/rna_attribute.cc +++ b/source/blender/makesrna/intern/rna_attribute.cc @@ -21,6 +21,7 @@ #include "DNA_pointcloud_types.h" #include "BLI_math_color.h" +#include "BLI_math_quaternion.hh" #include "BKE_attribute.hh" #include "BKE_customdata.hh" @@ -306,6 +307,23 @@ static int rna_Attribute_domain_get(PointerRNA *ptr) BKE_id_attribute_domain(ptr->owner_id, static_cast(ptr->data))); } +static void rna_Attribute_quaternion_value_set(PointerRNA *ptr, const float *values) +{ + using blender::VecBase; + using blender::math::Quaternion; + + float *fs = static_cast(ptr->data); + auto q = Quaternion(VecBase(values)); + if (!is_unit_scale(q)) { + q = normalize(q); + } + + fs[0] = q.w; + fs[1] = q.x; + fs[2] = q.y; + fs[3] = q.z; +} + static bool rna_Attribute_is_internal_get(PointerRNA *ptr) { const CustomDataLayer *layer = (const CustomDataLayer *)ptr->data; @@ -1101,6 +1119,7 @@ static void rna_def_attribute_quaternion(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Value", "Quaternion"); RNA_def_property_float_sdna(prop, nullptr, "x"); RNA_def_property_array(prop, 4); + RNA_def_property_float_funcs(prop, nullptr, "rna_Attribute_quaternion_value_set", nullptr); RNA_def_property_update(prop, 0, "rna_Attribute_update_data"); } -- 2.30.2 From f22da6f59dcc83236b2093798472adedd14f6297 Mon Sep 17 00:00:00 2001 From: megakite Date: Tue, 19 Mar 2024 23:57:38 +0800 Subject: [PATCH 2/2] Remove unnecessary unit check --- source/blender/makesrna/intern/rna_attribute.cc | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/source/blender/makesrna/intern/rna_attribute.cc b/source/blender/makesrna/intern/rna_attribute.cc index 24660c61e0f..cea5dba1e06 100644 --- a/source/blender/makesrna/intern/rna_attribute.cc +++ b/source/blender/makesrna/intern/rna_attribute.cc @@ -311,17 +311,15 @@ static void rna_Attribute_quaternion_value_set(PointerRNA *ptr, const float *val { using blender::VecBase; using blender::math::Quaternion; + using blender::math::normalize; - float *fs = static_cast(ptr->data); - auto q = Quaternion(VecBase(values)); - if (!is_unit_scale(q)) { - q = normalize(q); - } + float *data = static_cast(ptr->data); + const Quaternion src = normalize(Quaternion(VecBase(values))); - fs[0] = q.w; - fs[1] = q.x; - fs[2] = q.y; - fs[3] = q.z; + data[0] = src.w; + data[1] = src.x; + data[2] = src.y; + data[3] = src.z; } static bool rna_Attribute_is_internal_get(PointerRNA *ptr) -- 2.30.2