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") = True
bpy.ops.object.mode_set(mode='EDIT') #Switch to edit mode

#Select all faces, sort them by X axis, then deselect all.

obj = bpy.context.edit_object
me =
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] = True
        i += 1
    #Separate the faces to a new mesh
    i = 0
#Switch back to object mode

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:
    #Separate the faces to a new mesh
#Switch back to object mode

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:

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: (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.