Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillian Padovani Germano <wpgermano@gmail.com>2004-06-07 05:34:15 +0400
committerWillian Padovani Germano <wpgermano@gmail.com>2004-06-07 05:34:15 +0400
commitb82be27659f19b2916e0744a881ef7659f232ade (patch)
tree8fd59e27a0a346d28df1c8f522d13df4c1548dc6 /release/scripts/obj_export.py
parent372636c116b30adf8494bc035e1d6521e519fcd4 (diff)
Bundled scripts:
-starting updates and new additions for Blender 2.34: Some of the new scripts require Python modules not builtin with Blender, so you either need a full Python install or the needed extra modules. This is an ongoing work, there should be more scripts, better testing and also proper ways to tell users they don't have all expected modules. It's expected that Win users won't need full Python installs, since we can provide a minimal zip with the needed modules from 2.34 on. Thanks to Anthony D'Agostino (scorpius), Jean-Michel Soler (jms) and Campbell Barton (Cam / ideasman) for donating the scripts now added / updated. BPython: -added two new script menu groups: Tools and Utils. We still need to find places elsewhere in the gui where the groups can be put.
Diffstat (limited to 'release/scripts/obj_export.py')
-rw-r--r--release/scripts/obj_export.py161
1 files changed, 161 insertions, 0 deletions
diff --git a/release/scripts/obj_export.py b/release/scripts/obj_export.py
new file mode 100644
index 00000000000..2fb84274c98
--- /dev/null
+++ b/release/scripts/obj_export.py
@@ -0,0 +1,161 @@
+#!BPY
+
+"""
+Name: 'OBJ Wavefront'
+Blender: 232
+Group: 'Export'
+Tooltip: 'Save a Wavefront OBJ File'
+"""
+
+# --------------------------------------------------------------------------
+# OBJ Export v0.9 by Campbell Barton (AKA Ideasman)
+# --------------------------------------------------------------------------
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# ***** END GPL LICENCE BLOCK *****
+# --------------------------------------------------------------------------
+
+#================================================#
+# Gets the world matrix of an object #
+# by multiplying by parents mat's recursively #
+# This only works in some simple situations, #
+# needs work.... #
+#================================================#
+def getWorldMat(ob):
+ mat = ob.getMatrix()
+ p = ob.getParent()
+ if p != None:
+ mat = mat + getWorldMat(p)
+ return mat
+
+#==================#
+# Apply Transform #
+#==================#
+def apply_transform(verts, matrix):
+
+ x, y, z = verts
+ xloc, yloc, zloc = matrix[3][0], matrix[3][1], matrix[3][2]
+ xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + xloc
+ ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + yloc
+ zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + zloc
+
+ return [xcomponent, ycomponent, zcomponent]
+
+
+#=====================================#
+# Apply Transform for vertex normals #
+# ignore the translation #
+#=====================================#
+def apply_normal_transform(verts, matrix):
+ x, y, z = verts
+ xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0]
+ ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1]
+ zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2]
+ return Mathutils.Vector([xcomponent, ycomponent, zcomponent])
+
+#====================================================#
+# Return a 6 deciaml point floating point value #
+# as a string that dosent have any python chars #
+#====================================================#
+def saneFloat(float):
+ #return '%(float)b' % vars() # 6 fp as house.hqx
+ return str('%f' % float) + ' '
+
+
+from Blender import *
+
+NULL_MAT = '(null)'
+
+def save_obj(filename):
+
+ file = open(filename, "w")
+
+ # Write Header
+ file.write('# Blender OBJ File: ' + Get('filename') + ' \n')
+ file.write('# www.blender.org\n')
+
+ # Get all meshs
+ for ob in Object.Get():
+ if ob.getType() == 'Mesh':
+ m = ob.getData()
+ if len(m.verts) > 0: # Make sure there is somthing to write.
+
+ # Set the default mat
+ currentMatName = ''
+
+ file.write('o ' + ob.getName() + '_' + m.name + '\n') # Write Object name
+
+ # Dosent work properly,
+ matrix = getWorldMat(ob)
+
+ # Vert
+ for v in m.verts:
+ # Transform the vert
+ vTx = apply_transform(v.co, matrix)
+
+ file.write('v ')
+ file.write(saneFloat(vTx[0]))
+ file.write(saneFloat(vTx[1]))
+ file.write(saneFloat(vTx[2]) + '\n')
+
+ # UV
+ for f in m.faces:
+ for uvIdx in range(len(f.v)):
+ file.write('vt ')
+ if f.uv:
+ file.write(saneFloat(f.uv[uvIdx][0]))
+ file.write(saneFloat(f.uv[uvIdx][1]))
+ else:
+ file.write('0.0 ')
+ file.write('0.0 ')
+
+ file.write('0.0' + '\n')
+
+ # NORMAL
+ for f in m.faces:
+ for v in f.v:
+ # Transform the normal
+ noTx = apply_normal_transform(v.no, matrix)
+ noTx.normalize()
+ file.write('vn ')
+ file.write(saneFloat(noTx[0]))
+ file.write(saneFloat(noTx[1]))
+ file.write(saneFloat(noTx[2]) + '\n')
+
+ uvIdx = 0
+ for f in m.faces:
+ # Check material and change if needed.
+ if len(m.materials) > f.mat:
+ if currentMatName != m.materials[f.mat].getName():
+ currentMatName = m.materials[f.mat].getName()
+ file.write('usemtl ' + currentMatName + '\n')
+
+ elif currentMatName != NULL_MAT:
+ currentMatName = NULL_MAT
+ file.write('usemtl ' + currentMatName + '\n')
+
+ file.write('f ')
+ for v in f.v:
+ file.write( str(m.verts.index(v) +1) + '/') # Vert IDX
+ file.write( str(uvIdx +1) + '/') # UV IDX
+ file.write( str(uvIdx +1) + ' ') # NORMAL IDX
+ uvIdx+=1
+ file.write('\n')
+
+ file.close()
+
+Window.FileSelector(save_obj, 'SELECT OBJ FILE')