I’m still working on this. Many things changed
-no more support in C, too problematic to compile on each system
-export function to export the separated mesh, zipped in 2 different ways
-many and various bug fixes, and internal changes
bl_info = {
"name": "Mesh Separator",
"author": "Zoffoli Francesco (Makers_F)",
"version": (0,5),
"blender": (2, 5, 6),
"api": 35517,
"location": "View3D > Toolshelf ",
"description": "Subdivide the mesh in many tiles of given size." ,
"category": "Object"}
import bpy
import os
from mathutils import Vector
from bpy.props import BoolProperty, StringProperty, FloatProperty
from math import pow
import gzip, tarfile
def compress_obj_file(filepath, file_name):
with gzip.open(filepath + file_name + ".gz", mode = 'wb', compresslevel = 3) as new_file:
new_file.write((open(filepath + file_name, 'rb')).read())
new_file.close()
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.triangulate_mesh = BoolProperty(name="Triangulate the mesh when exporting",
description="Check if you want to export the mesh triangulated.",
default=False)
bpy.types.Scene.compress_single_gzip_files = BoolProperty(name="Compress each file",
description="Compress each single file inside a separate gzip file",
default=False)
bpy.types.Scene.compress_all_files_in_tarbz2 = BoolProperty(name="Compress all the files",
description="Compress every file inside a single tar.bz2 archive",
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.prop(context.scene, "triangulate_mesh")
row = box2.row(align=False)
row.prop(context.scene, "compress_single_gzip_files")
row.prop(context.scene, "compress_all_files_in_tarbz2")
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)
#the starting object remains with a 0 verts meshat the end of this process
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'}
(continue…)