glTF + Physics + Babylon

What is this?

I’d like to share a little project I’ve been working on lately with the Babylon community. It’s a Babylon plugin for loading glTF files with embedded physics information, making it possible to load up a glTF file and get simulation working without any code. It’s very much a beta, but I’ve been using it for months without [too many] problems.

I’ve got a demo app running with some sample assets on GitHub: linky

The plugin is available on NPM here and contains info on how to start using it in your project. Once the extension is loaded, you can load glTF+physics files as normal and stuff will start moving! Bug reports are welcome (bug fixes are even more welcome!)

Not only is there a Babylon plugin to load the physics, there’s also a Blender exporter, allowing physics properties to be configured in Blender and loaded straight into Babylon.

Little bit of extra background
There’s actually two glTF extensions added in the NPM package; one called “MSFT_rigid_bodies” and one called “KHR_rigid_bodies” - they both do pretty much the same thing at the moment. On the Havok team, we made the MSFT_rigid_bodies version a few years ago, while the KHR_rigid_bodies version is a version we (along with several others) want to get officially supported in glTF, so it’s in active development. I can promise we won’t make breaking changes to MSFT_rigid_bodies, but might do so for KHR_rigid_bodies.If you want a stable experience, I’d suggest using the “MSFT” version, while, if you want the bleeding edge, “KHR” is the way to go.

Sounds great! How do I get started?

Easy. Install the plugin:

npm install babylon-gltf-rigid-body-loader

And register it with Babylon:

import { GLTF2 } from "@babylonjs/loaders";
import { MSFT_RigidBodies_Plugin } from "babylon-gltf-rigid-body-loader";

GLTF2.GLTFLoader.RegisterExtension(
   "MSFT_rigid_bodies", function (loader) {
       return new MSFT_RigidBodies_Plugin(loader);
   });

That’s it! Then just load your glTF files!

You want to use Blender to export physics data? Also easy!

Download and unzip the Blender addon, then, in Blender, add the MSFT_rigid_bodies_file.py in the Addons settings. The README has some more info and pictures. This will get you the stable “MSFT” plugin version; if you want the latest-and-greatest, head on over to the main branch and get started (bug reports and bug fixes also welcome!)

16 Likes

Been stalking ur repo for those months :slight_smile:

cc @Lun
Rigid body go boin boin
.GitHub - Pauan/blender-rigid-body-bones: Blender Addon which adds rigid body / spring physics to bones

2 Likes

Video gif. A black dog stares wide-eyed as if in surprised shock.

It is happening !!!

1 Like

Boin boin!

1 Like

This now works in the playground as well. You will need to load the script and register the extensions until it’s finalized :slight_smile:

1 Like

Blender: 3.6
Addon: 0.0.2

Addon causes console error in headless (even though there is no physics in the scene). I did not export a GLTF file! The error does not seem to halt the script though.

//cmd call
call Blender.exe someBlendFile.blend --background --python somePythonScript.py
#python script that Blender runs in headless
import bpy
import sys
bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
// Error msg

...>node ./opForeachFileRunStaticScript/index.js "cliAdHoc.py .\+Out\GameHiPoly\"
Processing Orc_M_HiPoly__f373.blend 1 / 23
Traceback (most recent call last):
  File "\Blender Foundation\Blender 3.6.0\3.6\scripts\modules\addon_utils.py", line 333, in enable
    mod = __import__(module_name)
  File "C:\Users\AppData\Roaming\Blender Foundation\Blender\3.6\scripts\addons\KHR_rigid_bodies.py", line 702, in <module>
    viewportRenderHelper = KHRPhysicsSettingsViewportRenderHelper()
  File "C:\Users\AppData\Roaming\Blender Foundation\Blender\3.6\scripts\addons\KHR_rigid_bodies.py", line 541, in __init__
    self.shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')

SystemError: GPU API is not available in background mode
´´´

Ok, I see. It is just the KHRPhysicsSettingsViewportRenderHelper. If you go to here https://github.com/eoineoineoin/glTF_Physics_Blender_Exporter/blob/15d9f5a7259e394c0f8b6697917a2ee54ce93d15/addons/KHR_rigid_bodies.py#L541 and comment this line, there is no error in headless.

Would it suffice to add a check for headless and if so, just do not assign “self.shader”?

1 Like

Nice catch, thanks! I’ll get this fixed in the next update.

I tried that Babylon.js Playground, but the physics didn’t work. Am I missing something?

The name of the extension is registered as KHR_rigid_bodies, but the glTF has KHR_physics_rigid_bodies defined. @eoin - should I update the CDN with a new version?

Hey Raanan, yes, sorry, the extension was renamed. If we could update the CDN version and the playground, it would be super. Is it possible for me to do that, or do you have to hit a button somewhere?

since this is not part of our official release process it has to be done manually. Send me the file, i’ll update the CDN

2 Likes