Latest Blender/SHP Export/Import Scripts

Talk about your new mod or map here

Moderators: bert_the_turtle, jelco

PaladinOfKaos
level1
level1
Posts: 40
Joined: Mon May 07, 2007 4:35 pm

Latest Blender/SHP Export/Import Scripts

Postby PaladinOfKaos » 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:

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.
Last edited by PaladinOfKaos on Thu May 10, 2007 8:30 pm, edited 1 time in total.
User avatar
Testrie
level3
level3
Posts: 486
Joined: Thu Aug 17, 2006 8:02 pm
Location: CA, United States
Contact:

Postby Testrie » Wed May 09, 2007 10:27 pm

Wow. I wish I knew python...

eyra
brice
level2
level2
Posts: 167
Joined: Fri Feb 23, 2007 6:04 am

Postby brice » Wed May 09, 2007 11:07 pm

Testrie wrote:Wow. I wish I knew python...

eyra

Python's great, but you don't need to know it to use the script. You only need to learn to use Blender. The import / export scripts are used by Blender via a menu interface. No programming knowledge required. You can google blender and click 'feelin lucky to find out more about using it for 3D modelling. It's free, and cross platform.

But if you mean you'd like to contribute code... well that's a great aspiration too.
User avatar
Testrie
level3
level3
Posts: 486
Joined: Thu Aug 17, 2006 8:02 pm
Location: CA, United States
Contact:

Postby Testrie » Fri May 11, 2007 3:51 am

Yes, I was well aware of the fact that I didn't need blender to use the script... that reaction actually came from... "Wow... this person managed to write an entire python script... I've tried to learn python many times and failed... I wish I could write something like that..."

eyra
PaladinOfKaos
level1
level1
Posts: 40
Joined: Mon May 07, 2007 4:35 pm

Postby PaladinOfKaos » Fri May 11, 2007 5:36 pm

Testrie wrote:Yes, I was well aware of the fact that I didn't need blender to use the script...


You do need Blender, you just don't need to actually understand the script. If you want to learn, there are several good sites. http://mediawiki.blender.org is good starting point.
brice
level2
level2
Posts: 167
Joined: Fri Feb 23, 2007 6:04 am

Postby brice » Wed May 16, 2007 4:45 am

Thank You!! to whoever sticky'd this!

-brice
User avatar
Testrie
level3
level3
Posts: 486
Joined: Thu Aug 17, 2006 8:02 pm
Location: CA, United States
Contact:

Postby Testrie » Wed May 16, 2007 10:21 pm

PaladinOfKaos wrote:
Testrie wrote:Yes, I was well aware of the fact that I didn't need blender to use the script...


You do need Blender, you just don't need to actually understand the script. If you want to learn, there are several good sites. http://mediawiki.blender.org is good starting point.


did I say blender? I meant python. sorry.

eyra
revcompgeek+
level1
level1
Posts: 11
Joined: Sat Oct 13, 2007 1:57 am

Postby revcompgeek+ » Wed Nov 21, 2007 8:16 pm

I have been using Blender for a while now, and I know python pretty well. If there is a request I could try and make an import script for Blender. I might even start now, but I will work harder on it if there is enough requests.
User avatar
Lowell
level3
level3
Posts: 428
Joined: Mon Jan 02, 2006 8:03 pm
Location: Atlanta, Georgia
Contact:

Postby Lowell » Wed Jan 02, 2008 8:42 am

Sure wish I had that import script. I'm going to give the export script a workover. Thanks for all the hard work...
This will be great in Blender. I am using it for Nif files right now and it has worked out pretty good.
ERing
level1
level1
Posts: 53
Joined: Sat Mar 17, 2007 9:51 pm
Contact:

Postby ERing » Mon Feb 04, 2008 11:14 pm

Yeah, I wish I had the import script too. In the meanwhile, can anyone post a tutorial or a link on how to use Blender to model these?

Hmm... that might not be clear enough.

I know how to use Blender, but seeing as there is no import script, and 3ds Max 5 isn't available, how can I use Blender to modify the models? (I obviously can't use the model pack because that is full of .max files, which apparently is proprietary).

Any ideas?
revcompgeek+
level1
level1
Posts: 11
Joined: Sat Oct 13, 2007 1:57 am

Postby revcompgeek+ » Wed Feb 06, 2008 5:15 am

I had the import script finished a while ago, but I forgot to post it on this thread. I have uploaded the file here. It is still quite buggy, but most of the shapes import correctly. Color support is broken, and if anyone has ideas, please let me know.
User avatar
Lowell
level3
level3
Posts: 428
Joined: Mon Jan 02, 2006 8:03 pm
Location: Atlanta, Georgia
Contact:

Postby Lowell » Wed Feb 06, 2008 6:50 am

Cool...thanks...
I will give it a try this weekend.
ERing
level1
level1
Posts: 53
Joined: Sat Mar 17, 2007 9:51 pm
Contact:

Postby ERing » Thu Feb 07, 2008 3:57 am

I imported a darwinian into blender 2.45, and... well... yea:

Image
User avatar
Lowell
level3
level3
Posts: 428
Joined: Mon Jan 02, 2006 8:03 pm
Location: Atlanta, Georgia
Contact:

Postby Lowell » Thu Feb 07, 2008 5:55 am

...awesome...I am totally there this weekend. I just have a couple more buildings to convert. Just completed my oil tanker for a different mod so I'm happy.
Image
Image

Used Blender to convert this model into Nif (GameBro) format...shown here in-game/editor
User avatar
KingAl
level5
level5
Posts: 4138
Joined: Sun Sep 10, 2006 7:42 am

Postby KingAl » Thu Feb 07, 2008 6:10 am

For a different game? Damn, no Oiltankerwinia for me, then :(
Gentlemen, you can't fight in here: this is the War Room!
Ultimate Uplink Guide
Latest Patch

Return to “Mod Projects”

Who is online

Users browsing this forum: No registered users and 5 guests