The script below is part of a bigger script and just represents the initial part where the user chooses a planar curve to edit and some options. It was originally written for Py2 and runs fine there.
If I run it via the script editor in Py3 I get the following message:
I wouldn’t know where to look for the source of this error. UnhandledException.py (2.8 KB)
You just need to choose a planar curve, anything will do here, so I didn’t attach a file. In theory, the filter limits the choice to planar curves parallel to World XY - which is how it works in Py2, but as the special filter appears to be disabled because of the error in Py3, it allows you to choose one not in that plane.
yes, and in this context it means that
1: they are parallel and point in the same direction
-1: they are parallel and point in opposite directions
0: they are not parallel
so != 0 is the right operator
the filter is expecting a bool, not an int, and your previous code returned an int, hence the error
However any numerical value returned other than 0 is also considered to be `True" - at least I assumed so and most of the “Truthiness” info pages I found concur. However, in testing it doesn’t appear to be the exact case with negative numbers which seem to equate to False.
print(1==True)
print(-1==True)
print(0==True)
returns: True False False
when I thought it should return True True False
However, in this case, again, whether -1 is interpreted as False or True is not the issue, but rather whether any numerical values are accepted by the filter function to represent True or False. Apparently this is no longer the case, something has changed between Python2 and Python3 in this regard…
However, I can certainly add the comparison operator to the filter definition, it will then work in both Py2 and Py3 which is the goal here, so thanks for testing!!
That is true for Python, but I see that the error message says “cannot be converted to System.Boolean”.
So I guess that the problem here is about converting the return value of the function passed as parameter (to a RhinoCommon function) according to Python rules.
I guess (again ) that Python 3 scripting in Rhino does not (yet ?) handle this situation.
This is correct IMO.
you are directly comparing 2 values here.
And
Yeah.
Values in an if condition are always evaluated as booleans.
That’s why we are not required to use True or False there, but can write any value.
… Also
print( -1 == True )
print( -1 == False )
False False
In my understanding (which may obviously be wrong) True and False in Python are simply syntactic sugar. A less confusing way to write 1 and 0 when we need a boolean.
True and False were introduced in Python 2.3.
But Python used to work fine until then without them.
Here is the PEP about that:
Things haven’t changed in Python 3, True and Fasle are still a type of integers.
These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.
This may be confusing at times, I agree …
… One more Python weirdness …
Thanks for catching this. It’s deep in the pythonnet conversion code that fails to convert the returned value of int to the expected valye of System.Boolean
I have logged this here as major and will get it fixed:
RH-81272 Python int fails to convert to System.Boolean
A quick fix is obv to explicitly cast to bool return bool(s_plane.ZAxis.IsParallelTo(zVec,atol))