SOLVED How can I set a selection after I create a selection tag?

edited by [ @ferdinand ]:

How can I set the selection state the underlying c4d.BaseSelectof a c4d.SelectionTag?

@LingZA said:

For an exmaple like op.MakeTag(c4d.Tedgeselection).

BaseSelect1.CopyTo(BaseSelect2)
BaseSelect2 is of selection tag.

Hello @lingza,

Thank you for reaching out to us. Please read our forum guidelines.

  • Please provide a meaningful description of your problem in the body of a topic. Even a very descriptive topic title as yours is not enough alone.
  • Do not create multiple postings in a topic until someone has answered.
  • If you solve a problem yourself, or gather information that describes your problem better, please edit that information into your original question.
  • Please also refrain from deleting postings in cases where your posting did not contain incorrect information. When you delete your question and only leave your own answer, a topic will become hard to read for other users who might have the same question.

I did restore and consolidate your postings for you in this case. I also provided a small example snippet which provides an answer, even though you have a solution of your own.

Cheers,
Ferdinand

"""Provides an example for writing the selection state of a point selection tag.

The script will get an existing or create a new point selection tag for the selected object and 
write a random point selection into that tag.
"""

from typing import Optional
import c4d
import random

doc: c4d.documents.BaseDocument  # The active document
op: Optional[c4d.BaseObject]  # The active object, None if unselected

def main() -> None:
    """
    """
    # Ensure the active object can carry a point selection tag in a meaningful way; is a point 
    # object that has at least one vertex.
    if not isinstance(op, c4d.PointObject) and op.GetPointCount() > 0:
        raise RuntimeError("Please select a polygon or spline object that does contain vertcies.")

    # Get the first existing point selection tag on the object or create a new one.
    tag = op.GetTag(c4d.Tpointselection)
    if not isinstance(tag, c4d.SelectionTag):
        tag = op.MakeTag(c4d.Tpointselection)

    # Get the number of vertices in the object, the selection from the tag, and build a generator 
    # for a random set of vertex indices.
    pointCount = op.GetPointCount()
    selection = tag.GetBaseSelect()
    selectionCount = max(1, int(pointCount / 4))
    indeciesToSelect = random.choices(range(pointCount), k=selectionCount)

    # Deselect all items in the selection first and then select all new indices which should be
    # selected.
    selection.DeselectAll()
    for index in indeciesToSelect:
        selection.Select(index)

    # Push an update event to Cinema 4D. The selection tag will now contain the new selection which has
    # been defined for #selection.
    c4d.EventAdd()

if __name__ == '__main__':
    main()

Hello @lingza,

Thank you for reaching out to us. Please read our forum guidelines.

  • Please provide a meaningful description of your problem in the body of a topic. Even a very descriptive topic title as yours is not enough alone.
  • Do not create multiple postings in a topic until someone has answered.
  • If you solve a problem yourself, or gather information that describes your problem better, please edit that information into your original question.
  • Please also refrain from deleting postings in cases where your posting did not contain incorrect information. When you delete your question and only leave your own answer, a topic will become hard to read for other users who might have the same question.

I did restore and consolidate your postings for you in this case. I also provided a small example snippet which provides an answer, even though you have a solution of your own.

Cheers,
Ferdinand

"""Provides an example for writing the selection state of a point selection tag.

The script will get an existing or create a new point selection tag for the selected object and 
write a random point selection into that tag.
"""

from typing import Optional
import c4d
import random

doc: c4d.documents.BaseDocument  # The active document
op: Optional[c4d.BaseObject]  # The active object, None if unselected

def main() -> None:
    """
    """
    # Ensure the active object can carry a point selection tag in a meaningful way; is a point 
    # object that has at least one vertex.
    if not isinstance(op, c4d.PointObject) and op.GetPointCount() > 0:
        raise RuntimeError("Please select a polygon or spline object that does contain vertcies.")

    # Get the first existing point selection tag on the object or create a new one.
    tag = op.GetTag(c4d.Tpointselection)
    if not isinstance(tag, c4d.SelectionTag):
        tag = op.MakeTag(c4d.Tpointselection)

    # Get the number of vertices in the object, the selection from the tag, and build a generator 
    # for a random set of vertex indices.
    pointCount = op.GetPointCount()
    selection = tag.GetBaseSelect()
    selectionCount = max(1, int(pointCount / 4))
    indeciesToSelect = random.choices(range(pointCount), k=selectionCount)

    # Deselect all items in the selection first and then select all new indices which should be
    # selected.
    selection.DeselectAll()
    for index in indeciesToSelect:
        selection.Select(index)

    # Push an update event to Cinema 4D. The selection tag will now contain the new selection which has
    # been defined for #selection.
    c4d.EventAdd()

if __name__ == '__main__':
    main()

Hello @LingZA,

without any further questions or other postings, we will consider this topic as solved and flag it as such by Friday, 17/06/2022.

Thank you for your understanding,
Ferdinand