3D Animation for developers

The Blender to Xcode workflow

SceneKit is Apple’s high-level 3D API that first appeared on OS X Mountain Lion and made the jump to iPhone and iPad (and now Apple TV) from iOS 8. After having spent a lot of time with Banjax working on lighting shaders and implementing a very basic 2.5D physics engine, it’s something of a relief to use an API that has 3D physics built in and allows complex lighting effects to be achieved with just a line of code. It’s great that it can automatically implement an OpenGLES or Metal rendering engine depending on the hardware and OS its running on. It’s also really exciting to use an API that has supported for animating models with a rig/ armature. But how quickly could I get a rigged character with a run-cycle animation out of Blender and into Xcode and SceneKit?

The assets in the example SceneKit projects that Apple provides are made with AutoDesk products like Maya and 3ds Max, which, at £145 per month for a 3ds Max subscription, are perhaps beyond the budget of indie developers (at least the ones who don’t qualify for the free educational subscriptions). According to comments on forums, the workflow from Maya to Xcode is relatively painless. How well would the pipeline work for Blender, arguably the modeller of choice for zero-budget designers and devs? There are two sets of conversion needed for this kind of pipeline, from a Blender .blend file to a Collada .dae file (digital asset exchange), which we’re using as an intermediary format, and then from the .dae to Xcode’s SceneKit .scn format. At each end of the pipeline then, there is potential for things to break. How much would get lost in these translations?

Blender workflows

In this post I thought I’d share my first thoughts on the Blender to Xcode/ SceneKit workflow, with a focus on working with models in Blender that have rigs and animation, with .dae format as the intermediary. This post assumes a working knowledge of Blender. There are tonnes of online tutorials and videos for the Blender processes described here so I won’t go into too much detail describing them.

Export often

One thing working in our favour, is that .dae support is built right in to to OS X. So you can see the mesh animating right in the Finder’s preview window. You can even pan (with two-finger swipes) and rotate (click-and-drag) around the model, right in the Finder preview pane. If you want to get a closer look, you can use QuickLook or open it with Preview. This means that as soon as you hit the export button in Blender, you get a good idea of whether the animation has been exported correctly, or whether something has gone awry, without even having to bring the file into Xcode. This is immensely helpful, as you can frequently export and re-export as you work on your model in Blender, to check that you haven’t broken something. Far better to do this then to spend hours building a complex model, only to find that OS X/ Xcode doesn’t like it.

Some tips for rigging

  1. If your armature is symmetrical, turn on armature x-mirroring as you build it. As well as automatically adding the correct _L or _R suffix to the bone names, it assures the bones are symmetrical (obviously), and also have the right roll values. This is important for pose mirroring, so that if you’re animating a walk or a run cycle, you only have to animate the left or right stride, and then mirror the pose to get the opposite stride in the cycle.

  2. Do not tick don't inherit rotation for any of your bones. This setting is ignored by Blender’s .dae export, and the exported bone will in fact inherit its parents’ rotations. don't inherit rotation can be useful, for example, if you’re animating a walk cycle, where you want the shoulders to swing left to right, but you want the head to stay looking ahead, you might select the neck bone, and check don't inherit rotation. Instead of doing this, manually rotate the neck bone in the opposite direction to the shoulders to cancel out their rotation.

The modifier stack

When creating models in Blender, you often have a set of modifiers that you want to keep in the modifier stack without applying them. For instance, if you’re creating a symmetrical figure (such as a humanoid character), an x-axis mirror modifier greatly speeds up the modelling process because you only have to model one half of the model. Or, a subdivision surface modifier allows you to create complex curving surfaces out of much simpler geometries. By setting the subdivision depth variable you can control how much extra geometry you want to create to smooth out the surfaces, how low- or high-poly you want the model to be. By leaving the subdivision surface modifier in the stack and not applying it, you can defer this decision, about how detailed and complex you want your geometry to be, indefinitely. In testing you might realise that the model appears too blocky and lo-poly, or (most likely) that it is too distant/ fast-moving/ unimportant for the extra geometry to be worthwhile.

Typically, you do want these modifiers to be applied on export. Otherwise, your mirrored humanoid will be missing the left half of its body, and all that lovely subdivided geometry won’t be present.1 Luckily, all the Blender exporters have a handy apply modifiers check-box, and certainly with non-animated models you’d usually want to have this box checked. With models with armatures however, the situation is more complex, as the armature is one of the modifiers in the stack. As far as I can tell, the rule is do not apply the armature modifier. If you apply it, the animation gets messed up.2 This means, unfortunately, that you can’t use the handy apply modifiers check-box. So what do you do about the other modifiers that you do want to be applied for the export? It’s not an ideal solution, but my workaround is this:

  1. Duplicate the Blender file, adding an “-export” suffix or whatever for clarity
  2. Manually apply all modifiers in the stack except for the armature
  3. Export to .dae with apply modifiers in the export pane unchecked
  4. Probably best to delete the “-export” Blender file, to avoid confusion

Of course, once your model starts to get finalised, you might want to add some non-symmetrical detail or to further sculpt a subdivided mesh, so you’d apply the mirror and subdivide modifiers. At the point where the armature is the only modifier left in the stack, you don’t need to follow the above 4 steps, you can just export to .dae directly. Just make sure that when you do, apply modifiers in the export pane is unchecked.

Save export presets so that you can recreate your workflow

All the Blender exporters allow you to save sets of presets for your various workflows. To save a new set of presets, press the + next to Operator presets at the top of the export pane. Save an Xcode armature preset which has apply modifiers unchecked.

Handling multiple actions

Typically, a character in a game will have a number of animation cycles. A rest cycle for when they’re standing around, a walk cycle, run cycle, jump cycle etc. As I covered in a previous post, Blender has an action editor for saving multiple actions. Just remember to hit that F button to save each action!

When you export to .dae, only the currently linked action is exported as an animation. If the model has multiple animations then, you can export an additional .dae file, one for each action, which only contains the armature and the animation, not any of the geometry, the skinner, or the bone weights. This modular approach has its advantages, as it means you can tweak one of the animations, and just re-export that animation,without having to re-export the geometry. Or vice-versa, you can modify the geometry without having to re-export all the animations.

To export a .dae that only contains the armature and the currently connected animation:

  1. Select the action you want to export in the action editor, and make sure it is linked to the geometry by pressing play. You make need to select the armature and switch into pose mode for the action to become linked.

  2. Switch to object mode and make sure that only the armature and nothing else is selected (hit a a couple of times to select all and then clear any selection if you’re unsure). If you can’t see the armature, either switch from solid into wireframe display or turn on x-ray in the armature pane.

  3. With just the armature selected, hit File > Export > Collada (Default) .dae.

  4. In the export pane check selection only. As ever, apply modifiers should be unchecked.

This should result in a much more compact .dae that just contains the armature and the animation. If you select this file in the Finder, you won’t be able to see anything in the preview pane, because this file doesn’t contain any geometry.

Coming up in the next post

Up next, working with these animations in Xcode with Swift and SceneKit.

  1. Although, SCNGeometry can subdivide the geometry for you at render time, on the fly. I haven’t worked out what kind of performance hit this incurs.

  2. I’m by no means a Blender expert, and this could be a symptom of me rigging my model incorrectly.

Built with Jekyll      © Salt Pig Media