rs.SelectedObjects() fails if ran from button if Rhino has just started

Hi @stevebaer could you look into this?
I have a set of scripts that are ran from a button, but if any of them uses rs.SelectedObjects() Rhino throws an error unless I run a script from the PythonEditor first. (So somethings seem to not be loaded correctly from the button-runner)

image

Ho Jorgen,

Can you share one such button script for testing?

EDIT: are you loading scripts with a macro or is the actual script in the button macro?

-Willem

One script I have imports IFC’s through VisualArQ, selects all, meshes, joins based on layer, exports to a new file and reopens. That one always fails unless I run something in python editor.

I have another one too, that sections through stuff and the thing they share is that they use rs.Common("Mesh ‘+settings’ ") to mesh all kinds of stuff and then uses rs.LastCreatedObjects() to identify them, and that too fails unless I run the exact same script from the python editor first. Then it works flawlessly after.

But if I make a simple tool like this one:

!_-RunPythonScript (

import rhinoscriptsyntax as rs

def makeBox():
    rs.Command("_Box 0 10 10 10")
    box = rs.LastCreatedObjects()[0]
    return box

box = makeBox()
rs.SelectObject(box)

)

Then it works just fine…

So I guess it’s not as easy as to just find the bug then…

hmm indeed difficult to find if there is no reproduction possible.

My first guess would be that your script did not load the correct modules.
The error message reads CustomTable … GetSelectedObjects

The GetSelectedObjects is a method for the ObjectTable not a CustomTable

The python code for SelectedObjects is accesing scriptcontext.doc.Objects so it apprears that from the button scriptcontext.doc.Objects refers to a CustomTable.

Could it be that somewhere in the button scripts scriptcontext is loaded or assigned differently, is there some Grasshopper involved?

-Willem

Hi Willem, thanks for taking a look. The odd thing is that if I use rs.Command(“SelLast”) I get a similar error.

Maybe it has to do with meshing working in the background and a rs.sleep(0) could work, but that doesn’t make sense in the context of running the exact same script from within the python editor just once solves all issues for the instance of Rhino…

Unfortunately I don’t have time to bugtrack this now.

That indicates something more in the Rhino core I’d say.

Does it happen when you run the command SelLast as well?
In that case it could be duplicated with an example file

-Willem

Did you find a repeatable case where a script fails?

No, we hurried off to spend 19.5 hours in the car to pick this little pup up :slight_smile:


And has been busy with the little sweet heart since. I hope I’ll have time tomorrow between playing, feeding and more playing while the kids are at school :grinning_face_with_smiling_eyes:

6 Likes

Oh boy; that’ll keep ya busy. Congratulations

1 Like

Sure does, I’ve just spent an hour and a halv, doing nothing with her sleeping on my lap :grinning_face_with_smiling_eyes:
Step one: making the new home ‘home’ is going well so far.

OK, so I have done some testing on the script that fails unless ran once from the python editor. And I have no idea why it fails to select the objects, BUT if I add a check to see if they exists first as in “if objs: rs.SelectObjects(objs)” then it doesn’t fail and I can run it from a button.
Why this fixes it I don’t know. The thing is that I know that they already exists, so do you have any idea why it works in the python editor with out the check?

I’ll restart the computer to verify that this ‘fixes’ it.

Yep, still works after a restart. So that solves the issue, but doesn’t explain it though.
None the less, it’s never wrong to check if something exists before I select it…

I can send you the script if you want to have a look. It’s my section tool so it needs a display mode installed too.

Sure, send the script. @Alain is this something you can take a look at?

Yes, no problem. I’ll look at it as soon as I get the script.

1 Like

Great, I am trying to simplify it, but this little critter keeps stealing my attention…


I’ll try to be more disiplined next week!

3 Likes