Ho to test for null objects in GhPython?


#1

I try to filter away some Null points from a list with GhPython but trying the following code I still end up having 24 Null points in my lists… :slight_smile:

import rhinoscriptsyntax as rs
import Rhino

if pt_list:
    P = []
    for pt in pt_list:
        # Tried both variants below, but no go
        if pt:                 <- Does not filter away
        if pt is not None:     <- Does not filter away
            P.append(pt)

I searched for “Null” but no hits.

// Rolf


#2

There is also a component: [Clean]

Or upload a definition with internalised data…


#3

Ah, thank you for that tips!

My fantasy fails on my in trying to come up with all variants of words for dealing with Remove / Delete / IsNull, Not Null and… why didn’t I think of Clean… ?

But then one is forced to make lotsa good filters… :slight_smile:

// Rolf


(Giulio Piacentino) #4

Hi RIL,

Usually there is no “Null”/None point, because Point3d is a structure. While Python can create None and assign it to any variable, strongly-typed .Net languages do not support None on plain structs. There is a special Point3d.Unset value, that is sometimes used for None. You can use is not None followed by and pt.IsValid. That will also filter out any infinite points.


#5

Thank you @Piac, I’ll try that as well.

So these spooky “null” value points are actually valid objects (technically speaking) while conceptually being so called “null-objects”? Well, I’ve heard that name for objects that actually do exist but is meant to represent “null” for invalid data values/structures as to avoid fatal crashes by accidentally referencing nil pointers?

Anyway, whatever method to be used, what I’m looking for is the super fastest possible, since I generate lots of (gh) points on irregular surfaces and need to filter them in several stages in order to gradually “close in” on my target surfaces, which means I have to filter thousands of points in lists.


Edit:
BTW,

Q1: are VB.NET scripts faster (assuming that vb script components gets compiled?)

Q2: does GhPython apply “boolean shortcircuit” in expressions like the following code which I call (“NullPtFilter”) for point list?

if pt_list:
    P = []
    for pt in pt_list:    
        #// collect only valid points :
        if pt is not None and (pt.IsValid or (pt is not False) or (pt is not True)) 
            P.append(pt)

// Rolf


(Giulio Piacentino) #6

Yes, because of how Rhino is setup, “null” / invalid might actually be something different than Python’s None. I mean, None is exactly always None, but there are some values that mean undefined/incorrect/unset data. This is similar to float ‘NaN’ (not a number, but it’s a float, still), which is documented in Python, too: https://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python

It might be, but I doubt it will make a significant difference here.

The question here is whether Python applies shortcircuiting. The answer is, the Python "or" operator does. See the Python docs.

I hope this helps,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com