简体   繁体   中英

Export SCNScene as *.obj in SceneKit

We are trying create a simple polygon via scenekit based on the vertices in the sortedVerts array using the SCNShape init(path: UIBezierPath?, extrusionDepth: CGFloat)-Constructor. The exported.dae file is being constructed just fine, however.usd-files as well as.obj-files return empty. Whats the reason for that? We plan on importing the constructed.usd file back into realitykit following that step.

The code:

func drawSimplePolygon(from sortedVerts: [Experience.MesspunktSzene]) -> URL {
    
    let scene = SCNScene()
    let path = UIBezierPath()
    
    guard let initialPointX = sortedVerts.first?.position(relativeTo: nil).x else { return URL(string: "")! }
    guard let initialPointY = sortedVerts.first?.position(relativeTo: nil).z else { return URL(string: "")! }
    
    path.move(to: CGPoint(x: CGFloat(initialPointX), y: CGFloat(initialPointY)))
    
    for i in 1..<sortedVerts.count{
        let x = sortedVerts[i].position(relativeTo: nil).x
        let z = sortedVerts[i].position(relativeTo: nil).z
        path.addLine(to: CGPoint(x: CGFloat(x), y: CGFloat(z)))
    }
    path.addLine(to: CGPoint(x: CGFloat(initialPointX), y: CGFloat(initialPointY)))
    path.close()
    
    let shape = SCNShape(path: path, extrusionDepth: 0.02)
    
    let polygonNode = SCNNode(geometry: shape)
    polygonNode.geometry?.firstMaterial?.diffuse.contents = UIColor.green
    polygonNode.geometry?.firstMaterial?.lightingModel = .physicallyBased
    
    scene.rootNode.addChildNode(polygonNode)
    let url: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("test.obj")
    
    scene.write(to: url, delegate: nil)
    
    return url
}

Thanks

Maybe this will help to give you an approach. It's for export of SCNNodes as.obj File.

You will need to import SceneKit.ModelIO - never tested this on the rootNode. Good luck

// Get the Geometry to export, do this using a Guard, and return if nil

guard let geometry = yourNode?.geometry else { return }

let fixedFilenameOBJ = String("MyGeometryObject.obj")
let fixedFilenameMTL = String("MyGeometryObject.mtl")

let fullPathOBJ = getDocumentsDirectory().appendingPathComponent(fixedFilenameOBJ) // for the OBJ file
let fullPathMTL = getDocumentsDirectory().appendingPathComponent(fixedFilenameMTL) // for the MTL file

let mesh = MDLMesh(scnGeometry: geometry)
let asset = MDLAsset()
asset.add(mesh)

do {
    try asset.export(to: fullPathOBJ) // this will create two files: the obj + mtl
} catch {
    print("Couldn't create file(s)")
    return
}

Use this function in addition:

func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths.first! // paths[0]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM