Skip to content

Commit

Permalink
Separate USD init into Creator.main, misc. cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed May 2, 2024
1 parent acaacd4 commit 045ea68
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 128 deletions.
12 changes: 1 addition & 11 deletions Kraken.usda
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
#usda 1.0
(
doc = "Kraken v1.0.7 | PixarUSD v23.11.35"
doc = "Kraken v1.0.7 | 05-02-2024 07:06:13"
)

def Xform "Hello"
{
def Xform "Metaverse"
{
def Cylinder "Kraken"
{
}
}
}

8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "86ca3573181d97fb51f834cb8c76d8139ea4570c71591717fff34d96048696a1",
"originHash" : "ce0db44958ff8591b4c42ae4f857d67915b3d81f770a691d427294c14393afa7",
"pins" : [
{
"identity" : "aexml",
Expand Down Expand Up @@ -238,10 +238,10 @@
{
"identity" : "swiftusd",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/SwiftUSD.git",
"location" : "https://github.com/wabiverse/SwiftUSD",
"state" : {
"revision" : "e36e2d011a2c8bcc0b8863a3c80757c176017202",
"version" : "23.11.35"
"branch" : "main",
"revision" : "0e1a99f8640de43dd2a657365f72e1bb4ab61b21"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let package = Package(

// --- 🦄 Package Dependencies. ---
dependencies: [
.package(url: "https://github.com/wabiverse/SwiftUSD.git", from: "23.11.35"),
.package(url: "https://github.com/wabiverse/SwiftUSD", branch: "main"),
.package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"),
.package(url: "https://github.com/ChimeHQ/TextFormation", from: "0.8.2"),
.package(url: "https://github.com/ChimeHQ/TextStory.git", from: "0.8.0"),
Expand Down
73 changes: 56 additions & 17 deletions Sources/Kraken/IO/Hydra/KIO.StageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import CxxStdlib
import Foundation
import PixarUSD
import SwiftUI

public extension Kraken.IO
{
Expand All @@ -38,12 +39,17 @@ public extension Kraken.IO
/** The shared instance of the stage manager singleton. */
public static let manager = Stage()

private init()
{}

/** The underlying file manager for file ops. */
private let fileManager = FileManager.default

/** The date formatter for timestamp metadata. */
public let formatter = DateFormatter()

private init()
{
formatter.dateFormat = "MM-dd-yyyy HH:mm:ss"
}

/**
* Validates the usd file at the given path.
*
Expand All @@ -56,7 +62,13 @@ public extension Kraken.IO
@discardableResult
public func validate(url fileURL: URL?) -> String
{
let filePath = fileURL?.absoluteString
var isBinary = false
if Kraken.IO.USD.blacklistedFileExts.contains(fileURL?.pathExtension ?? "")
{
isBinary = true
}

let filePath = fileURL?.path
?? "\(Bundle.main.resourcePath ?? ".")/Untitled.usda"

guard
Expand All @@ -67,13 +79,16 @@ public extension Kraken.IO
{
try? fileManager.removeItem(atPath: filePath)

fileManager.createFile(
atPath: filePath,
contents: (try? String(
contentsOfFile: filePath
).data(using: .utf8)) ?? "".data(using: .utf8),
attributes: [.posixPermissions: 0o777]
)
if !isBinary
{
fileManager.createFile(
atPath: filePath,
contents: (try? String(
contentsOfFile: filePath
).data(using: .utf8)) ?? "".data(using: .utf8),
attributes: [.posixPermissions: 0o777]
)
}

return filePath
}
Expand Down Expand Up @@ -113,15 +128,39 @@ public extension Kraken.IO
stage.save()
}

func save(_ stage: inout UsdStageRefPtr)
/**
* Saves usd file and sets timestamp metadata.
*
* - Parameters:
* - stage: The stage to save. */
public func save(_ stage: inout UsdStageRefPtr)
{
let formatter = DateFormatter()
formatter.dateFormat = "MM-dd-yyyy HH:mm:ss"
let date = formatter.string(from: Date())
/* set the timestamp metadata. */
let metadata = "Kraken v\(Kraken.version) | \(timestamp())"

/* set the timestamp metadata & save. */
stage.getPseudoRoot().set(doc: "\(date) | Kraken v\(Kraken.version)")
/* set the metadata on the stage. */
stage.getPseudoRoot().set(doc: metadata)
stage.save()
}

private func timestamp() -> String
{
formatter.string(from: Date())
}

/**
* Checks if the file is a binary usd file.
*
* Currently checks if the file extension is
* a (.usda) file extension, if it is any other
* extension, or none, it is considered binary.
*
* - Parameters:
* - file: The file configuration to check.
* - Returns: Whether the file is binary or not. */
public static func isBinary(_ file: FileDocumentConfiguration<Kraken.IO.USD>) -> Bool
{
file.fileURL?.pathExtension != "usda"
}
}
}
9 changes: 9 additions & 0 deletions Sources/Kraken/IO/Universal/KIO.USD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public extension Kraken.IO
]
}

public static var blacklistedFileExts: [String]
{
[
"usd",
"usdc",
"usdz"
]
}

public init(configuration: ReadConfiguration) throws
{
guard let data = configuration.file.regularFileContents,
Expand Down
72 changes: 14 additions & 58 deletions Sources/Kraken/KR/KR.Creator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,33 @@
* . x x x . o o o . x x x . : : : . o x o . : : : .
* -------------------------------------------------------------- */

import CxxStdlib
import Foundation
import KrakenKit
import KrakenLib
import PixarUSD
import SceneKit
#if canImport(PyBundle)
import PyBundle
import Python
#endif /* canImport(PyBundle) */
import SwiftUI
#if canImport(GtkBackend)
import GtkBackend
#endif /* canImport(GtkBackend) */

/**
* this is the main entry point for Kraken,
* it initializes the usd plugins, any/all
* resources, and python, which is required
* for the application to run. */
@main
public struct Kraken: SwiftUI.App
public enum Creator
{
#if canImport(GtkBackend)
typealias Backend = GtkBackend
#endif /* canImport(GtkBackend) */

/** The bundle identifier for Kraken. */
public static let identifier = "foundation.wabi.Kraken"

/** The current version of Kraken. */
public static let version = ".".join(array: Pixar.GfVec3i(1, 0, 7))

/** Whether to show the splash screen. */
@State public var showSplash = true

public init()
static func main()
{
/* setup usd plugins & resources. */
Pixar.Bundler.shared.setup(.resources)

/* embed & init python. */
PyBundler.shared.pyInit()
PyBundler.shared.pyInfo()

Msg.logger.log(level: .info, "\("Kraken".magenta) \("v".yellow)\(Kraken.version.yellow) | \("PixarUSD".magenta) \("v".yellow)\(Pixar.version.yellow)")
Msg.logger.log(level: .info, "Kraken launched.")
}

/* --- xxx --- */
#if canImport(PyBundle)
/* embed & init python. */
PyBundler.shared.pyInit()
PyBundler.shared.pyInfo()
#endif /* canImport(PyBundle) */

public var body: some SwiftUI.Scene
{
DocumentGroup(newDocument: Kraken.IO.USD())
{ stage in
HStack
{
Kraken.UI.CodeEditor(
document: stage.$document,
fileURL: stage.fileURL
)

if let scene = stage.fileURL
{
SceneView(
scene: try? SCNScene(url: scene),
options: [.allowsCameraControl, .autoenablesDefaultLighting]
)
}
else
{
Text("Create or open a USD file.")
}
}
}
/* kraken main entry point. */
Kraken.main()
}

/* --- xxx --- */
}
99 changes: 99 additions & 0 deletions Sources/Kraken/KR/KR.Kraken.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* --------------------------------------------------------------
* :: : K R A K E N : ::
* --------------------------------------------------------------
* @wabistudios :: metaverse :: kraken
*
* This program is free software; you can redistribute it, and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Check out
* the GNU General Public License for more details.
*
* You should have received a copy for this software license, the
* GNU General Public License along with this program; or, if not
* write to the Free Software Foundation, Inc., to the address of
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (C) 2023 Wabi Foundation.
* All Rights Reserved.
* --------------------------------------------------------------
* . x x x . o o o . x x x . : : : . o x o . : : : .
* -------------------------------------------------------------- */

import CxxStdlib
import Foundation
import KrakenKit
import KrakenLib
import PixarUSD
import SceneKit
import SwiftUI
#if canImport(GtkBackend)
import GtkBackend
#endif /* canImport(GtkBackend) */

public struct Kraken: SwiftUI.App
{
#if canImport(GtkBackend)
typealias Backend = GtkBackend
#endif /* canImport(GtkBackend) */

/* --- xxx --- */

/** The bundle identifier for Kraken. */
public static let identifier = "foundation.wabi.Kraken"

/** The current version of Kraken. */
public static let version = ".".join(array: Pixar.GfVec3i(1, 0, 7))

/* --- xxx --- */

/** Whether to show the splash screen. */
@State public var showSplash = true

/** The currently opened stage. */
@State private var stage: UsdStageRefPtr = Usd.Stage.createNew("Kraken", ext: .usda)

/* --- xxx --- */

public init()
{
Kraken.IO.Stage.manager.save(&stage)

Msg.logger.log(level: .info, "\("Kraken".magenta) \("v".yellow)\(Kraken.version.yellow) | \("PixarUSD".magenta) \("v".yellow)\(Pixar.version.yellow)")
Msg.logger.log(level: .info, "Kraken launched.")
}

/* --- xxx --- */

public var body: some SwiftUI.Scene
{
DocumentGroup(newDocument: Kraken.IO.USD())
{ usdFile in
HStack
{
Kraken.UI.CodeEditor(
document: usdFile.$document,
isBinary: Kraken.IO.Stage.isBinary(usdFile)
)

if let scene = usdFile.fileURL
{
SceneView(
scene: try? SCNScene(url: scene),
options: [.allowsCameraControl, .autoenablesDefaultLighting]
)
}
else
{
Text("Create or open a USD file.")
}
}
}
}

/* --- xxx --- */
}
Loading

0 comments on commit 045ea68

Please sign in to comment.