Python If ... elif ... elif behaves super weird. Am I doing something wrong?

This doesn’t work:

This works:

Test file and both scripts are attached.
PD_Cutlist_BAD.py (6.3 KB) PD_Cutlist.py (6.3 KB)
Cutlist Test.3dm (7.6 MB)

Sorry I’m no compiler, I‘m not gonna try to understand the meaning of this code, but there is a connection between hard-to-debug and hard-to-read. :wink::slightly_smiling_face: Also if you call more then one function in one line , setting a breakpoint is not as helpful, because you can not fully evaluate the current variables.

If you not gonna reach an elif statement then this is because another if/elif has been true before. I‘m pretty sure on Python side anything is working correctly. Have you jumped through this code?

If you encapsulate your subroutines into functions/methods with readable names, you can not only read it better, but also spot logical errors much simpler.

2 Likes

Really hard to read - starting from the awful coloring (dark blue on black background) to the cryptic variable and function names.

2 Likes

The basic answer is that all your other if/elif tests are checking for colour, but the one that fails to run is not a colour check, so remove it from the test cases, and don’t put it at the top of the colour checking either. Take for example the case where the object with usertext Cut:List also has the colour ARGb(255,255,255,90), it has already been dealt with. And if you put it at the top of the if/elif cases it will be dealt with before reaching the colour check.

Not knowing what you’re trying to achieve overall, you maybe want to check the usertext for each colour option.

It won’t hit because the user text test works on objects that also satisfy one of the other tests. Since one of the other tests already is satisfied the last elif in your bad python file won’t get hit.

Your test file has only two objects with the user text List. All of your objects that get picked up by your if-elifs are dimensions with color that have alpha 160. The two that have the usertext set included.

if-elif constructs are evaluated in order. And only the first test to succeed will trigger its code block, none of the others.

1 Like

That’s exactly it! Thank you!

Sorry, R6 doesn’t support Dark mode for built-in editors. And I’m not changing the whole window enviro colors for just script editor.

Oh come on, this is obvious. “elif” stands for “else if” . If I find an object with color white I do this, else (=otherwise) if its black I do this, else that.

color = GetColorAttribute(myObject)

if color == Color.Black:
   DoX()
elif color == Color.White:
   DoY()
else IsList(myObject):
   Delete(myObject)

If you write it like this, you already see the wrong logic in it. Thats basically what @Ncik and me are saying…

Yup, I hadn’t seen all of the comments. But what is obvious to one isn’t necessarily to the other, so I typed it out fully.

I am not contradicting anything you two were saying.

1 Like

Now this is a real freakin’ problem:
It’s a text object I’m clicking created by rs.AddText()

That’s the reason attribute check wasn’t hitting.

Since the color is really bothering me I’ll just remind that you can use three accents and the name of the language as opening tag and another three accents as closing tag to post the code here.

This will also protect your code when discourse people screw up the pictures after a while.

```python
some code
```

Hello,

If I understand correctly, rs.IsDimension returns True for any object which is a subclass of AnnotationObjectBase, i.e Dimensions, leaders and text objects.

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

def IsDimension(object_id):
    "Verifies an object is a dimension object"
    id = rhutil.coerceguid(object_id, True)
    annotation_object = scriptcontext.doc.Objects.Find(id)
    geom = annotation_object.Geometry
    return isinstance(geom, Rhino.Geometry.AnnotationBase)
1 Like

Yeah, that’s wrong IMO… texts should not be considered as dimensions! Otherwise the function should be called IsAnnotation().

And - the vb Rhinoscript version of IsDimension() returns False on text objects…

@dale, @alain - this looks like a bug to me.

1 Like

FWIW, I support the other claim, texts ARE annotations, hence dimensions.

To clarify a bit, they should have all the properties a dimension has scale, font, prefix, suffix etc.

There is confusion because in Rhino 5 there were dimensions and text and they were not related. In Rhino 6 dimensions and text are descendants of annotations and share many properties. Unfortunately not all the concepts have been smoothly transitioned. IsDimension should return false for text objects and maybe there should be an IsAnnotation function. The less confusing way is probably go to RhinoCommon directly:

isAnnotation = isinstance(geom, Rhino.Geometry.AnnotationBase)
isDimension = isinstance(geom, Rhino.Geometry.Dimension)

logged: RH-58483

3 Likes

I’m glad we’re in the same boat. McNeel just needs to fix the colors and we’re good. :wink:

1 Like

Nope… dimensions and text are annotations, but texts are not dimensions.

fair enough