Import rhino objects into rhino by groups

Hello,

when i read a rhino file with

a = Rhino.FileIO.File3dm.Read(x).Objects

how can i filter these objects by group or better how can i import a group of geometry objects to rhino by group name.
second option is prefered.

Thanks in advanced

Hi @flokart
If you are using Rhino 6 then this sample will get you started:

import Rhino
import scriptcontext as sc

__author__ = "Lando Schumpich"
__version__ = "1.0"
__email__ = "lando.schumpich@gmail.com"

def getGroupsFromFile3dm():
    
    # your filepath here
    sFilePath = "D:\\RhinoForum\\GroupsFromFile3dm\\groupsFromFile3dm.3dm"
    
    # your desired groupname
    sTargetGroupName = "Group01"
    
    # read file3dm
    file = Rhino.FileIO.File3dm.Read(sFilePath)
    
    # find group for target name
    group = file.AllGroups.FindName(sTargetGroupName)
    if not group: return
    
    # when you open a rhiodoc you could just run groupTable.GroupMembers(iGroupIndex)
    # we have to do it the hard way because there is no helper for file3dm
    # start by iterating over all objects
    arrGroupObjects = []
    for obj in file.Objects:
        # test if object is in our found group
        if group.Index in obj.Attributes.GetGroupList():
            arrGroupObjects.append(obj)
            
    # add all found objects to the rhinodoc
    if arrGroupObjects:
        for obj in arrGroupObjects:
            sc.doc.Objects.Add(obj.Geometry)
            
    # redraw views so we can see what we have done
    sc.doc.Views.Redraw()
        
if __name__ == "__main__":
    
    getGroupsFromFile3dm()

Thanks a lot for your help Lando Schumpich,

unfortunatly

# find group for target name
group = file.AllGroups.FindName(sTargetGroupName)

dont work and i have no idea why, it returns all time None no matter wether groups exist or not , only FindIndex work so i have to iterate over the group table to find the right group.

GrList = []
for i in GrTable:
    if 'TEST00' == i.Name:
        GrList.append(i)

Another thing is that if i bake the object geometry the group attribute get lost and i have to add the object again to the group.

But anyway it works to import group objects and bake it to layers as grouped objects, again thanks a lot for your clean code example.