Grasshopper Python

Hi all, I am trying to execute a simple python script that doesn’t seem to be working as it’s returning

Please see attached image for explanation.

You need to learn Python’s basics first.

You are assigning result to Out, where result is the actual output.

Thanks for pointing that out. The result still doesn’t appear to be executing the right item, do you know what’s missing?

I’m not quite familiar with Python but it seems that x is a NET list from Grasshopper (likely a System.Collections.Generic.List<>), which cannot be directly compared against a python list [0, 1, 0] here.

I also doubt if your input is an integer list. Is it a point or a list of numbers (rather than integers)? It’s hard to guess without the actual GH file.

From your screenshot I can see {1, 0, -7.2032e-12}, I assume the condition is a list of numbers. It might cause issue in comparsion, e.g., 1 ≠ 1.000.

Python List sorting.gh (8.9 KB)
Hi,

Thanks for your reply. My thoughts as well about the list comparison but I am unsure how to fix this. Any help will be appreciated.

I have attached the grasshopper file.

Thanks in advance

Oh, so your input is a vector which cannot be directly compared against a list.
Try this:

"""Provides a scripting component.
    Inputs:
        x: The x script variable
        y: The y script variable
    Output:
        a: The a output variable"""

__author__ = "A.M"
__version__ = "2022.08.25"

Out = []
Tolerance = 1e-7

if abs(x.X) < Tolerance and abs(x.Y - 1) < Tolerance and abs(x.Z) < Tolerance:
    Out.append(x)
    
elif abs(x.X) < Tolerance and abs(x.Y + 1) < Tolerance and abs(x.Z) < Tolerance:
    Out.append(x)
    
else:
    Out.append(y)

result = Out
1 Like

The underlying RhinoCommon Vector3D class has a lot of useful methods you will want to get acquainted with. In this case you could use the Vector3d.IsParallelTo() method to check if the first vector is parallel to the world YAxis:


229825_VectorParallelToYAxis_00.gh (2.6 KB)

Note that I’ve set the parameter input type hints to Vector3D to use these methods:

Edit: Here’s a slightly terser take on the conditional, that avoids assigning the parallel check to a variable:

import Rhino as rc

if VecA.IsParallelTo(rc.Geometry.Vector3d.YAxis) in (-1,1):
    Foo = VecA
else:
    Foo = VecB
2 Likes

I don’t think hinting params as Vector3d is necessary. Python (and even the editor) can handle that.

Thank you, this worked like a charm. This might seem like a dumb question but what is the purpose of the Tolerance you added and could the script have worked without adding the tolerance?

Thanks a lot Andres, your first solution worked as well. The second one produced a null result.

It depends on the quality of inputs. For example, the vector of a curve can be {0, 1.00000001, 0} and you can barely see any visual difference , but since 1.00000001≠1 and inputs would fail the condition. The tolerance is used to control this.

1 Like

Right! That makes sense, thank you for the explanation.

That’s odd, they work the same for me:


229825_VectorParallelToYAxis_01.gh (3.5 KB)

Also note that you can use this overload instead, if you require a different tolerance than the default one degree:

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Vector3d_IsParallelTo_1.htm