Mismatch between python and rhinoscript: copyobjects in group

Heyhello. I’m new to rhino and these forums so please do correct any mistakes.

I’ve been trying my hand at scripting to lighten a part of my workload and found an odd mismatch between the output of RhinoScript and Python.

When I try to copy objects inside a group using CopyObjects, Python adds all the copies into the existing group while RhinoScript creates new groups for the copies.

I would prefer it if Python followed the output of RhinoScript since grouping groups seems easier to do than splitting them as well as it just working better in my usecase.

My question is whether the mismatch is unintentional and/or whether it would be worth bringing them in line.

Here, a snippet of Python code to illustrate:

def CopyGroup():
    objects=rs.GetObjects("Select objects",preselect=True)
    group = rs.AddGroup()
    rs. AddObjectsToGroup(objects,group)
    rs.CopyObjects(objects,[-10,0,0])
CopyGroup()

thanks in advance.

A scriptwise workaround requiring only a couple of additional lines of code would be to remove the copied objects from the original group and add them to a new group…

It’s not documented, but what’s probably happening is because CopyGroup always tries to create, and then add the objects, to a group called “Group”. After the first time the function runs, that group will already exist.

Add a string arg called “name” to CopyGroup.

def CopyGroup(name):
    ...
    group = rs.AddGroup(name)
    ...

and call it with the name of the new group.

Or you can even make it check for existing groups and create a unique name itself (if so, I’d return the name, to keep track of all the new objects).

https://developer.rhino3d.com/api/RhinoScriptSyntax/#group-AddGroup

Excuse me, I’m a little rusty and my snippet was a somewhat hastily made. I don’t need to add a specific name for my group. I should have simply done group = rs.AddGroup().

The same interaction remains in that case. I am currently working on a workaround for my usecase, I just found the mismatch odd.

Sample code:

import rhinoscriptsyntax as rs
obj_ids=rs.GetObjects()
copy_ids=rs.CopyObjects(obj_ids, [100,0,0])
for copy_id in copy_ids: rs.RemoveObjectFromAllGroups(copy_id)
new_group=rs.AddGroup()
rs.AddObjectsToGroup(copy_ids,new_group)

for my usecase I don’t know whether my objects are in a group and if not, I don’t want them to be.

My current fix is to do a check and then do as you did. so:

def ObjectsInGroup(objects):
    for object in objects:
        if rs.ObjectGroups(object):
            return True
    return False

import rhinoscriptsyntax as rs
obj_ids=rs.GetObjects()
copy_ids=rs.CopyObjects(obj_ids, [100,0,0])
if ObjectsInGroup(obj_ids):
    for copy_id in copy_ids: rs.RemoveObjectFromAllGroups(copy_id)
    new_group=rs.AddGroup()
    rs.AddObjectsToGroup(copy_ids,new_group)

This still feels like an odd workaround as opposed to if CopyObjects followed RhinoScript, but it works.

Hi @DeVuurtoren,

Seems like we should do better.

https://mcneel.myjetbrains.com/youtrack/issue/RH-85146

Thanks,

– Dale