Can someone help with OBJ export script?

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

ghoulish
Posts: 7
Joined: Wed Apr 30, 2003 5:19 pm

Can someone help with OBJ export script?

Postby ghoulish » Wed Apr 30, 2003 10:08 pm

Hello - I have come across this OBJ export script from sourceforge:

http://sourceforge.net/projects/blenderobjex/

It does not work with Blender 2.26 for Windows. Can somebody get this to work with Blender 2.26 on Windows? I dont understand Python or scripting. What im after is to be able to export mesh, texture, and material data, and this script is supposed to do that. - Thank you.

Here is the contents of the script:

################################################################################
# $Id: blenderobjex.py,v 1.3 2002/02/02 11:41:55 rid Exp $
#
# blenderobjex - Blender Alias/Wavefront OBJ-File exporter
#
# Copyright (C) 2001-2002 Christoph Frick <rid@zefix.tv>
#
# 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
################################################################################

import Blender210 as Blender
import GUI
import sys, os

def export( filename ):
scene = Blender.getCurrentScene();
objfile = open( filename+".obj", "w" );
mtlfile = open( filename+".mtl", "w" );
bfilename = os.path.basename(filename)

mapfile = open( filename+".map", "w" );
objfile.write( "# \n" );
objfile.write( "mtllib "+bfilename+".mtl\n");
objfile.write( "maplib "+bfilename+".map\n");
mtllib = {};
vs = {};
vns = {};
vts = {};
mats = {};
maps = {};
s_count = 1;
t_count = 1;
n_count = 1;
for name in scene.objects:
if Blender.isMesh( name ):

meshobj = Blender.getObject( name );
mesh = Blender.getMesh( meshobj.data );

for vertex in mesh.vertices:
v = "v %f %f %f" % (vertex[0],vertex[1],vertex[2]);
if not vs.has_key(v):
vs[v] = s_count;
s_count = s_count + 1;
objfile.write( v + "\n" );

for normal in mesh.normals:
vn = "vn %f %f %f" % (normal[0],normal[1],normal[2]);
if not vns.has_key(vn):
vns[vn] = n_count;
n_count = n_count +1;
objfile.write( vn + "\n" );

for texcoord in mesh.texcoords:
for tc in texcoord:
vt = "vt %f %f" % (tc[0],tc[1]);
if not vts.has_key(vt):
vts[vt] = t_count;
t_count = t_count +1;
objfile.write( vt + "\n" );

for material_name in meshobj.materials:
if not mats.has_key( material_name ):
material = Blender.getMaterial( material_name );
mtlfile.write( "newmtl %s\n" % (material_name) );
mtlfile.write( "Ka %f %f %f\n" % (material.Amb,material.Amb,material.Amb) );
mtlfile.write( "Kd %f %f %f\n" % (material.R,material.G,material.B) );
mtlfile.write( "Ks %f %f %f\n" % (material.SpecR,material.SpecG,material.SpecB) );
mtlfile.write( "Ns %f\n" % (material.Spec) );
mtlfile.write( "\n" );
mats[ material_name ] = 1;

if mesh.texture:
bmapname = os.path.basename(mesh.texture);
maps[ mesh.texture ] = bmapname;
mapfile.write( "newmap %s\nKa %s\n\n" % (bmapname,bmapname) );


lastmat = "";

for name in scene.objects:
if Blender.isMesh( name ):

meshobj = Blender.getObject( name );
mesh = Blender.getMesh( meshobj.data );

lastsmooth = -1;
facecount = 0;

if ( mesh.texture ):
objfile.write("usemap %s\n" % (maps[mesh.texture]) );

for face in mesh.faces:
if face[4] != lastsmooth:
objfile.write( "s %i\n" % face[4] );
lastsmooth = face[4];

if len(meshobj.materials) > 0 and lastmat != meshobj.materials[face[5]]:
lastmat = meshobj.materials[face[5]]
objfile.write( "usemtl %s\n"%(lastmat) );

objfile.write( "f " );
for i in range(4):
if i!=3 or face[3]:
x = "";
v = "v %f %f %f" % (mesh.vertices[face[i]][0],mesh.vertices[face[i]][1], mesh.vertices[face[i]][2]);
x = x + "%i/" % (vs[v]);
if len(mesh.texcoords):
tc = mesh.texcoords[facecount][i];
vt = "vt %f %f" % (tc[0],tc[1]);
x = x + "%d"%(vts[vt]);
x = x + "/";
vn = "vn %f %f %f" % (mesh.normals[face[i]][0],mesh.normals[face[i]][1], mesh.normals[face[i]][2]);
x = x + "%i"%(vns[vn]);
x = x + " ";
objfile.write( x );
objfile.write( "\n" );
facecount = facecount+1;

def callback(fs):
export(fs.filename)

fs = GUI.FileSelector()
fs.activate(callback, fs)

Michel
Posts: 208
Joined: Wed Oct 16, 2002 7:27 pm
Location: Somewhere below the rivers in Holland (but not Limburg)

Re: Can someone help with OBJ export script?

Postby Michel » Thu May 01, 2003 8:57 am

ghoulish wrote:import Blender210 as Blender
import GUI


Hi,

the Blender210 and GUI modules are not included in Blender 2.26. You'll have to use prior versions of Blender for that.

both modules will probably never return :twisted: . The fileselector from the GUI module will probably return in some other form.

With regards,
Michel

ghoulish
Posts: 7
Joined: Wed Apr 30, 2003 5:19 pm

Postby ghoulish » Thu May 01, 2003 5:08 pm

What are "GUI modules", and how can this script be modified to be used wint Blender 2.26? What is "fileselector from the GUI module"? Is it possible to put back into Blender 2.26 "GUI modules"?

z3r0_d
Posts: 1522
Joined: Wed Oct 16, 2002 2:38 am
Contact:

Postby z3r0_d » Fri May 02, 2003 6:07 am

ghoulish wrote:What are "GUI modules",

Graphical User Interface
they do things like buttons, and textinputs.
ghoulish wrote:how can this script be modified to be used wint Blender 2.26?

I would look for what values the necescary ui items return, and set them to hard coded values, as a hack.
but I know python
(I could suggest how to change the script if you put it into code block when you posted so that the indentation would be saved[code]...[removme/code])
ghoulish wrote:What is "fileselector from the GUI module"?

it is a lot like the open and save windows
----------------------edit----------------------
I now see a link to the sourceforge page
am too lazy to extact a tar.gz to try to update it
why not use an older version of blender? 2.23 should work.
is on pablosbrain
http://www.pablosbrain.com/blender3d/files/

ghoulish
Posts: 7
Joined: Wed Apr 30, 2003 5:19 pm

Postby ghoulish » Fri May 02, 2003 4:43 pm

Thanks for you reply z3r0_d, but your talking way over my head. I dont konw the first thing about programming, scripting, file extensions, GUI modules or anything related. All i know to do is make pictures, and shapes. What im after here is to be able to take a shape i made in Blender (with UV textures added) and export it to be used elsewhere.

z3r0_d
Posts: 1522
Joined: Wed Oct 16, 2002 2:38 am
Contact:

Postby z3r0_d » Sat May 03, 2003 8:40 pm

I am (again) too lazy to fix it, but I would suggest you try it in Blender 2.23, not 2.25 or 2.26.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest