Swift-SceneKit-无法从'art.scnassets'加载'.scn'文件

3
我正在尝试从'diceCollada.scn'文件创建一个新的SCNScene。但是这个文件无法加载。 enter image description here 这个文件在“ARDicee/art.assets”文件夹中。
不仅是"diceCollada.scn",连默认的"ship.scn"也无法加载。我不知道为什么无法加载文件。
以下是我的代码。

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 - 版本14.1

macOS Ventura - 版本13.0.1

GitHub - 这个项目


我还尝试了另一种创建SCNScene的方法。

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)
        }
    }

但是它出现了这个错误。

错误域=NSCocoaErrorDomain Code=260 "无法打开文件“diceCollada.scn”,因为没有这样的文件。" UserInfo={NSFilePath=/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn, NSUnderlyingError=0x282924570 {Error Domain=NSPOSIXErrorDomain Code=2 "没有这样的文件或目录"}}


我正在尝试从'diceCollada.scn'文件创建一个新的SCNScene

3个回答

3

如果应用程序无法从苹果模板中加载飞船,则项目可能已经出现故障。尝试创建一个全新的,默认的SceneKit / ARKit项目,直接编译并检查飞船是否正确加载。如果是,请将代码从当前项目复制并粘贴到新项目中。如果即使从新模板中也无法加载飞船,则您的xCode安装可能会出现问题。您还可以清除项目构建文件夹或删除派生数据,这里有相关文章介绍如何进行此类操作。另外,您可以在StackOverflow上共享您的项目,以便我们可以查看它。


谢谢你的回答。 我创建了一个全新的SceneKit项目。但是我仍然遇到同样的问题。 我的Xcode无法识别“ship.scn”文件。 - ADAM _YONEDA
1
我的问题通过安装命令工具10以上版本和清理构建项目得到解决。感谢你的回答ZAY! - Marfin. F

3

重置命令行工具路径

您的命令行工具存在一些问题。因此,art.scnassets文件夹中的内容无法读取。为了解决这个问题,您需要安装Xcode 14.1的最新版本Command_Line_Tools,然后在终端中执行以下命令:

sudo xcode-select --reset

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

enter image description here

重新安装命令行工具

或者您可以使用终端删除旧版本的命令行工具,并安装新版本:

sudo rm -rf /Library/Developer/CommandLineTools

sudo xcode-select --install

然后重新启动您的Mac。
我也遇到了同样的问题,这些步骤对我有帮助。
重命名
如果以上步骤仍不能解决问题,请将art.scnassets文件夹重命名为artisan.scnassets

2
谢谢你的回答。我尝试了这个过程。我将“art.scnassets”文件夹重命名为“artisan.scnassets”,我的问题得到了解决。 - ADAM _YONEDA
1
恭喜Andy - 你是最棒的 - ZAY

3
字面上花了一整天的时间尝试修复这个问题,做了所有上面提到的事情 - 以及我在网上找到的其他任何东西 - 重新安装XCode、命令行工具等等都没有用 - 但是下面的方法对我有效,所以我发帖分享,以防止其他人像我一样浪费数小时:
  1. 将art.scnassets文件夹的内容复制到硬盘上的一个安全位置(例如桌面上的新文件夹) - 但不包括“art.scnassets”文件夹,只包括它下面的所有内容。

  2. 创建一个新的“art.scnassets”文件夹(Xcode菜单 文件>新建文件>Scenekit目录>“art.scnassets”)。

  3. 将来自安全硬盘位置的内容拖放(复制)到其中,然后它就可以工作了。

因此,由于某种原因,文件夹本身已经损坏 - 出于未知的原因,在该应用程序上运行了2年 - 重命名它或操作其内容都没有用,而重新创建一个新的解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接