Revision: 21291
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21291
Author:   kazanbas
Date:     2009-07-01 20:23:11 +0200 (Wed, 01 Jul 2009)

Log Message:
-----------
- started OBJ importer conversion
- added Mesh.add_uv_layer, Object.add_vertex_group

Modified Paths:
--------------
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_mesh_api.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object_api.c

Added Paths:
-----------
    branches/soc-2009-kazanbas/release/io/import_obj.py

Copied: branches/soc-2009-kazanbas/release/io/import_obj.py (from rev 21285, 
branches/soc-2009-kazanbas/release/scripts/import_obj.py)
===================================================================
--- branches/soc-2009-kazanbas/release/io/import_obj.py                         
(rev 0)
+++ branches/soc-2009-kazanbas/release/io/import_obj.py 2009-07-01 18:23:11 UTC 
(rev 21291)
@@ -0,0 +1,1280 @@
+#!BPY
+ 
+"""
+Name: 'Wavefront (.obj)...'
+Blender: 249
+Group: 'Import'
+Tooltip: 'Load a Wavefront OBJ File, Shift: batch import all dir.'
+"""
+
+__author__= "Campbell Barton", "Jiri Hnidek", "Paolo Ciccone"
+__url__= 
['http://wiki.blender.org/index.php/Scripts/Manual/Import/wavefront_obj', 
'blender.org', 'blenderartists.org']
+__version__= "2.11"
+
+__bpydoc__= """\
+This script imports a Wavefront OBJ files to Blender.
+
+Usage:
+Run this script from "File->Import" menu and then load the desired OBJ file.
+Note, This loads mesh objects and materials only, nurbs and curves are not 
supported.
+"""
+
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# Script copyright (C) Campbell J Barton 2007
+#
+# 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 *****
+# --------------------------------------------------------------------------
+
+from Blender import Mesh, Draw, Window, Texture, Material, sys
+import bpy
+import BPyMesh
+import BPyImage
+import BPyMessages
+
+try:           import os
+except:                os= False
+
+# Generic path functions
+def stripFile(path):
+       '''Return directory, where the file is'''
+       lastSlash= max(path.rfind('\\'), path.rfind('/'))
+       if lastSlash != -1:
+               path= path[:lastSlash]
+       return '%s%s' % (path, sys.sep)
+
+def stripPath(path):
+       '''Strips the slashes from the back of a string'''
+       return path.split('/')[-1].split('\\')[-1]
+
+def stripExt(name): # name is a string
+       '''Strips the prefix off the name before writing'''
+       index= name.rfind('.')
+       if index != -1:
+               return name[ : index ]
+       else:
+               return name
+# end path funcs
+
+def unpack_list(list_of_tuples):
+       l = []
+       for t in list_of_tuples:
+               l.extend(t)
+       return l
+
+# same as above except that it adds 0 for triangle faces
+def unpack_face_list(list_of_tuples):
+       l = []
+       for t in list_of_tuples:
+               if len(t) == 3:
+                       t += [0]
+               l.extend(t)
+       return l
+
+
+def line_value(line_split):
+       '''
+       Returns 1 string represneting the value for this line
+       None will be returned if theres only 1 word
+       '''
+       length= len(line_split)
+       if length == 1:
+               return None
+       
+       elif length == 2:
+               return line_split[1]
+       
+       elif length > 2:
+               return ' '.join( line_split[1:] )
+
+def obj_image_load(imagepath, DIR, IMAGE_SEARCH):
+       '''
+       Mainly uses comprehensiveImageLoad
+       but tries to replace '_' with ' ' for Max's exporter replaces spaces 
with underscores.
+       '''
+       
+       if '_' in imagepath:
+               image= BPyImage.comprehensiveImageLoad(imagepath, DIR, 
PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
+               if image: return image
+               # Did the exporter rename the image?
+               image= BPyImage.comprehensiveImageLoad(imagepath.replace('_', ' 
'), DIR, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
+               if image: return image
+       
+       # Return an image, placeholder if it dosnt exist
+       image= BPyImage.comprehensiveImageLoad(imagepath, DIR, PLACE_HOLDER= 
True, RECURSIVE= IMAGE_SEARCH)
+       return image
+       
+
+def create_materials(filepath, material_libs, unique_materials, 
unique_material_images, IMAGE_SEARCH):
+       '''
+       Create all the used materials in this obj,
+       assign colors and images to the materials from all referenced material 
libs
+       '''
+       DIR= stripFile(filepath)
+       
+       
#==================================================================================#
+       # This function sets textures defined in .mtl file                      
           #
+       
#==================================================================================#
+       def load_material_image(blender_material, context_material_name, 
imagepath, type):
+               
+               texture= bpy.data.textures.new(type)
+               texture.setType('Image')
+               
+               # Absolute path - c:\.. etc would work here
+               image= obj_image_load(imagepath, DIR, IMAGE_SEARCH)
+               has_data = image.has_data
+               texture.image = image
+               
+               # Adds textures for materials (rendering)
+               if type == 'Kd':
+                       if has_data and image.depth == 32:
+                               # Image has alpha
+                               blender_material.setTexture(0, texture, 
Texture.TexCo.UV, Texture.MapTo.COL | Texture.MapTo.ALPHA)
+                               texture.setImageFlags('MipMap', 'InterPol', 
'UseAlpha')
+                               blender_material.mode |= Material.Modes.ZTRANSP
+                               blender_material.alpha = 0.0
+                       else:
+                               blender_material.setTexture(0, texture, 
Texture.TexCo.UV, Texture.MapTo.COL)
+                               
+                       # adds textures to faces (Textured/Alt-Z mode)
+                       # Only apply the diffuse texture to the face if the 
image has not been set with the inline usemat func.
+                       unique_material_images[context_material_name]= image, 
has_data # set the texface image
+               
+               elif type == 'Ka':
+                       blender_material.setTexture(1, texture, 
Texture.TexCo.UV, Texture.MapTo.CMIR) # TODO- Add AMB to BPY API
+                       
+               elif type == 'Ks':
+                       blender_material.setTexture(2, texture, 
Texture.TexCo.UV, Texture.MapTo.SPEC)
+               
+               elif type == 'Bump':
+                       blender_material.setTexture(3, texture, 
Texture.TexCo.UV, Texture.MapTo.NOR)            
+               elif type == 'D':
+                       blender_material.setTexture(4, texture, 
Texture.TexCo.UV, Texture.MapTo.ALPHA)                          
+                       blender_material.mode |= Material.Modes.ZTRANSP
+                       blender_material.alpha = 0.0
+                       # Todo, unset deffuse material alpha if it has an alpha 
channel
+                       
+               elif type == 'refl':
+                       blender_material.setTexture(5, texture, 
Texture.TexCo.UV, Texture.MapTo.REF)            
+       
+       
+       # Add an MTL with the same name as the obj if no MTLs are spesified.
+       temp_mtl= stripExt(stripPath(filepath))+ '.mtl'
+       
+       if sys.exists(DIR + temp_mtl) and temp_mtl not in material_libs:
+                       material_libs.append( temp_mtl )
+       del temp_mtl
+       
+       #Create new materials
+       for name in unique_materials: # .keys()
+               if name != None:
+                       unique_materials[name]= bpy.data.materials.new(name)
+                       unique_material_images[name]= None, False # assign None 
to all material images to start with, add to later.
+               
+       unique_materials[None]= None
+       unique_material_images[None]= None, False
+       
+       for libname in material_libs:
+               mtlpath= DIR + libname
+               if not sys.exists(mtlpath):
+                       #print '\tError Missing MTL: "%s"' % mtlpath
+                       pass
+               else:
+                       #print '\t\tloading mtl: "%s"' % mtlpath
+                       context_material= None
+                       mtl= open(mtlpath, 'rU')
+                       for line in mtl: #.xreadlines():
+                               if line.startswith('newmtl'):
+                                       context_material_name= 
line_value(line.split())
+                                       if 
unique_materials.has_key(context_material_name):
+                                               context_material = 
unique_materials[ context_material_name ]
+                                       else:
+                                               context_material = None
+                               
+                               elif context_material:
+                                       # we need to make a material to assign 
properties to it.
+                                       line_split= line.split()
+                                       line_lower= line.lower().lstrip()
+                                       if line_lower.startswith('ka'):
+                                               
context_material.setMirCol((float(line_split[1]), float(line_split[2]), 
float(line_split[3])))
+                                       elif line_lower.startswith('kd'):
+                                               
context_material.setRGBCol((float(line_split[1]), float(line_split[2]), 
float(line_split[3])))
+                                       elif line_lower.startswith('ks'):
+                                               
context_material.setSpecCol((float(line_split[1]), float(line_split[2]), 
float(line_split[3])))
+                                       elif line_lower.startswith('ns'):
+                                               context_material.setHardness( 
int((float(line_split[1])*0.51)) )
+                                       elif line_lower.startswith('ni'): # 
Refraction index
+                                               context_material.setIOR( max(1, 
min(float(line_split[1]), 3))) # Between 1 and 3
+                                       elif line_lower.startswith('d') or 
line_lower.startswith('tr'):
+                                               
context_material.setAlpha(float(line_split[1]))
+                                       elif line_lower.startswith('map_ka'):
+                                               img_filepath= 
line_value(line.split())
+                                               if img_filepath:
+                                                       
load_material_image(context_material, context_material_name, img_filepath, 'Ka')
+                                       elif line_lower.startswith('map_ks'):
+                                               img_filepath= 
line_value(line.split())
+                                               if img_filepath:
+                                                       
load_material_image(context_material, context_material_name, img_filepath, 'Ks')
+                                       elif line_lower.startswith('map_kd'):
+                                               img_filepath= 
line_value(line.split())
+                                               if img_filepath:
+                                                       
load_material_image(context_material, context_material_name, img_filepath, 'Kd')
+                                       elif line_lower.startswith('map_bump'):
+                                               img_filepath= 
line_value(line.split())
+                                               if img_filepath:
+                                                       
load_material_image(context_material, context_material_name, img_filepath, 
'Bump')
+                                       elif line_lower.startswith('map_d') or 
line_lower.startswith('map_tr'): # Alpha map - Dissolve
+                                               img_filepath= 
line_value(line.split())
+                                               if img_filepath:
+                                                       
load_material_image(context_material, context_material_name, img_filepath, 'D')
+                                       
+                                       elif line_lower.startswith('refl'): # 
Reflectionmap
+                                               img_filepath= 
line_value(line.split())
+                                               if img_filepath:
+                                                       
load_material_image(context_material, context_material_name, img_filepath, 
'refl')
+                       mtl.close()
+
+
+
+       
+def split_mesh(verts_loc, faces, unique_materials, filepath, 
SPLIT_OB_OR_GROUP, SPLIT_MATERIALS):
+       '''
+       Takes vert_loc and faces, and seperates into multiple sets of 
+       (verts_loc, faces, unique_materials, dataname)
+       This is done so objects do not overload the 16 material limit.
+       '''
+       
+       filename = stripExt(stripPath(filepath))
+       
+       if not SPLIT_OB_OR_GROUP and not SPLIT_MATERIALS:
+               # use the filename for the object name since we arnt chopping 
up the mesh.
+               return [(verts_loc, faces, unique_materials, filename)]
+       
+       
+       def key_to_name(key):
+               # if the key is a tuple, join it to make a string
+               if type(key) == tuple:
+                       return '%s_%s' % key
+               elif not key:
+                       return filename # assume its a string. make sure this 
is true if the splitting code is changed
+               else:

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to