Post

Replies

Boosts

Views

Activity

SCNGeometry and .copy()
Up to now I have created multiple new SCNNodes using an instance of SCNGeometry and it was OK that they all had the same appearance. Now I want variety and when I make a copy of that instance using: let newGeo = myGeoInstance.copy() as! SCNGeometry (must be force cast because copy() -> any?) all elements are verified present. :-) Likewise: node.geometry?.replaceMaterial(at: index, with: myNewMaterial) is verified to correctly change the material(s) at the correct index(s). The only problem is the modified "teapot" is not visible, and yes I have set node.isHidden = false. Has anyone experienced this? In the old days reversing the verts was a solution. In desperation I tried that. |-(
6
0
826
Dec ’24
Missing Fundamental something
Code to draw a graph of data. Each abscissa has an ordinate range to be displayed as a line segment. All data, i.e., scaled points are verified to be within the declared analysisView.bounds. strokeColors are verified within the range 0..1 BTW, no I don't need animation for this static data, but CALayer seemed to require more coding, and I found fewer code examples for it. The code below has two problems: 1) it doesn't draw into the window the weird behavior of min/max The first is why I am posting. What am I missing? import AppKit class AnalysisViewController: NSViewController { @IBOutlet var analysisView: NSView! var ranges = [ClosedRange<Double>]() var ordinateMinimum = CGFloat() var ordinateMaximum = CGFloat() var ordinateScale = CGFloat() let abscissaMinimum:CGFloat = 1 let abscissaMaximum:CGFloat = 92 let abscissaScale :CGFloat = 800/92 let shapeLayer = CAShapeLayer() var points = [CGPoint]() // created just to verify (in debugger area) that points are within analysisView.bounds func genrateGraph() { // ranges.append(0...0) // inexplicably FAILS! @ ordinateMinimum/ordinateMaximum if replaces "if N == 1" below // ranges.append(0.1...0.1) // non-zero range does not fail but becomes the min or max, therefore, not useful for N in 1...92 { if let element = loadFromJSON(N) { if N == 1 { ranges.append( element.someFunction() ) } // ranges[0] is an unused placeholder // if N == 1 { ranges.append(0...0) } // inexplicably FAILS! @ ordinateMinimum/ordinateMaximum if replacing above line ranges.append( element.someFunction() ) } else { ranges.append(0...0) } // some elements have no range data } ordinateMinimum = CGFloat(ranges.min(by: {$0 != 0...0 && $1 != 0...0 && $0.lowerBound < $1.lowerBound})!.lowerBound) ordinateMaximum = CGFloat(ranges.max(by: {$0 != 0...0 && $1 != 0...0 && $0.upperBound < $1.upperBound})!.upperBound) ordinateScale = analysisView.frame.height/(ordinateMaximum - ordinateMinimum) for range in 1..<ranges.count { shapeLayer.addSublayer(CALayer()) // sublayer each abscissa range so that .strokeColor can be assigned to each // shapeLayer.frame = CGRect(x: 0, y: 0, width: analysisView.frame.width, height: analysisView.frame.height) // might be unneccessary let path = CGMutablePath() // a new path for every sublayer, i.e., range that is displayed as line segment points.append(CGPoint(x: CGFloat(range)*abscissaScale, y: CGFloat(ranges[range].lowerBound)*ordinateScale)) path.move(to: points.last! ) points.append(CGPoint(x: CGFloat(range)*abscissaScale, y: CGFloat(ranges[range].upperBound)*ordinateScale)) path.addLine(to: points.last! ) path.closeSubpath() shapeLayer.path = path // shapeLayer.strokeColor = CGColor.white let r:CGFloat = 1.0/CGFloat(range) let g:CGFloat = 0.3/CGFloat(range) let b:CGFloat = 0.7/CGFloat(range) // print("range: \(range)\tr: \(r)\tg: \(g)\tb: \(b)") // just to verify 0...1 values shapeLayer.strokeColor = CGColor(srgbRed: r, green: g, blue: b, alpha: 1.0) } } override func viewDidLoad() { super.viewDidLoad() view.wantsLayer = true // one of these (view or analysisView) must be unneccessary view.frame = CGRect(x: 0, y: 0, width: 840, height: 640) analysisView.wantsLayer = true analysisView.frame = CGRect(x: 0, y: 0, width: 840, height: 640) genrateGraph() } }
7
0
1.3k
Oct ’21
NSOpenPanel not closing after OK button
I launch the following runSavePanel() from, e.g., @IBAction func JSONall(_ sender: NSButton) {         if !nsPanelActivatedThisSessionForSandbox {         nsPanelActivatedThisSessionForSandbox = true runSavePanel() } ... } where var nsPanelActivatedThisSessionForSandbox = false is declared as a global in the AppDelegate.swift file and is intended to prevent repeated user prompting as the program creates many files but needs sandbox acknowledgment at least once. The problem behavior is that upon clicking the OK button, the app merrily creates the files in the chosen directory but the modal window stays open until the app is done creating files (around ten minutes). Here is my runSavePanel() and its (empty) delegate:     func runSavePanel() {         let defaults = UserDefaults.standard         let savePanel = NSSavePanel()         savePanel.canCreateDirectories     = true         savePanel.canSelectHiddenExtension = false         savePanel.showsHiddenFiles         = false         //        savePanel.allowedFileTypes         = ["JSON", "csv"]                            //  deprecated         savePanel.directoryURL             = defaults.url(forKey: "fileDirectoryPref")         let delegate = OpenSavePanelDelegate()      //  cannot consolidate with next line         savePanel.delegate = delegate         let result = savePanel.runModal()         if result == NSApplication.ModalResponse.OK {             if savePanel.url != nil {                 do {                     try savePanel.delegate!.panel!(savePanel, validate: savePanel.url!)                     defaults.set(savePanel.url, forKey: "fileDirectoryPref")                     //        defaults.set(savePanel.url, forKey: "NSNavLastRootDirectory")   //  does not write                 } catch {                     print("\(result) ViewController Structures.swift:739 runSavePanel()")                 }             }         }     }          class OpenSavePanelDelegate: NSObject, NSOpenSavePanelDelegate {         func panel(_ sender: Any, validate url: URL) throws {             /*  In Swift, this method returns Void and is marked with the throws keyword to indicate that it throws an error in cases of failure.              You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.              */ //            throw CustomError.InvalidSelection         }     }      //    enum CustomError: LocalizedError { //        case InvalidSelection // //        var errorDescription: String? { //            get { //                return "\(CustomError.InvalidSelection)" //            } //        } //    } Idk how to stop the double spacing when pasting my code. Sorry. The delegate is a requirement but I don't know how to configure it. The app doesn't generate errors... or at least it allows the desired output. Is the delegate supposed to terminate the modal window somehow?
8
0
1.5k
Sep ’21
ProgressBar with DispatchQueue
I have searched but cannot find an example of this. In my example I use playground to nest for-loops in a function called from within DispatchQueue. I find many roadblocks: //import AppKit // playground doesn't recognize AppKit?! import Dispatch import Foundation var comboCount:NSKeyValueObservation? //class viewContr: NSViewController { @IBOutlet weak var progressBar: NSProgressIndicator! // deinit { comboCount?.invalidate() } @IBAction func calcStuff() { DispatchQueue.global(qos: .userInitiated).async { let combos = possibleCombos() DispatchQueue.main.async { for word in combos { word.print0b8() } } } } //} func possibleCombos() -> [Int8] { var combos = [Int8]() for a in [0, 1] { for b in [0, 1] { for c in [0, 1] { for d in [0, 1] { combos.append(Int8(d | (c << 1) | (b << 2) | (a << 3)) ) comboCount = combos.count/16 as! NSKeyValueObservation // known total combos = 16 // How do I pass combos.count out to a progress bar? } } } } return combos } extension Int8 {func print0b8() {print("0b" + pad(string: String(self, radix: 2), toSize: 4))}} func pad(string : String, toSize: Int) -> String {var padded = string;for _ in 0..<(toSize - string.count) {padded = "0" + padded};return padded} Removing the @IB obstacles and the attempted declaration of a ViewController does produce output, but can't be used with a progress bar, e.g., import Dispatch import Foundation DispatchQueue.global(qos: .userInitiated).async { let combos = possibleCombos() DispatchQueue.main.async { for word in combos { word.print0b8() } } } func possibleCombos() -> [Int8] { var combos = [Int8]() for a in [0, 1] { for b in [0, 1] { for c in [0, 1] { for d in [0, 1] { combos.append(Int8(d | (c << 1) | (b << 2) | (a << 3)) ) // known total combos = 16 // How do I pass combos.count out to a progress bar? } } } } return combos } extension Int8 {func print0b8() {print("0b" + pad(string: String(self, radix: 2), toSize: 4))}} func pad(string : String, toSize: Int) -> String {var padded = string;for _ in 0..<(toSize - string.count) {padded = "0" + padded};return padded} In my actual App, i.e., not this playground, I have a ProgressBar in my storyboard and an IBoutlet declared in my view controller. I just can't find how to pass a value out of "possibleCombos()" and into the NSProgressIndicator. What I have read suggests a delegate but reading about delegates hasn't helped. I need an example. Thanks.
8
0
2.1k
Nov ’21
SceneView selective draw since concurrency
I have used SceneKit for several years but recently have a problem where a scene with fewer than 50 nodes is partially drawn, i.e., some nodes are, some aren't, and greater than 50 nodes are always draw correctly. This seems to have happened since concurrency was introduced. (w.r.t. concurrency, I had been using DispatchQueue successfully before then.) Since all nodes (few or many) are constructed and implemented by the same functions etc. I'm baffled. When I print the node hierarchy all nodes are present whether few or many. SceneView() has [.rendersContinually] option selected. Every node created (few or many) has .opacity = 1.0, .isHidden = false I haven't tried setting-back the compiler version as that is not a long term solution, and I know the same code worked fine then.
8
0
747
Feb ’25
SecureCoding roadblock?
Attached is an entire project (4 files) that mirrors my actual project including the failure to save to file. Am I: missing some syntax in this code? failing to config a defaults file? not set the necessary parameters in " "Build Settings" or "Build Rules etc.? I was writing to JSON files, but now that I must append to files directly, and JSON doesn't do that easily, I am trying to write using native macOS tools. WELL, IT SEEMS I CAN'T SEND YOU THE CODE, TOO MANY CHARS. I CAN'T ATTACH ANY FILE EITHER. WHY OFFER IT IF IT IS NOT ALLOWED? ANYWAY, CAN YOU GLEAN ANYTHING FROM THIS... Thanks. My debugger area: 2022-05-28 12:03:11.827372-0500 exampleClassInClassSecureCoding[1508:29981] Metal API Validation Enabled 2022-05-28 12:03:11.940123-0500 exampleClassInClassSecureCoding[1508:29981] *** NSForwarding: warning: object 0x600003cf7090 of class 'exampleClassInClassSecureCoding.classOne' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[exampleClassInClassSecureCoding.classOne replacementObjectForKeyedArchiver:] 2022-05-28 12:03:11.940416-0500 exampleClassInClassSecureCoding[1508:29981] Unrecognized selector -[exampleClassInClassSecureCoding.classOne replacementObjectForKeyedArchiver:] Unrecognized selector -[exampleClassInClassSecureCoding.classOne replacementObjectForKeyedArchiver:] Performing @selector(didPressButton:) from sender _TtC7SwiftUIP33_9FEBA96B0BC70E1682E82D239F242E7319SwiftUIAppKitButton 0x7ff08ab06480
9
0
2k
Feb ’24
swift Process() return values
How do I access a returned value from a Process(), in this case 'which'... var sips_path : String? //MARK: locate sips on local machine let which_sips = Process() which_sips.executableURL = URL(fileURLWithPath: "which") which_sips.arguments = ["sips"] do { sips_path = try which_sips.run() } catch let error as NSError { sips_path = "/usr/bin/sips"; print("Failed to execute which_sips", error) }line 8. gets compiler error "Cannot assign value of type '()' to type 'String?'" I believe, but cannot prove, 'which' returns a string. .run() throws and throws are for errors only, right? So where is the result of calling which?It seems I should use a closure to use $0 but it's already in one...line 9. intends to assign a default path.
13
1
7.7k
Oct ’21