Results 1 to 4 of 4
  1. #1

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



  2. #2
    Member Meta-Androcto's Avatar
    Join Date
    Aug 2006
    Location
    australia
    Posts
    3,990
    thanks, not sure i'll get time to test, but looks useful & thanks for sharing.



  3. #3
    I combined the two into a single addon called MeshSplit:
    Attachment 467404

    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.



  4. #4
    Here's version 1.1
    http://www.mediafire.com/file/kybyqs...shSplit1.1.zip

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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •