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
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Expression doesn't update property correctly

Post by onekk »

When dealing with rotations you have to be aware of some things.

Precedence of axis there are no a unique convention but at more than 20. I could post later a link where this matter is discussed.

If you use valued bigger than 1.0 you are not using normalized values.

the gimbal lock problem

the fact that a rotation as a sense if it is limited to a turn (0 - 360 degrees) a rotation of 370 degree is equivalent to a rotation of 10 degree, final position of solid is the same.

When things get hard, I usually use Matrix even if they are not perfect (gimbal lock) they permit to apply rotation in the desired order, but I usually script my work so it is a very low level approach.

Later I will post the link when I have fired up my computing machine. Now I'm using a dumbphone.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
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 »

onekk wrote: Tue Dec 06, 2022 6:48 am If you use valued bigger than 1.0 you are not using normalized values.
This problem is there whether I calculate normalized values or not.
exp_err5.png
exp_err5.png (27.09 KiB) Viewed 794 times
It is impossible to use expressions at all on the individual X, Y and Z of the Axis.
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 »

Workaround is to set an expression on the Axis instead. There it is no problem with a non normalized vector.

Code: Select all

create(<<vector>>;1;1;1)
But it should work on the individual values too or else the expression button should be removed from them.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Expression doesn't update property correctly

Post by onekk »

First thing, link promised:

https://forum.freecadweb.org/viewtopic. ... 5&start=20

Second thing, here it is working as expected:

see:
20221207-Angles.png
20221207-Angles.png (24.21 KiB) Viewed 721 times
with this FC version:

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git) AppImage
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: Italian/Italy (it_IT)
Installed mods: 
  * QuickMeasure 2022.10.28
  * Assembly4 0.12.4
  * fasteners 0.4.24
  * toSketch 1.0.1
  * Curves 0.6.1
  * Help 1.0.3
Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
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 thing is if you replace the expression with a new one that gives the same result you force a recalculation and for each recalculation you get closer to expected result. Try to replace your expressions with:
1 * 1 / sqrt(2)
1 * 1 / sqrt(2)
1 * 1 * 0
What is your immediate result then?
onekk wrote: Wed Dec 07, 2022 7:52 am Second thing, here it is working as expected
Also note that in your row for Placement it shows [(0.62 0,58 0,58);....
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Expression doesn't update property correctly

Post by onekk »

Jolbas wrote: Wed Dec 07, 2022 8:41 am The thing is if you replace the expression with a new one that gives the same result you force a recalculation and for each recalculation you get closer to expected result. Try to replace your expressions with:
1 * 1 / sqrt(2)
1 * 1 / sqrt(2)
1 * 1 * 0
What is your immediate result then?

No it is not at least for me:
test_expressions.FCStd
(3.53 KiB) Downloaded 21 times
and:
20221207-Angles2.png
20221207-Angles2.png (23.79 KiB) Viewed 687 times
with 90 degree in Angle and the rotation as above, I will obtain

Code: Select all

obj.Placement.toMatrix()
Matrix ((0.5,0.5,0.707107,0),(0.5,0.5,-0.707107,0),(-0.707107,0.707107,0,0),(0,0,0,1))
obj.Placement.Rotation
Rotation (0.5, 0.5, 0.0, 0.7071067811865476)
obj.Placement.Rotation.Axis
Vector (0.7071067811865476, 0.7071067811865476, 0.0)
obj.Placement.Rotation.Angle
1.5707963267948966
At least for me it seems consistent with expected values, see obj.Placement.Rotation.Axis that resemble those printed in the image

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
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 »

Ok @onekk . So this is not affecting all versions? The main difference is that I have a macOS:

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
  * dxf-library
  * fcgear 1.0.0
  * __pycache__
  * sheetmetal 0.2.53
  * Curves 0.5.6
  * Assembly4 0.12.4
I get this results from python console. Resetting the Axis first:

Code: Select all

>>> obj.setExpression('.Placement.Rotation.Axis.x',None)
>>> obj.setExpression('.Placement.Rotation.Axis.y',None)
>>> obj.setExpression('.Placement.Rotation.Axis.z',None)
>>> obj.Placement.Rotation.Axis.x = 1
>>> obj.Placement.Rotation.Axis.y = 0
>>> obj.Placement.Rotation.Axis.z = 0
>>> obj.Placement.Rotation.Axis
Vector (1.0, 0.0, 0.0)
>>> obj.setExpression('.Placement.Rotation.Axis.x', '1 * 1 / sqrt(3)')
>>> obj.Placement.Rotation.Axis
Vector (1.0, 0.0, 0.0)
>>> obj.setExpression('.Placement.Rotation.Axis.y', '1 * 1 / sqrt(3)')
>>> obj.Placement.Rotation.Axis
Vector (1.0, 0.0, 0.0)
>>> obj.setExpression('.Placement.Rotation.Axis.z', '1 * 1 / sqrt(3)')
>>> obj.Placement.Rotation.Axis
Vector (1.0, 0.0, 0.0)
>>> obj.recompute()
True
>>> obj.Placement.Rotation.Axis
Vector (0.7499999999999999, 0.43301270189221935, 0.5000000000000001)
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Expression doesn't update property correctly

Post by adrianinsaval »

onekk wrote: Wed Dec 07, 2022 9:43 am
If you look at the values shown at the Placement line there's still something weird going on.
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 can reproduce similar errors without using expressions but by assigning values to Axis parts in python console.

If I set the axis vector to a non normalized vector:

Code: Select all

obj.Placement.Rotation.Axis = App.Vector(1,1,1)
Then I can ask for the RawAxis

Code: Select all

obj.Placement.Rotation.RawAxis
Returns: Vector (1.0, 1.0, 1.0)

And the Axis attribute returns a normalized vector

Code: Select all

obj.Placement.Rotation.Axis
Returns: Vector (0.5773502691896258, 0.5773502691896258, 0.5773502691896258)

This all seems to be good, but if I change one of the Axis parameters a normalization event is applied on the RawAxis. Appearantly before assigning the new value which gives unexpexted result:

Code: Select all

obj.Placement.Rotation.Axis.z = 2
obj.Placement.Rotation.RawAxis
Returns: Vector (0.5773502691896258, 0.5773502691896258, 2.0)

And

Code: Select all

obj.Placement.Rotation.Axis
Returns: Vector (0.2672612419124244, 0.2672612419124244, 0.9258200997725514)
Which is a normalized representation of RawAxis
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 »

@onekk Do you get expected results from python commands above?
Post Reply