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 :smile:

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