Use Poly API for ARKit

Hi! I’m Kakkun a.k.a. fromKK working as a iOS engineer in Timers Inc.
Apple announced ARKit in WWDC 2017, I’m heard that directly and so excited.
And, Google announced Poly in end of October, 2017. Also, Poly API announced by Google in end of November. I’m tried to use Poly API with ARKit.

Poly

Poly is service of free downloadable 3D data by Google.
Many creators upload 3D data, we can use them for free.
But, we must take care for license, that is mostly CC-BY license, we must show author name in display.

Poly API

Poly API is platform of downloadable 3D data with programming.
There are four features now, searching, get asset, get user posts, get user liked.
Searching and get asset feature is available with easy authentication, but get user posts and get user liked feature needs permission with OAuth.
There is a sample by Google, please check it also.

Usage

First, we must activate Poly API in page of console api in google.

Then we can get API Key in page of document.

API

Domain is https://poly.googleapis.com.

v1.assets

get asset: GET /v1/{name=assets/*}
list of asset: GET /v1/assets

We can easily use searching and get detail api, to set API Key after url.
e.x. https://poly.googleapis.com/v1/assets?key=YOUR_API_KEY

v1.users.assets

users assets: GET /v1/{name=users/*}/assets

Get list of user posted assets. But, this api needs permission with OAuth, so I can't try it in this time.

v1.users.likedassets

users liked assets: GET /v1/{name=users/*}/likedassets

Get list of users liked assets. But this api also needs permission with OAuth, so I can’t try it in this time.

Use data of Poly in ARKit/SceneKit

There are objects named formats in asset object.
We need to find format object of .obj in formats.
And there is a root object and resources objects in format object, that has URLs of .obj and .mtl, and that has image files like a JPG, PNG for texture.
We must download files from resource URLs to the same local directory. After downloaded all files, We can use as a SCNNode from URL of .obj file via MDLAsset.

import SceneKit
import SceneKit.ModelIO
import ModelIO
// create instance of MDLAsset
let mdlAsset = MDLAsset(url: objURL)
// load texture
mdlAsset.loadTextures()
// create instance of SCNNode
let node = SCNNode(mdlObject: mdlAsset.object(at: 0))

PolyKit

Now I tried to use Poly API, but the mapping models and download files was too complex. So I made a library for easy to use Poly API named PolyKit.
I supported Carthage, you can install to insert follow line in Cartfile.

github “fromkk/PolyKit”

After insert the line, execute carthage update --platform ios in Terminal.app and setup in Xcode.

Usage

Search feature
Use PolyAPI for search assets from Poly.

import PolyKit
let query = PolyAssetsQuery(keywords: “Cat”, format: Poly3DFormat.obj)
let polyApi = PolyAPI(apiKey: “Poly API Key is HERE!!!”)
polyApi.assets(with: query) { (result) in
switch result {
case .success(let assets):
self.dataSource.assets = assets.assets ?? []
case .failure(_):
self.showFetchFailedAlert()
}
}

Download files of .obj and .mtl

To download file of .obj, call downloadObj function in object of PolyAsset. After download, you can convert to SCNNode via MDLAsset.

import PolyKit
import SceneKit
import SceneKit.ModelIO
import ModelIOlet asset: PolyAsset = …
// Download obj and mtl files from Poly
asset.downloadObj { (result) in
switch result {
case .success(let localUrl):
let mdlAsset = MDLAsset(url: localUrl)
mdlAsset.loadTextures()
let node = SCNNode(mdlObject: mdlAsset.object(at: 0))
// do something with node
case .failure(let error):
debugPrint(#function, “error”, error)
}
}

Summary

PolyKit is a library which just wrapped Poly API, so became easier to use it. Especially, it was hard to use data of 3D as variables in apps. We can realize ideas too easy with Poly and ARKit and SceneKit, that get resolving our complex problems.
There are many 3D data in Poly and increasing now. Let’s try to use it, and make apps and games more and more!
If you favorite PolyKit, please push ⭐️ button in page of Github.

P.S.
I don't know how to use OAuth feature in Google, if you know that please teach me, or send Pull Request to PolyKit!

Japanese version is here!