Two mesh split scripts

I’ve recently been thinking of making a race track for a computer game (see this thread). My preferred modelling method is a plane with an array modifier and a curve modifier.

In Blender I’d like to keep the whole track as one object to make editing easier (e.g. so I don’t have to worry about the distance when using proportional editing), but the for game it’s necessary to split it up into multiple segments. I decided to have a go at scripting.

Script 1: Split by Count (splits every 3000 polys). This doesn’t require any setup but the result can be a bit messy if lots of points have been added/removed and faces not sorted.

import bpy
import bmesh

thing = bpy.context.active_object
#If the first modifer is an Array modifier, apply it to create a real object.
mods = thing.modifiers
if(mods[0].name == "Array"):
    bpy.ops.object.modifier_apply(modifier="Array")
thing.select = True
bpy.ops.object.mode_set(mode='EDIT') #Switch to edit mode

#Select all faces, sort them by X axis, then deselect all.
bpy.ops.mesh.select_all(action='DESELECT')

#credit: http://blender.stackexchange.com/questions/28934/select-neighbour-faces-from-a-mesh-with-python
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

#Split object be every 3000 faces until there are fewer than 3000 remaining.
while(len(bm.faces) > 3000):
    i = 0
    bm.faces.ensure_lookup_table() #reqiured due to accessing faces by index
    while(i < 3000):
        face = bm.faces[i]
        face.select = True
        i += 1
    
    #Separate the faces to a new mesh
    bpy.ops.mesh.separate(type='SELECTED')
    i = 0
       
#Switch back to object mode
bpy.ops.object.mode_set(mode='OBJECT')

Script 2: Split by Vertex Groups. This requires more setup work (creating and assigning) but you have more control over where the splitting occurs. This also doesn’t really work with the Array modifier still applied, but once a track gains unique features like banking and bumps etc, an Array modifier is no longer relevant.

import bpy

bpy.ops.object.mode_set(mode='EDIT') #Switch to edit mode
bpy.ops.mesh.select_all(action='DESELECT') #Clear any existing selection

obj = bpy.context.edit_object

for group in obj.vertex_groups:
    bpy.ops.object.vertex_group_set_active(group=str(group.name))
    bpy.ops.object.vertex_group_select()
    #Separate the faces to a new mesh
    bpy.ops.mesh.separate(type='SELECTED')
    
#Switch back to object mode
bpy.ops.object.mode_set(mode='OBJECT')

I hope someone finds these useful. They’ll certainly make it easier for me because there will be a lot of trial and error, so being able to easily switch between a full track and a split track will make things a lot simpler.

thanks, not sure i’ll get time to test, but looks useful & thanks for sharing.

I combined the two into a single addon called MeshSplit:
[ATTACH]467404[/ATTACH]

The user can change the max number of polys. Splitting by vertex group now looks for names that start with something. By default it’s “split…”, and the user can change that.

Here’s version 1.1

The created objects had names like thing.001, thing.002. The update replaces the . with _, which is useful for my use of this script.


I can’t get it to work. Any chance for a fix?
This sounds like a tool I could need.

The error suggests the code is expecting at least one modifier.
Here is hopefully a tweak that checks if there are any modifiers before trying to access the first one:
MeshSplit1.1.1.zip (1.8 KB)

Do bear in mind that it’s been a few years since I wrote that, and it was written for v2.79, which I’m not using since 2.8x is the current version, so I haven’t tested it and there might be something I’ve overlooked. In other words, cross your fingers before running it.