This small script is tested with Blender 4.1.1. It might not work any older versions.
You can find the example Blender project and Python Scripts in the blenderfolder of the repo.
Defold project contains example Convex hull shapes.
If you want to match the transform of your model and convex hull, disable the +Y Up option in the Transform section of the export window.
Select the model/object and switch to Edit Mode (Tab).
Select all vertices (A).
Use the “Convex Hull” operation (press F3, search for “Convex Hull”) or select “Mesh” → “Convex Hull” from menu.
Export Convex Hull Points Using Python Script:
Go to the Scripting tab or open a new Text Editor in Blender.
Copy and paste the script into the scripting editor.
Adjust the export_path variable to the desired file path where you want to save the exported convex hull data.
Ensure the correct object is selected.
Click the Run Script button in the scripting editor.
This should correctly export the convex hull data to the specified file.
import bpy
import bmesh
import os
def export_convex_hull_points(obj, filepath):
# Create a new bmesh and fill it with the object's mesh data
bm = bmesh.new()
bm.from_mesh(obj.data)
# Create the convex hull
bmesh.ops.convex_hull(bm, input=bm.verts)
# Extract vertices from the convex hull
vertices = [v.co.copy() for v in bm.verts]
# Write to file
with open(filepath, 'w') as f:
f.write(f"shape_type: TYPE_HULL\n")
for v in vertices:
f.write(f"data: {v.x}\n")
f.write(f"data: {v.y}\n")
f.write(f"data: {v.z}\n")
# Free the bmesh
bm.free()
# Ensure the correct object is selected
obj = bpy.context.object
# Specify the export path
# Default is .blender file's folder
export_path = os.path.join(bpy.path.abspath("//"), "file_name.convexshape")
# Export the convex hull points
export_convex_hull_points(obj, export_path)
print(f"Convex hull points exported to {export_path}")
Does this script allow to create convex shape which is precise to the initial mesh of the model? I see that the convexhull Blender creates, is a very rough low poly blob.
What if my model is a low-poly house of three floors, doorways and stairs, all as one mesh. Can it just wrap that house mesh with a simplified convex shape based on vertices?
This Blender script automates exporting selected objects to GLB format and generates corresponding Convex Hull shape files.
It is useful for game engines such as Defold, Unity, Godot, or any workflow where both mesh and convex collision data are required.
Tried using it to create collision for my complex scene (using primitive types would take too long) but I guess this is not the intended way of doing it?
Yes, if it is a single model, then Convex hull will not work. In this case, you need to use Concave hull, which, as far as I know, is not supported in Defold, but you can split Convex hull into parts using composite models.
Yes thats curently a hassle. Concave shapes are currently not supported unfortunately, so in complex shapes that are not convex, you would have to either subdivide mesh into convex parts or use convex decomposition algorithm to do so.
So currently after you choose either manual split or convex decomposition algorithm you can use libraries like dae2collision which is editor script that you can use to create collision shapes from. And then attach them to game objects.
But still its a work-around and would be awesome if Defold supported concave shapes out-of-the-box. As currently with the approach I described downside is you would need 1000s of separate collision objects, each with their own settings, which in complex levels becomes a mess to deal with
Thank you for the detailed answer. I guess I will stick to using primitive shapes for collisions
It is a bit disappointing tbh. I am creating a project for an extensive 3D tutorial on Defold and explaining that you have to use hundreds of primitive shapes for a large map (so it will have collisions) will not exactly make Defold appealing to new developers..
Hope concave shapes will be added soon as well as proper/full gltf support
How do you use the convex shapes after they are created? How is the implementation of it is done on Defold’s side? Still needs multiple collision shapes and placing on the correct world position?
As I said, I never used those. But I assume you’d end up with meshes that are already convex hulls in Blender.
The next step is to get the vertices of the hull and write the string for Defold’s .convexshape file.
For that you could use one one of the scripts presented above.
And regarding managing collision shapes yes that’s a headache, and concave shapes should solve it, best if we can have a feature of attaching a completely arbitrary shape to the CO or literally ability to infer from Gltf files. But one trick you can use for now is have an editor script that reads all decomposed meshes from the directory and write new collision objects into a new directory. Still messy but it can kinda work..
Hope amazing Defold team will have a chance too look into it soon, because that thing would be a game-changer. As right now most 3D examples are literally flat ground.