Search code examples
swiftscenekitscnscene

Swift-SceneKit-Can not load '.scn' file from 'art.scnassets'


I'm trying to create a new SCNScene from 'diceCollada.scn' file. But this file won't be loaded. enter image description here

This file is in "ARDicee/art.assets" folder.

Not only "diceCollada.scn", but also it cannot load the default "ship.scn". I don't know why it doesn't load files.


Here is my code.


import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {
    
    @IBOutlet var sceneView: ARSCNView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        
        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true
         
        // Create a new scene. ---------- The error is here ---------------
        guard let diceScene = SCNScene(named: "art.scnassets/diceCollada.scn") else {
            fatalError()
        }
        // Setting node
        if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: true) {
            diceNode.position = SCNVector3(x: 0, y: 0, z: -0.1)
            sceneView.scene.rootNode.addChildNode(diceNode)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if ARWorldTrackingConfiguration.isSupported {
            
            // Create a session configuration
            let configuration = ARWorldTrackingConfiguration()

            // Run the view's session
            sceneView.session.run(configuration)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // Pause the view's session
        sceneView.session.pause()
    }
}


Xcode - Version 14.1

macOS Ventura - Version 13.0.1

GitHub - This project


I also tried to create SCNScene another way.

override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        
        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true

        // --- Another way to create SCNScene ---
        let filePath = URL(fileURLWithPath: "/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn")
        do {
            let diceScene = try SCNScene(url: filePath)
            if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: true) {
                diceNode.position = SCNVector3(x: 0, y: 0, z: -0.1)
                sceneView.scene.rootNode.addChildNode(diceNode)
            }
        } catch {
            print(error)
        }
    }

But it gave this error.

Error Domain=NSCocoaErrorDomain Code=260 "The file “diceCollada.scn” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn, NSUnderlyingError=0x282924570 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}


I'm trying to create a new SCNScene from 'diceCollada.scn' file.


Solution

  • Resetting the path to command-line tools

    There are some issues in your command line tools. As a result, the content in the art.scnassets folder isn't readable. In order to fix this, you need to install the latest version of Command_Line_Tools for Xcode 14.1 and then execute the following commands in Terminal:

    sudo xcode-select --reset
    

    sudo xcode-select -switch /Library/Developer/CommandLineTools
    

    enter image description here

    Reinstalling command-line tools

    Or you can remove the old version of command line tools and install the new one using Terminal:

    sudo rm -rf /Library/Developer/CommandLineTools
    

    sudo xcode-select --install
    

    Then restart your Mac.

    I had the same problem and these steps helped me.

    Renaming

    If the above steps still do not help, rename the art.scnassets folder to artisan.scnassets.