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.