Skip to content

selimanac/defold-blender-convex-hull

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published