Expression doesn't update property correctly

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Expression doesn't update property correctly

Post by Jolbas »

I found an issue with expressions on some Placement properties.

In the example file I have two cones. Cone001 has expression cos(90 * Variables.Index) on .Placement.Rotation.Axis.X and sin(90 * Variables.Index) on .Placement.Rotation.Axis.Y. Cone002 has expression create(<<vector>>; cos(90 * Variables.Index); sin(90 * Variables.Index); 0) on .Placement.Rotation.Axis. The two cones should be placed at the same position when changing Variables.Index but on some Indexes Cone001 is placed wrong. The result of each expression seems ok but the Placement property is not updated correctly.
exp_err.png
exp_err.png (74.46 KiB) Viewed 1268 times
I'm not able at the moment to try this in the development build. It may be related to Issues #7589, #7489, #7390 but they are all in Scetcher

If I edit the expression it sometimes updates correctly but if I change Variables.Index to 4 and then back to 5 the error is back
exp_err2.png
exp_err2.png (23.71 KiB) Viewed 1268 times

Code: Select all

OS: macOS 10.16
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git)
Build type: Release
Branch: (HEAD detached at 0.20.1)
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: C/Default (C)
Installed mods: 
  * kugghjul.py
  * fcgear 1.0.0
  * __pycache__
  * sheetmetal 0.2.53
  * Curves 0.5.6
  * Assembly4 0.12.4
Attachments
exprossion_error.FCStd
(5.44 KiB) Downloaded 20 times
chrisb
Veteran
Posts: 53921
Joined: Tue Mar 17, 2015 9:14 am

Re: Expression doesn't update property correctly

Post by chrisb »

Can you reproduce in 0.21?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

chrisb wrote: Fri Nov 25, 2022 9:31 pm Can you reproduce in 0.21?
Yes.
exp_err3.png
exp_err3.png (42.66 KiB) Viewed 1194 times

Code: Select all

OS: macOS 10.16
Word size of FreeCAD: 64-bit
Version: 0.21.31097 (Git)
Build type: Release
Branch: master
Hash: ccf074008774f1bdfed8fec09ed60e2c0fe77cae
Python 3.10.8, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: C/Default (C)
Installed mods: 
  * kugghjul.py
  * fcgear 1.0.0
  * __pycache__
  * sheetmetal 0.2.53
  * Curves 0.5.6
  * Assembly4 0.12.4
chrisb
Veteran
Posts: 53921
Joined: Tue Mar 17, 2015 9:14 am

Re: Expression doesn't update property correctly

Post by chrisb »

I can reproduce, despite the model showing errors due to some Assembly4 stuff missing. It seems to be the same as issue #7390, at least the workaround is the same: You have to make the expression more complicated, e.g. by including some other property. If I use

cos(90 * Variables.Index) * Radius1 / Radius1

it updates correctly. I have added a link to this topic to the Github issue.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

chrisb wrote: Sat Nov 26, 2022 7:03 am You have to make the expression more complicated, e.g. by including some other property. If I use

cos(90 * Variables.Index) * Radius1 / Radius1

it updates correctly. I have added a link to this topic to the Github issue.
That doesn't work. If I change Variables.Index to 4 and then back to 5 the error is there again.
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

... but changing from 3 to 5 works ??

chrisb wrote: Sat Nov 26, 2022 7:03 am despite the model showing errors due to some Assembly4 stuff missing.
Here is a file without references to Asm4_libs
Attachments
expression_error.FCStd
(5.21 KiB) Downloaded 22 times
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

A recompute on the wrongly placed helps when the angle step is set to 90°. But if I set the angle to multiples of 45° instead, a recompute just moves it closer and closer to the correct placement. And you need to do a lot of recomputing to get it right.

Made some further tests and maybe it has to do with the normalization of the axis vector. Setting expressions to static values also showing weird results:
exp_err4.png
exp_err4.png (22.62 KiB) Viewed 964 times
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

Her is a minimized document showing static expressions gives wrong result: A part with the placement rotation set to 120° and the axis vectors X, Y and Z is all set to 1.0 but in expressions. Expected to have the part aligned with the global origin but it is not
axis_expression_error.png
axis_expression_error.png (107.46 KiB) Viewed 950 times
Attachments
axis_expression_error.FCStd
(3.4 KiB) Downloaded 18 times
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

I think I found a pattern and this is what seems to happen:
1. The expression for Axis.X is calculated
2. The axis vector is normalized
3. The expression for Axis.Y is calculated
4. The axis vector is normalized
5. The expression for Axis.Z is calculated
4. The axis vector is normalized but the result is not shown in the property editor

I think the wanted steps would rather be:
1. The expression for Axis.X is calculated
2. The expression for Axis.Y is calculated
3. The expression for Axis.Z is calculated
4. The axis vector is normalized

I haven't found the relevant code yet though. Maybe someone else has a clue where to look.

EDIT: It is normalized after Z axis is set too but the property editor is not updated.
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

The expected behaviour i not obvious when there is expressions of some of the X, Y and Z parts but not all.

I wonder if the rotation axis vector actually not should be normalized when set. There is a companion quaternion saved along with the axis and angle. And the Rotation.getAxis() function seems to return a normalized vector anyway.
Post Reply