I’m wondering if anyone has a quick workaround for ObjectTopGroup which is apparently not implemented in Python yet…
My first guess is the count the members in all the groups an object is attached to and go with the one that has the most members, but I’m sure there is a more foolproof/better way…
I got such a great response to my last question, I thought I’d keep asking
cheers peter
I don’t think that will get you there… The number of members in a group will not help determine what level of grouping you’re at. I’m not sure what the official definition of “Top group” is or what the logical approach to getting it should be… There are so many scenarios possible. For example, in the image below, which is the top group…?
I was wondering the same: What defines the top group.
Maybe it’s simply checking on the last group an object is added to.
@Dale, Can you elaborate on how Rhinoscript determines what the topgroup of an object is ?
That would make it easier to recreate a python equivalent for it.
Exactly. Each groups gets added to the GroupTable. The last group in the GroupTable where object has been “spotted”, is its top group.
import rhinoscriptsyntax as rs
import scriptcontext as sc
id = rs.GetObject()
groupNames = sc.doc.Groups.GroupNames(False)
groupName = False
for i in range(rs.GroupCount()):
groupRO = sc.doc.Groups.GroupMembers(i)
for ele in groupRO:
if rs.coercerhinoobject(ele).Id == id:
groupName = groupNames[i]
if groupName:
print groupName
else:
print "the element you chose does not belong to any group"
Hi djordje, i´m not shure. Please try it in below file. The red sphere belongs only to one group named “Group01” but the script returns Group03 as top group. Groups.3dm (54.7 KB)
I´m wondering what has to be done if the top group has been deleted but the group name is still listed in the group table.
Have you tried it on the red sphere which only belongs to Group01 ? I´ve too compared with below script from the vbscript helpfile which gives different results compared to the python version above:
Call Main()
Sub Main()
Dim strObject, strGroup
strObject = Rhino.GetObject("Select object",, True)
If Not IsNull(strObject) Then
strGroup = Rhino.ObjectTopGroup(strObject)
If Not IsNull(strGroup) Then
Rhino.Print "Top object group: " & strGroup
End If
End If
End Sub
Ok. Btw. In my file there is no deleted group. I´ve just been wondering myself how to do it.
Strangely, my counting members strategy seems to be working
I am organising paths to be sent off for lasercutting. I am looking to collect all the objects in layer and arrange them in a rough ‘nest’. Many of these ‘objects’ are grouped collections of paths, and I want to make sure I move the whole group and an not the sub objects.
def ObjectTopGroup(obj_id):
grplist = rs.ObjectGroups(obj_id)
if grplist:
biggestGroup = 0
for grp_id in grplist:
memberCount = len(rs.ObjectsByGroup(grp_id))
if memcnt >= biggestGroup:
topGroup = grp_id
biggestGroup = memberCount
return topGroup
else:
return False
This seems to work as I hoped, returning the group name that contains all of the curves that would be selected if you were to click on something in the Rhino window. I can use this to manipulate the whole group and find the bounding box etc.
Perhaps what I am looking for is not the ‘top group’, but this seems to work for me. It allows me to specify a sub-object and get all the other objects that are linked to it in the group, and that is all I need
cheers
peter
UPDATE:
I realise now this only works for me because of how I model. I don’t ever combine sub-objects from one group with another: I just make larger, all-encompassing groups, which is why this works for me.
In this context, the ‘top group’ is easy to calculate.
Ok, now I feel a bit dumb… But how did you create those overlapping groups?!
Did you do it ‘by hand’ or was it scripted?
I feel like I’ve missed something in how Rhino works at a very basic level…