Latest Blender/SHP Export/Import Scripts
Posted: Wed May 09, 2007 8:56 pm
To any mod or admin: Please sticky this. Thanks in advance.
Current Exporter Version: 1.0.1
Current Importer Version: None
I recommend saving these scripts as shape_export.py and shape_import.py. Place them in your Blender scripts location (The exact location varies, sorry). They should show up as 'File->Export->Darwinia Shape File (.shp)' and 'File->Import->Darwinia Shape File (.shp)'
Export Script:
Implemented Features:
* Location and Orientation exported properly
* Markers can be created with Blender 'empties'
* Defaults to a .shp file
* Uses Blender's WaitCursor() during calculations
To-Do:
* Allow Meshes as Markers
* Automatically triangulate meshes
Notes:
* Every selected mesh will be exported as an SHP fragment. Blender's parent-child relationships are used to create the parent-child relationships of the SHP fragments
* Blender's axes map to Darwinia axes like so:
* Object names are used to name fragments. Blender's 'name.001' duplication method works fine.
* Don't start any mesh names with 'Marker', as that will be used to support mesh-based markers in a future version. You've been warned.
* Can be slow on large meshes. Most Darwinia meshes should be low-poly, though, so it shouldn't be a huge issue
Changelog
* 1.0.1: Added WaitCursor() around main code.
The Code
Import Script:
Currently unavailable. I'll put it here once it's done.
Current Exporter Version: 1.0.1
Current Importer Version: None
I recommend saving these scripts as shape_export.py and shape_import.py. Place them in your Blender scripts location (The exact location varies, sorry). They should show up as 'File->Export->Darwinia Shape File (.shp)' and 'File->Import->Darwinia Shape File (.shp)'
Export Script:
Implemented Features:
* Location and Orientation exported properly
* Markers can be created with Blender 'empties'
* Defaults to a .shp file
* Uses Blender's WaitCursor() during calculations
To-Do:
* Allow Meshes as Markers
* Automatically triangulate meshes
Notes:
* Every selected mesh will be exported as an SHP fragment. Blender's parent-child relationships are used to create the parent-child relationships of the SHP fragments
* Blender's axes map to Darwinia axes like so:
Code: Select all
Z Y
| Y | X
| / | /
|/ |/
0---X 0---Z
(Blender) (Darwinia)
* Object names are used to name fragments. Blender's 'name.001' duplication method works fine.
* Don't start any mesh names with 'Marker', as that will be used to support mesh-based markers in a future version. You've been warned.
* Can be slow on large meshes. Most Darwinia meshes should be low-poly, though, so it shouldn't be a huge issue
Changelog
* 1.0.1: Added WaitCursor() around main code.
The Code
Code: Select all
#!BPY
# """
# Name: 'Darwinia Shape File (.shp)'
# Blender: 243
# Group: 'Export'
# Tooltip: 'Export to the Shape format used by Darwinia'
# """
__author__ = 'Branan Riley'
__version__ = '0.9'
__email__ = 'branan@gmail.com'
__bpydoc__ = """\
This script exports to the Shape format used by Darwinia.
Every selected object will be exported as a fragment in
the shape file. If an object has a parent, that parent will
be defined as the parent of its fragment. Otherwise, a
fragment will be parented to 'SceneRoot'. Markers are not
yet supported, but it should be pretty easy, once I get
everything else done.
This program is distributed under the GPL. The complete
text of the GPL is availble at http://www.gnu.org/licenses/gpl.txt
"""
import Blender
mesh_objects = []
empty_objects = []
class shp_object:
def __init__(self):
self.ParentName='SceneRoot'
self.Up=0.0,1.0,0.0
self.Front=0.0,0.0,1.0
self.Pos=0.0,0.0,0.0
self.Name='NewDataChunk'
def setParent(self, parent):
self.ParentName=parent
def setUp(self, x, y, z):
self.Up=x,y,z
def setFront(self, x, y, z):
self.Front=x,y,z
def setPos(self, x, y, z):
self.Pos=x,y,z
def setName(self, name):
self.Name=name
class shp_fragment(shp_object):
def __init__(self):
shp_object.__init__(self)
self.Positions=[]
self.Colours=[]
self.Vertices=[]
self.Triangles=[]
def addPosition(self, x, y, z):
if self.Positions.count((x,y,z)) == 0:
self.Positions.append((x,y,z))
return self.Positions.index((x,y,z))
def addColour(self, r, g, b):
if self.Colours.count((r,g,b)) == 0:
self.Colours.append((r,g,b))
return self.Colours.index((r,g,b))
def addVertex(self, pos, col):
if self.Vertices.count((pos,col)) == 0:
self.Vertices.append((pos,col))
return self.Vertices.index((pos,col))
def addTriangle(self, pos0, pos1, pos2):
self.Triangles.append((pos0,pos1,pos2))
def write(self, file):
file.write('Fragment: '+self.Name+'\n')
file.write('\tParentName: '+self.ParentName+'\n')
s = '\tUp:'
for i in range(3):
s = s+' '+"% .3f" % self.Up[i]
s = s+'\n'
file.write(s)
s = '\tFront:'
for i in range(3):
s = s+' '+"% .3f" % self.Front[i]
s = s+'\n'
file.write(s)
s = '\tPos:'
for i in range(3):
s = s+' '+"% .3f" % self.Pos[i]
s = s+'\n'
file.write(s)
s = '\tPositions: '+str(len(self.Positions))+'\n'
file.write(s)
for i in range(len(self.Positions)):
s = '\t\t'+str(i)+':'
for j in range(3):
s = s+' '+"% .3f" % self.Positions[i][j]
s = s+'\n'
file.write(s)
file.write('\tNormals: 0\n')
s = '\tColours: '+str(len(self.Colours))+'\n'
file.write(s)
for i in range(len(self.Colours)):
s = '\t\t'+str(i)+':'
for j in range(3):
s = s+' '+str(self.Colours[i][j])
s = s+'\n'
file.write(s)
s = '\tVertices: '+str(len(self.Vertices))+'\n'
file.write(s)
for i in range(len(self.Vertices)):
s = '\t\t'+str(i)+':'
for j in range(2):
s = s+' '+str(self.Vertices[i][j])
s = s+'\n'
file.write(s)
s = '\tTriangles: '+str(len(self.Triangles))+'\n'
file.write(s)
for i in range(len(self.Triangles)):
s = '\t\t'
for j in range(2):
s = s+str(self.Triangles[i][j])+' '
s = s+str(self.Triangles[i][2])+'\n'
file.write(s)
class shp_marker(shp_object):
def __init__(self):
shp_object.__init__(self)
self.Depth = 1
def setDepth(self, depth):
self.Depth = depth
def write(self, file):
file.write('Marker: '+self.Name+'\n')
file.write('\tParentName'+self.ParentName+'\n')
s = '\tDepth: '+str(self.Depth)+'\n'
file.write(s)
s = '\tUp:'
for i in range(3):
s = s+' '+"% .3f" % self.Up[i]
s = s+'\n'
file.write(s)
s = '\tFront:'
for i in range(3):
s = s+' '+"% .3f" % self.Front[i]
s = s+'\n'
file.write(s)
s = '\tPos:'
for i in range(3):
s = s+' '+"% .3f" % self.Pos[i]
s = s+'\n'
file.write(s)
file.write('MarkerEnd\n')
def getObjects():
global mesh_objects
selected_obs = Blender.Object.GetSelected()
for obj in selected_obs:
type = obj.getType()
if type == 'Mesh':
mesh_objects.append(obj)
elif type == 'Empty':
empty_objects.append(obj)
def createShpFragment(obj):
new_shp = shp_fragment()
new_shp.setName(obj.getName())
parent = obj.getParent()
if parent:
new_shp.setParent(parent.getName())
matrix = obj.getMatrix('localspace')
new_shp.setPos(-1.0*matrix[3][0],matrix[3][2],matrix[3][1])
new_shp.setFront(-1.0*matrix[1][0],matrix[1][2],matrix[1][1])
new_shp.setUp(-1.0*matrix[2][0],matrix[2][2],matrix[2][1])
msh_name = obj.getData(name_only=True)
msh = Blender.Mesh.Get(msh_name)
for f in msh.faces:
verts=[0,0,0]
for i in range(3):
col = new_shp.addColour(f.col[i].r,f.col[i].g,f.col[i].b)
pos = new_shp.addPosition(-1.0*f.verts[i].co.x,f.verts[i].co.z,f.verts[i].co.y)
verts[i]=new_shp.addVertex(pos,col)
new_shp.addTriangle(verts[0],verts[1],verts[2])
return new_shp
def countParent(obj,count):
parent = obj.getParent()
if parent:
return countParent(parent, count+1)
else:
return count
def createShpMarker(obj):
new_shp = shp_marker()
new_shp.setName(obj.getName())
parent = obj.getParent()
if parent:
new_shp.setParent(parent.getName())
new_shp.setDepth(countParent(obj,1))
matrix = obj.getMatrix('localspace')
new_shp.setPos(-1.0*matrix[3][0],matrix[3][2],matrix[3][1])
new_shp.setFront(-1.0*matrix[1][0],matrix[1][2],matrix[1][1])
new_shp.setUp(-1.0*matrix[2][0],matrix[2][2],matrix[2][1])
return new_shp
def mainFunc(filename):
Blender.Window.WaitCursor(1)
file = open(filename, 'w')
getObjects()
shp_fragments = []
shp_markers = []
for obj in mesh_objects:
shp_fragments.append(createShpFragment(obj))
for obj in empty_objects:
shp_markers.append(createShpMarker(obj))
for frag in shp_fragments:
frag.write(file)
file.write('\n')
for mrk in shp_markers:
mrk.write(file)
file.close()
Blender.Window.WaitCursor(0)
Blender.Window.RedrawAll()
fname = Blender.sys.makename(ext='.shp')
Blender.Window.FileSelector(mainFunc, "Export SHP File", fname)
Import Script:
Currently unavailable. I'll put it here once it's done.