Search code examples
androidarcoreandroid-sdk-manager

How to Remove 3d objects which are placed on AR Screen


I have an AR Screen where I placed some 3d objects. I am using google AR Core with android SDK.

I need to remove those objects from the scene on clicking of a clear button.

I was able to remove the last placed object. But the other objects were not removing.

This is the code i am using.

if (newAnchor != null) {
            arFragment.getArSceneView().getScene().removeChild(newAnchor);
            newAnchor.getAnchor().detach();

Solution

  • you can detach android model from scene form using below code

     List<Node> children = new ArrayList<>(arFragment.getArSceneView().getScene().getChildren());
            for (Node node : children) {
                if (node instanceof AnchorNode) {
                    if (((AnchorNode) node).getAnchor() != null) {
                        ((AnchorNode) node).getAnchor().detach();
                    }
                }
                if (!(node instanceof Camera) && !(node instanceof Sun)) {
                    node.setParent(null);
                }
            }