"""
Copyright: MAXON Computer GmbH
Author: Manuel Magalhaes

Description:
    Retrieve the selected node material and group the node that are selected.
    
Class/method highlighted:
    - MoveToGroup
    - IsSelected
"""
import c4d
import maxon


def main():
    # Retrieve the selected BaseMaterial
    mat = doc.GetActiveMaterial()
    if mat is None:
        raise ValueError("There is no selected BaseMaterial")

    # Retrieve the reference of the material as a node material.
    nodeMaterial = mat.GetNodeMaterialReference()
    if nodeMaterial is None:
        raise ValueError("Cannot retrieve node material reference")

    # Retrieve the current node space Id
    nodespaceId = c4d.GetActiveNodeSpaceId()

    # Retrieve the Nimbus reference for a specific node space
    nimbusRef = mat.GetNimbusRef(nodespaceId)
    if nimbusRef is None:
        raise ValueError("Cannot retrieve the nimbus ref for that node space")

    # Retrieve the graph corresponding to that node space.
    graph = nimbusRef.GetGraph()
    if graph is None:
        raise ValueError("Cannot retrieve the graph of this nimbus ref")

    # Get the root of the GraphNode
    root = graph.GetRoot()

    # Retrieve all nodes, child of the root node
    nodes = []
    root.GetChildren(nodes, maxon.NODE_KIND.NODE)

    # Create a list of the selected ones.
    selectedNodes = []

    for node in nodes:
        if node.IsSelected():
            selectedNodes.append(node)

    # Group all the selected nodes in an empty node.
    groupRoot = maxon.GraphNode()

    # To modify a graph, modification must be done inside a transaction. After
    # modifications are done, the transaction must be committed.
    with graph.BeginTransaction() as transaction:
        graph.MoveToGroup(groupRoot, maxon.Id("idOfMyGroup"), selectedNodes)
        transaction.Commit()

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()


if __name__ == "__main__":
    main()

dbfc97564fb0189942b4e92ba715ab9d967991c5
<mask token>

<mask token>


def main():
    mat = doc.GetActiveMaterial()
    if mat is None:
        raise ValueError('There is no selected BaseMaterial')
    nodeMaterial = mat.GetNodeMaterialReference()
    if nodeMaterial is None:
        raise ValueError('Cannot retrieve node material reference')
    nodespaceId = c4d.GetActiveNodeSpaceId()
    nimbusRef = mat.GetNimbusRef(nodespaceId)
    if nimbusRef is None:
        raise ValueError('Cannot retrieve the nimbus ref for that node space')
    graph = nimbusRef.GetGraph()
    if graph is None:
        raise ValueError('Cannot retrieve the graph of this nimbus ref')
    root = graph.GetRoot()
    nodes = []
    root.GetChildren(nodes, maxon.NODE_KIND.NODE)
    selectedNodes = []
    for node in nodes:
        if node.IsSelected():
            selectedNodes.append(node)
    groupRoot = maxon.GraphNode()
    with graph.BeginTransaction() as transaction:
        graph.MoveToGroup(groupRoot, maxon.Id('idOfMyGroup'), selectedNodes)
        transaction.Commit()
    c4d.EventAdd()


<mask token>

<mask token>


def main():
    mat = doc.GetActiveMaterial()
    if mat is None:
        raise ValueError('There is no selected BaseMaterial')
    nodeMaterial = mat.GetNodeMaterialReference()
    if nodeMaterial is None:
        raise ValueError('Cannot retrieve node material reference')
    nodespaceId = c4d.GetActiveNodeSpaceId()
    nimbusRef = mat.GetNimbusRef(nodespaceId)
    if nimbusRef is None:
        raise ValueError('Cannot retrieve the nimbus ref for that node space')
    graph = nimbusRef.GetGraph()
    if graph is None:
        raise ValueError('Cannot retrieve the graph of this nimbus ref')
    root = graph.GetRoot()
    nodes = []
    root.GetChildren(nodes, maxon.NODE_KIND.NODE)
    selectedNodes = []
    for node in nodes:
        if node.IsSelected():
            selectedNodes.append(node)
    groupRoot = maxon.GraphNode()
    with graph.BeginTransaction() as transaction:
        graph.MoveToGroup(groupRoot, maxon.Id('idOfMyGroup'), selectedNodes)
        transaction.Commit()
    c4d.EventAdd()


if __name__ == '__main__':
    main()

<mask token>
import c4d
import maxon


def main():
    mat = doc.GetActiveMaterial()
    if mat is None:
        raise ValueError('There is no selected BaseMaterial')
    nodeMaterial = mat.GetNodeMaterialReference()
    if nodeMaterial is None:
        raise ValueError('Cannot retrieve node material reference')
    nodespaceId = c4d.GetActiveNodeSpaceId()
    nimbusRef = mat.GetNimbusRef(nodespaceId)
    if nimbusRef is None:
        raise ValueError('Cannot retrieve the nimbus ref for that node space')
    graph = nimbusRef.GetGraph()
    if graph is None:
        raise ValueError('Cannot retrieve the graph of this nimbus ref')
    root = graph.GetRoot()
    nodes = []
    root.GetChildren(nodes, maxon.NODE_KIND.NODE)
    selectedNodes = []
    for node in nodes:
        if node.IsSelected():
            selectedNodes.append(node)
    groupRoot = maxon.GraphNode()
    with graph.BeginTransaction() as transaction:
        graph.MoveToGroup(groupRoot, maxon.Id('idOfMyGroup'), selectedNodes)
        transaction.Commit()
    c4d.EventAdd()


if __name__ == '__main__':
    main()

"""
Copyright: MAXON Computer GmbH
Author: Manuel Magalhaes

Description:
    Retrieve the selected node material and group the node that are selected.
    
Class/method highlighted:
    - MoveToGroup
    - IsSelected
"""
import c4d
import maxon


def main():
    # Retrieve the selected BaseMaterial
    mat = doc.GetActiveMaterial()
    if mat is None:
        raise ValueError("There is no selected BaseMaterial")

    # Retrieve the reference of the material as a node material.
    nodeMaterial = mat.GetNodeMaterialReference()
    if nodeMaterial is None:
        raise ValueError("Cannot retrieve node material reference")

    # Retrieve the current node space Id
    nodespaceId = c4d.GetActiveNodeSpaceId()

    # Retrieve the Nimbus reference for a specific node space
    nimbusRef = mat.GetNimbusRef(nodespaceId)
    if nimbusRef is None:
        raise ValueError("Cannot retrieve the nimbus ref for that node space")

    # Retrieve the graph corresponding to that node space.
    graph = nimbusRef.GetGraph()
    if graph is None:
        raise ValueError("Cannot retrieve the graph of this nimbus ref")

    # Get the root of the GraphNode
    root = graph.GetRoot()

    # Retrieve all nodes, child of the root node
    nodes = []
    root.GetChildren(nodes, maxon.NODE_KIND.NODE)

    # Create a list of the selected ones.
    selectedNodes = []

    for node in nodes:
        if node.IsSelected():
            selectedNodes.append(node)

    # Group all the selected nodes in an empty node.
    groupRoot = maxon.GraphNode()

    # To modify a graph, modification must be done inside a transaction. After
    # modifications are done, the transaction must be committed.
    with graph.BeginTransaction() as transaction:
        graph.MoveToGroup(groupRoot, maxon.Id("idOfMyGroup"), selectedNodes)
        transaction.Commit()

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()


if __name__ == "__main__":
    main()
