Since it may be useful to someone, i post it now. This is wrote for my use, so many of the comments are my notes for me, or are related to old stuff or other things. But it should work. Try it out
bl_info = {
"name": "Mesh Separator",
"author": "Zoffoli Francesco (Makers_F)",
"version": (0,5),
"blender": (2, 5, 6),
"api": 35042,
"location": "View3D > Toolshelf ",
"description": "Subdivide the mesh in many tiles of given size." ,
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Object"}
import bpy
import os
from mathutils import Vector
from bpy.props import BoolProperty, StringProperty, FloatProperty
from math import pow
def SetProperties():
bpy.types.Scene.separator_square_size = FloatProperty(name="Square Side",
description="The lengh of the square side",
min = 0.01,
default = 9.0,
step = 100,
precision = 2)
bpy.types.Scene.delete_file_if_already_exists = BoolProperty(name="Override files if they exist",
description="Check if exixsts a file with the same name of the one that will be created and overwrite it if True",
default=False)
bpy.types.Scene.exported_obj_path = StringProperty(name="Export Path",
description="Set the path where all the exported .obj files will be placed",
subtype='DIR_PATH'
)
def FindLowHigVertex(nomeID):
low = bpy.data.objects[nomeID].data.vertices[0]
hig = bpy.data.objects[nomeID].data.vertices[1]
for v in bpy.data.objects[nomeID].data.vertices:
if v.co[0]<=low.co[0] and v.co[1]<=low.co[1]:
low = v
elif v.co[0]>=hig.co[0] and v.co[1]>=hig.co[1]:
hig = v
return low , hig
def get_vertex_list_python(nomeID, size):
low , hig = FindLowHigVertex(nomeID)
for v in bpy.data.objects[nomeID].data.vertices:
if v.co[0]-low.co[0]<=size and -size/100<=v.co[1]-low.co[1]<=size:
v.select = True
def SepareMesh(size, nomeID):
#deselect all vertices
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
get_vertex_list_python(nomeID, size)
#separe the vertex
bpy.context.scene.objects.active=bpy.data.objects[nomeID]
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.separate(type="SELECTED")
bpy.ops.object.mode_set(mode='OBJECT')
return
def SetOrigin(size, nomeID):
oblower, obhighter = FindLowHigVertex(nomeID)
cursorx = oblower.co[0] + (obhighter.co[0] - oblower.co[0])/2
cursory = oblower.co[1] + (obhighter.co[1] - oblower.co[1])/2
bpy.context.scene.cursor_location = Vector((cursorx, cursory, 0))
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_name(name=nomeID)
bpy.context.scene.objects.active=bpy.data.objects[nomeID]
bpy.ops.object.origin_set(type="ORIGIN_CURSOR")
class SepareMeshPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_SepareMeshPanel"
bl_label = "Separe Mesh"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
#set all the needed properies
SetProperties()
@classmethod
#only if the object selected is a mesh object the menu will be displayed
def poll(self, context):
if context.object and context.object.type == "MESH":
return 1
def draw(self, context):
layout = self.layout
box1 = layout.box()
row = box1.row(align=False)
row.prop(context.scene, "separator_square_size")
row = box1.row(align=False)
row.operator("object.separe_mesh", text="Separe Mesh")
box2 = layout.box()
row = box2.row(align=False)
row.prop(context.scene, "exported_obj_path")
row = box2.row(align=False)
row.prop(context.scene, "delete_file_if_already_exists")
row = box2.row(align=False)
row.operator("object.export_objects", text="Export Objects")
class SepareMeshOperator(bpy.types.Operator):
bl_idname = "object.separe_mesh"
bl_label = "Separe Mesh"
bl_description = "Separe the mesh in tiles, change the origin and rename them"
bl_options = {'REGISTER', 'UNDO'}
context = bpy.context
def execute(self, context):
import bpy
#get options
size = context.scene.separator_square_size
nomeID = context.active_object.name
lower, highter = FindLowHigVertex(nomeID)
tiles_height = int((highter.co[1] - lower.co[1])/size) +1 #round per excess
tiles_width = int((highter.co[0] - lower.co[0])/size) + 1
lox=lower.co[0]
loy=lower.co[1]
hix=highter.co[0]
hiy=highter.co[1]
#SEPARE MESH
print("The mesh will be divided in ", tiles_height ," rows and ", tiles_width , " columns for a total of ", tiles_height*tiles_width , " objects..")
for w in range(tiles_width):
for h in range(tiles_height):
SepareMesh(size, nomeID)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_name(name=nomeID)
bpy.ops.object.delete()
#######################################
#ORIGIN SET
print("Setting the orgins..")
#set the origin
for obj in bpy.data.objects:
#it the name of the object has the same name of the separed mesh followed by whatever
if (obj.name.rfind(nomeID))==0 and obj.type=="MESH":
#set the origin to the center of the square mesh
SetOrigin(size, obj.name)
#NAME SET
print("Setting the names..")
for obj in bpy.data.objects:
#if the name of the object has the same name of the separed mesh followed by whatever
if obj.name.rfind(nomeID)==0 and obj.type=="MESH":
#struct of the object name: "xsector ysector"
firstpart=str(int((obj.location[0]-lox + size/2)/size)) +" "
secondpart=str(int((obj.location[1]-loy + size/2)/size))
obj.name=firstpart+secondpart
obj.data.name=obj.name #set the same name for the mesh
#######################################
print("Finished")
return{'FINISHED'}
class ExportObjectsOperator(bpy.types.Operator):
bl_idname = "object.export_objects"
bl_label = "Export Objects"
bl_description = "Export the objects"
bl_options = {'REGISTER', 'UNDO'}
context = bpy.context
def execute(self, context):
if not os.path.exists(bpy.path.abspath(context.scene.exported_obj_path)):
try:
os.mkdir(context.scene.exported_obj_path)
except:
print("An error occoured while creating the directory")
path = bpy.path.abspath(context.scene.exported_obj_path)
print("Exporting to : " + path)
for obj in bpy.data.objects:#add list from previous script if have time
if obj.type=="MESH":
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_name(name=obj.name)
file_path = bpy.path.ensure_ext(path + obj.name, ".obj")
#if the file exist overwrite or not
if os.path.exists(file_path):
if context.scene.delete_file_if_already_exists:
try:
os.remove(file_path)
except:
print("Unable to remove the file")
else:
continue
loc = obj.location
bpy.ops.object.location_clear()
bpy.ops.export_scene.obj(filepath=file_path,
check_existing=True,
filter_glob="*.obj;*.mtl",
use_selection=True,
use_all_scenes=False,
use_animation=False,
use_apply_modifiers=True,
use_rotate_x90=True,
use_edges=True,
use_normals=False,
use_hq_normals=True,
use_uvs=True,
use_materials=True,
copy_images=False,
use_triangles=False,
use_vertex_groups=False,
use_nurbs=False,
use_blen_objects=True,
group_by_object=False,
group_by_material=False,
keep_vertex_order=False)
obj.location = loc
return{'FINISHED'}
def register():
bpy.utils.register_class(SepareMeshOperator)
bpy.utils.register_class(ExportObjectsOperator)
bpy.utils.register_class(SepareMeshPanel)
def unregister():
bpy.utils.unregister_class(SepareMeshOperator)
bpy.utils.unregister_class(ExportObjectsOperator)
bpy.utils.unregister_class(SepareMeshPanel)
#register the two classes|| used while trying it in the text editor
if __name__ == "__main__":
register()
You can run it as an addon or in the text window