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

问题描述 投票:0回答:3

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

SCNScene
。 但是这个文件不会被加载。

此文件位于“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)
        }
    }

但是它给出了这个错误。

Error Domain=NSCocoaErrorDomain Code=260 “文件“diceCollada.scn”无法打开,因为没有这样的文件。” UserInfo={NSFilePath=/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn, NSUnderlyingError=0x282924570 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}


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

SCNScene

swift scenekit scnscene
3个回答
3
投票

如果应用程序无法从苹果模板加载宇宙飞船,项目可能会以某种方式被破坏。尝试创建一个全新的默认 SceneKit/ARKit 项目,直接编译它并检查飞船是否正确加载。如果是,请将当前项目中的代码复制并粘贴到新项目中。如果宇宙飞船甚至没有从新模板加载,您的 xCode 安装可能会被破坏。您还可以清理项目构建文件夹或删除派生数据,这里有关于如何执行此类操作的文章。此外,您可以在 StackOverflow 上分享您的项目,以便我们查看。


2
投票

重置命令行工具的路径

你的

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

sudo xcode-select --reset

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

重新安装命令行工具

或者您可以删除旧版本的

command line tools
并使用终端安装新版本:

sudo rm -rf /Library/Developer/CommandLineTools

sudo xcode-select --install

然后重新启动您的 Mac。

我遇到了同样的问题,这些步骤帮助了我。

重命名

如果上述步骤仍然没有帮助,请将

art.scnassets
文件夹重命名为
artisan.scnassets
.


0
投票

实际上浪费了一天的时间来尝试通过以上所有方式来解决这个问题——以及我在网上找到的任何其他东西——重新安装了 XCode、命令行工具等,但没有任何效果——但下面的方法对我有用,所以发帖以防有人浪费时间像我自己:

  1. 将 art.scnassets 文件夹的 contents 复制到硬盘驱动器上的安全位置(例如桌面上的新文件夹) - 但不包括“art.scnassets”文件夹,只是它下面的所有内容。

  2. Created a new "art.scnassets" folder (Xcode Menu File>New File>Scenekit Catalog> "art.scnassets").

  3. 从那里的安全硬盘驱动器位置拖放(复制)内容,它起作用了。

因此,由于某种原因,文件夹本身已损坏 - 由于未知原因,它在该应用程序上工作了 2 年 - 重命名或操纵其内容无济于事,重新创建一个新文件夹解决了它。

© www.soinside.com 2019 - 2024. All rights reserved.