Convex hull data from Blender

Creating Convex Hull in Blender

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 blender folder of the repo.
Defold project contains example Convex hull shapes.

Create Convex Hull in Blender:

  • Load your model and prepare it for importing Defold.
  • 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.

+Y Up

  • Select the model/object and switch to Edit Mode (Tab).

Edit Mode

  • Select all vertices (A).

Select all vertices

  • Use the “Convex Hull” operation (press F3, search for “Convex Hull”) or select “Mesh” → “Convex Hull” from menu.

Convex Hull

Export Convex Hull Points Using Python Script:

  • Go to the Scripting tab or open a new Text Editor in Blender.

Text Editor

  • 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.

Run Scrip

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}")

14 Likes

Thank you. I’ll try this when I get back next week.

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 script doesn’t create convex shape . Blender does with its own algorithm. Script just gets the data and export it as Defold compatible file.

This is what convex hull is!?

Then it is a single mesh

I don’t know. convex hull cant handle holes etc… you have to separate the convex hull or mesh for that.

2 Likes