Find block & explode & group

Hi all, I did a short script in python in Rhino 5 before. It works in Rhino 5, but for some reason it doesn’t work in Rhino 7. I am not sure what’s wrong… Hope you can help, thanks
The script is as below:
import rhinoscriptsyntax as rs
import scriptcontext

def explodeBlocks():

rs.Command("_SelBlockInstance")
Blocks = rs.GetObjects("Pick Blocks",4096,False,True,False)

if Blocks is None:return

for b in Blocks:
    
    if scriptcontext.escape_test(False):    #Break if press 'ESC'
        break
    
    rs.SelectObject(b)
    rs.Command("_ExplodeBlock")
    rs.Command("_SelLast")
    item = rs.GetObjects("Pick items",0,False,True,True)
    if len(item) == 1:continue     #if the block has one item only,then don't group
    rs.Command("_Group")
    rs.Command("_SelNone")
    
    if scriptcontext.escape_test(False):    #Break if user press 'ESC'
        break
rs.Command("_SelAll")
rs.ZoomSelected()

explodeBlocks()

Hi @Jack_Zeng,

We might need a sample .3dm file too.

Thanks,

– Dale

Hi @Jack_Zeng ,
You could approach this two ways for a quick working solution:

for b in block : 
 if b : 
  [ur code]

Or you could try adding exception handling

for b in block : 
 try : 
  [your code]
 except : 
   continue

However I would suggest designing the script to avoid getting into these try except scenarios, high quality maintainable code is always worth it in the long run.
Be that as it may for your use case exception handling may be enough
Hope this sparks some ideas,
Farouk

Thanks, Farouk