Iterate over list of guids error

I’m trying to convert all polysurfaces to blocks for export to sketchup but the code exits with error “iteration over non-sequence of type Guid”.

import rhinoscriptsyntax as rs
import System.Guid

#objs = rs.GetObjects("Select Objects") #, filter = rs.filter.surface)

objs = rs.ObjectsByType(16, False)
print("Types", type(objs), type(objs[0]), objs[0] == System.Guid, objs[0] == str)

for obj in objs:
    point = (0, 0, 0)  # rs.GetPoint("Block base point")
    #objGuid = rs.coerceguid(obj)
    #print("type GUID: ", type(ogjGuid), objGuid == System.Guid)
    #objList = list(obj)
    block = rs.AddBlock(obj, point, None, True)
    rs.InsertBlock(block, point)

The script works when passing objs as a list of Guids (without for loop) to the addBlock function, turning all polysurfaces into a single block. The addblock definition actually says that the function takes an array as first parameter but trying to put the Guid into list within the for loop ends also with error “Guid is not iterable”
Hints are much appreciated, thanks.

rhino for mac 5.5
macOS 10.14.3
Python 2.7.10

Hi Jack,

I’m on my phone so did not test this, but how about:

block = rs.AddBlock([obj], point, None, True)

note I put obj in backets to form a list with obj as single item

Does this help?
-Willem

Hi Willem, thanks for your reply, but still the same error message:
“Message: Guid is not iterable”

Hi can you share a simple file with clear explanation of the expected outcome?

I’m getting rhino models from a colleague every now and then and then do further editing in sketchup. If polysurfaces are exported to sketchup, you end up with bunch of unconnected surfaces and edges, while blocks become components, hence much easier editing in sketchup.

Not the ideal solution but I ended up with rewriting the original AddBlock method for a single Guid
and adding it to a new module (eg “myCustomModule”) within the Rhino app content folder.

run this in atom:

import rhinoscriptsyntax as rs
objs = rs.ObjectsByType(16, False)
for obj in objs:
    point = (0, 0, 0)
    block = rs.AddBlockForSingleObject(obj, point, None, True)  # my rewritten function
    rs.InsertBlock(block, point)

the new module function:

def AddBlockForSingleObject(id, base_point, name=None, delete_input=False):
    base_point = rhutil.coerce3dpoint(base_point, True)
    if not name:
        name = scriptcontext.doc.InstanceDefinitions.GetUnusedInstanceDefinitionName()
    found = scriptcontext.doc.InstanceDefinitions.Find(name, True)
    objects = []

    obj = rhutil.coercerhinoobject(id, True)
    if obj.IsReference: return
    ot = obj.ObjectType
    if ot==Rhino.DocObjects.ObjectType.Light: return
    if ot==Rhino.DocObjects.ObjectType.Grip: return
    if ot==Rhino.DocObjects.ObjectType.Phantom: return
    if ot==Rhino.DocObjects.ObjectType.InstanceReference and found:
        uses, nesting = obj.UsesDefinition(found.Index)
        if uses: return
    objects.append(obj)

    if objects:
        geometry = [obj.Geometry for obj in objects]
        attrs = [obj.Attributes for obj in objects]
        rc = 0
        if found:
          rc = scriptcontext.doc.InstanceDefinitions.ModifyGeometry(found.Index, geometry, attrs)
        else:
          rc = scriptcontext.doc.InstanceDefinitions.Add(name, "", base_point, geometry, attrs)
        if rc>=0:
            if delete_input:
                for obj in objects: scriptcontext.doc.Objects.Delete(obj, True)
            scriptcontext.doc.Views.Redraw()
    return name
1 Like