Hi @orestiskon,
thank you for reaching out to us. The problem with your code is that you pass the number of selected elements as the second argument to BaseSelect.GetRange()
. But the method actually does expect the maximum index there, i.e., the total number of points for a point selection. While this method via .GetRange()
is a bit more performant, in Python you can also use .GetAll()
and list comprehensions. For details see the attached example.
Cheers,
Ferdinand
"""A simple example for how to iterate over point selections.
As discussed in:
https://plugincafe.maxon.net/topic/13115
"""
import c4d
def main():
"""Iterates over the selected objects and accesses their point selection.
"""
# Iterate over the selected objects.
for obj in doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_NONE):
# Step over non-point objects.
if not isinstance(obj, c4d.PointObject):
continue
# All the points of the point object.
points = obj.GetAllPoints()
# And their count.
pointCount = obj.GetPointCount()
# The point selection.
selection = obj.GetPointS()
# The selection state of all elements in the BaseSelect. This is
# a list of booleans, e.g., [True, False, False, False] for a polygon
# object with just one quad and the first point in it being
# selected.
state = selection.GetAll(pointCount)
# We have to manually convert this to indices.
indices = [eid for eid, value in enumerate(state) if value]
# I.e., you can get all selected indices like this in one go:
# [eid for eid, val in enumerate(selection.GetAll(pointCount)) if val]
# Print some stuff out.
print("Object:", obj.GetName())
print("Indices of selected points:", indices)
print("The corresponding points:", [points[i] for i in indices])
# The number of selected elements in the selection.
print ("selection.GetCount():", selection.GetCount())
# The segments in the selection, this has nothing to do with the
# topology, but is rather the number of consecutive selection index
# "strips". E.g.: [True, True, False, True, True] would be two
# segments.
print ("selection.GetSegments():", selection.GetSegments())
# You can also access the selected elements like you tried to.
for segment in range(selection.GetSegments()):
# But you have to pass in here the point count, i.e., the number
# of total total possible indices, not the number of selected
# elements.
print(selection.GetRange(segment, pointCount))
print("-"*79)
if __name__ == '__main__':
main()