14wml
14wml

Reputation: 4166

Is it possible to programmatically download a .scn file and run it?

Basically I was wondering if it was possible to host a .scn file on the web, download it, and render it as a SCNNode and put it in a sceneView.

And if so, what frameworks & functions would I need to call to do this

Upvotes: 1

Views: 1078

Answers (1)

M. Bedi
M. Bedi

Reputation: 1086

I did something like this on a project last year objective-C. I had a zip file containing the SCN file & other resources. I used AFNetworkng to download the zip file & then I de-compressed it to the app's documents folder using SSZipArchive.

In a JSON file, I had the shape ID for the node I wanted to display.

I constructed a path to the SCN file resource. I removed some code for brevity.

SCNSceneSource *sceneSource = [SCNSceneSource sceneSourceWithURL:documentsDirectoryURL options:nil]; 

newShapeNode = [sceneSource entryWithIdentifier:shapeID withClass:[SCNNode class]];

[parentNode addChildNode:newShapeNode];

Here is the download method - edited.

- (void) fetchRelease:(Album *) album usingProgressIndicator:(UIProgressView *) progressView completionHandler:(void(^)(NSError *error))completionHandler {
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"XXXXXXXX" password:@"XXXXXXX"];

    NSURL *URL = [NSURL URLWithString:album.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        return [self.documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
#ifdef DEBUG
            NSLog(@"File downloaded to: %@", filePath);
#endif
            NSError *error;
            [SSZipArchive unzipFileAtPath:filePath.path toDestination:self.documentsDirectoryURL.path overwrite:YES password:nil error:&error];
            if (error != nil) {
                completionHandler(error);
            } else {
                [self updateAlbumRelease:album];  // update the data model
                completionHandler(nil);
            }
        }
    }];

    if (progressView != nil) {
        [progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
    }
    [downloadTask resume];
}

And here is the edited pod file:

platform :ios, '9.1'

target 'SOMEAPPNAME' do
pod 'SVProgressHUD'
pod 'SSZipArchive'
pod 'AFNetworking', '~> 3.0'
end

Upvotes: 2

Related Questions