This seems to be a bug in the current version of Rhino 7. It was already reported and will probably be fixed in one of the coming updates. Check this out:
If you want to, you can use my GHPython script that accomplishes the same and has some more functionality. It can be found in the same thread: