Error when mirroring newly created component: NONROOT_OCCURRENCE_PATH_ROOT

Error when mirroring newly created component: NONROOT_OCCURRENCE_PATH_ROOT

tiktuk
Advocate Advocate
220 Views
2 Replies
Message 1 of 3

Error when mirroring newly created component: NONROOT_OCCURRENCE_PATH_ROOT

tiktuk
Advocate
Advocate
Hi,
 
Has anyone ever seen this error before? No results for this error exist online as fast as I can tell.
 
I'm trying to mirror a newly created component.
 
Have not succeeded in recreating this problem in a sample script yet, sorry.
 
The error only happens if I have another component that the root component active in the ui.

 

 

 

File "...logic.py", line 424, in create_mirror_feature
mirror_feature = features.mirrorFeatures.add(mirror_feature_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Library/Application Support/Autodesk/webdeploy/pre-production/f20f20e2c96b4b67a6e9ce4db65bc41e8c4963b7/Autodesk Fusion.app/Contents/Api/Python/packages/adsk/fusion.py", line 36635, in add
return _fusion.MirrorFeatures_add(self, input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: 3 : MirrorComponent1 / Compute Failed // NONROOT_OCCURRENCE_PATH_ROOT - Occurrence Path must be from root component

 

 

 

 

 

 

def create_mirror_feature(
    entities: Union[adsk.core.ObjectCollection,
                    List[Union[adsk.fusion.BRepFace, adsk.fusion.Feature,
                               adsk.fusion.BRepBody, adsk.fusion.Occurrence]]],
    midplane: adsk.fusion.ConstructionPlane,
    name: str,
    parent_component: Optional[adsk.fusion.Component] = None,
    is_join: bool = True
) -> adsk.fusion.MirrorFeature:
    """
    Create a mirror feature.

    Args:
        entities (Union[adsk.core.ObjectCollection, List[Union[adsk.fusion.BRepFace, adsk.fusion.Feature, adsk.fusion.BRepBody, adsk.fusion.Occurrence]]]): The entities to mirror.
        midplane (adsk.fusion.ConstructionPlane): The midplane to mirror along.
        name (str): The name of the mirror feature.
        parent_component (adsk.fusion.Component, optional): The parent component where the mirror feature will be added. If None is passed, the root component is used.
        is_join (bool, optional): Determines if the mirror feature should combine the mirrored bodies with the original bodies. Defaults to True.

    Returns:
        adsk.fusion.MirrorFeature: The created mirror feature.
    """
    # Get the active design.
    design = adsk.core.Application.get().activeProduct

    # Use the root component if parent_component is None.
    if parent_component is None:
        parent_component = design.rootComponent

    features = parent_component.features

    # Check if entities is a list and convert to ObjectCollection if necessary
    if isinstance(entities, list):
        # Check if all entities are of the same type
        entity_types = {type(entity) for entity in entities}
        if len(entity_types) > 1:
            raise ValueError(
                f'All entities in a mirror feature must be of the same type. Found types: {", ".join([str(t) for t in entity_types])}'
            )

        entities = adsk.core.ObjectCollection.createWithArray(entities)

    mirror_feature_input = features.mirrorFeatures.createInput(
        entities, midplane
    )
    mirror_feature_input.isCombine = is_join  # This is how to make a join feature

    entity_to_mirror = entities.item(0)
    plane_parent = midplane.parent
    root_comp = design.rootComponent
    # Add debug information about component hierarchy
    entity_info = ""
    if isinstance(entity_to_mirror, adsk.fusion.Occurrence):
        entity_info = f"Component to Mirror: {entity_to_mirror.name} (Full path: {entity_to_mirror.fullPathName})"
    else:  # BRepBody
        entity_info = f"Body to Mirror: {entity_to_mirror.name} (Parent: {entity_to_mirror.parentComponent.name})"
    
    # Log information
    entity_type = "component occurrence" if isinstance(entity_to_mirror, adsk.fusion.Occurrence) else "body"
    ui.messageBox(
        f"Component Hierarchy Information:\n\n" +
        f"Root Component: {root_comp.name} (ID: {root_comp.id})\n" +
        f"{entity_info}\n" +
        f"Mirror Plane Parent: {plane_parent.name} (ID: {plane_parent.id})\n"
        f"Attempting to mirror {entity_type} {entity_to_mirror.name} using plane {midplane.name} from {plane_parent.name}.\n" +
        f"Mirror feature will be created in {parent_component.name}."
    )
    
    mirror_feature = features.mirrorFeatures.add(mirror_feature_input)
    mirror_feature.name = name
    return mirror_feature

 

 

 
Component Hierarchy Information logged before the error happens:

 

Root Component: (Unsaved) (ID: 74b03e4a-8a8a-44e1-a66d-49b97b263d12)

Component to Mirror: Drawer front:1 (Full path: Drawer front:1)

Mirror Plane Parent: Component2 (ID: 933468d0-7062-452e-8f3f-0e9286706aa2)

Attempting to mirror component occurrence Drawer front:1 using plane Width Midplane from Component2.

Mirror feature will be created in Component2.

 

0 Likes
221 Views
2 Replies
Replies (2)
Message 2 of 3

tiktuk
Advocate
Advocate

Ok, so I managed to create a script demonstrating the error. It's happening to newly created components.

 

So how do I make sure a new component has the full path?

 

# Author-
# Description-Mirror bodies using the XY origin plane

import adsk.core
import adsk.fusion
import traceback


def move_body_to_new_component(body, name, parent_component):
    """
    Create a new component and move the body to it.
    """
    # Create a new component for the board, under the parent component.
    occurrence = parent_component.occurrences.addNewComponent(adsk.core.Matrix3D.create())

    # Name the new component.
    occurrence.component.name = name

    # Move the new board body to its own component.
    body.moveToComponent(occurrence)

    return occurrence


def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Get the active design
        design = app.activeProduct
        root_comp = design.rootComponent

        # Get the current selection
        selection = ui.activeSelections

        if selection.count < 1:
            ui.messageBox(
                "Please select a body before running this script."
            )
            return

        # Get the selected body
        body_to_mirror = None

        for i in range(selection.count):
            entity = selection.item(i).entity
            if isinstance(entity, adsk.fusion.BRepBody):
                body_to_mirror = entity
                break

        # Check if we have a body
        if not body_to_mirror:
            ui.messageBox(
                "No body selected. Please select a body before running this script."
            )
            return

        # Get the parent component of the body
        parent_component = body_to_mirror.parentComponent
            
        # Use the XY origin plane as the mirror plane
        mirror_plane = parent_component.xYConstructionPlane
        
        # Get the parent component of the mirror plane
        plane_parent = parent_component

        # Add debug information about component hierarchy
        body_info = f"Body to Mirror: {body_to_mirror.name} (Parent: {body_to_mirror.parentComponent.name})"

        ui.messageBox(
            f"Component Hierarchy Information:\n\n"
            + f"Root Component: {root_comp.name} (ID: {root_comp.id})\n"
            + f"{body_info}\n"
            + f"Using XY origin plane from {plane_parent.name} as mirror plane\n"
        )

        # Always create the mirror feature in the active component
        mirror_feature_parent = design.activeComponent
        
        # Log information
        ui.messageBox(
            f"Attempting to mirror body {body_to_mirror.name} using the XY origin plane.\n" +
            f"Mirror feature will be created in the active component."
        )

        # Try to mirror the body
        try:
            # Try to recreate the error by creating a new component and mirroring it
            ui.messageBox("Creating a new component and mirroring it to demonstrate the error...")

            # Instead of copying, we'll temporarily move the body to a new component
            # First, remember the original parent component
            original_parent = body_to_mirror.parentComponent

            # Create a new component and move the body to it
            new_component = move_body_to_new_component(body_to_mirror, "Test Component", mirror_feature_parent)

            # Create a sub-component and move the body to it
            # We'll need to get the body from the new component first
            test_body = new_component.component.bRepBodies.item(0)

            # Move the body to a sub-component
            sub_component = move_body_to_new_component(test_body, "Sub Component", new_component.component)

            # Now try to mirror the sub-component occurrence
            ui.messageBox(f"Created component hierarchy: {new_component.name} > {sub_component.name}")
            ui.messageBox(f"Sub component full path: {sub_component.fullPathName}")

            # Try to mirror the sub-component occurrence
            sub_entities = adsk.core.ObjectCollection.create()
            sub_entities.add(sub_component)

            sub_mirror_input = mirror_feature_parent.features.mirrorFeatures.createInput(sub_entities, mirror_plane)

            try:
                sub_mirror_feature = mirror_feature_parent.features.mirrorFeatures.add(sub_mirror_input)
                ui.messageBox(f"SUCCESS: Mirrored sub-component successfully")
            except Exception as e:
                ui.messageBox(
                    f"ERROR when mirroring sub-component: {str(e)}\n\n"
                    + "This should recreate the error from makeDrawer function."
                )
        except Exception as e:
            ui.messageBox(
                "ERROR ENCOUNTERED:\n\n"
                + f"{str(e)}\n\n"
            )

    except:
        if ui:
            ui.messageBox("Failed:\n{}".format(traceback.format_exc()))
0 Likes
Message 3 of 3

tiktuk
Advocate
Advocate

Nobody has any idea? I feel like I must be missing something fundamental..

0 Likes