OBJ importer almost converted, except a few features (NURBS, NGON, FGON and sharp edges).

Added to API:
- Main.add_image
- Material.z_transparency
- two temporary properties: Image.depth and Image.has_data
This commit is contained in:
2009-07-03 17:44:20 +00:00
parent 617851bf21
commit 2a23fda9d5
4 changed files with 158 additions and 47 deletions

View File

@@ -40,14 +40,16 @@ Note, This loads mesh objects and materials only, nurbs and curves are not suppo
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
from Blender import Mesh, Draw, Window, Texture, Material, sys
import bpy
# import BPyMesh
import BPyImage
import BPyMessages
import os
try: import os
except: os= False
# from Blender import Mesh, Draw, Window, Texture, Material, sys
# # import BPyMesh
# import BPyImage
# import BPyMessages
# try: import os
# except: os= False
# Generic path functions
def stripFile(path):
@@ -101,22 +103,44 @@ def line_value(line_split):
elif length > 2:
return ' '.join( line_split[1:] )
# limited replacement for BPyImage.comprehensiveImageLoad
def load_image(imagepath, direc):
if os.path.exists(imagepath):
return bpy.data.add_image(imagepath)
im_base = os.path.basename(imagepath)
tmp = os.path.join(direc, im_base)
if os.path.exists(tmp):
return bpy.data.add_image(tmp)
# TODO comprehensiveImageLoad also searched in bpy.config.textureDir
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)
image= load_image(imagepath.replace('_', ' '), DIR)
if image: return image
return load_image(imagepath, DIR)
# def obj_image_load(imagepath, DIR, IMAGE_SEARCH):
# '''
# Mainly uses comprehensiveImageLoad
# but tries to replace '_' with ' ' for Max's exporter replaces spaces with underscores.
# '''
# Return an image, placeholder if it dosnt exist
image= BPyImage.comprehensiveImageLoad(imagepath, DIR, PLACE_HOLDER= True, RECURSIVE= IMAGE_SEARCH)
return image
# 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):
@@ -139,46 +163,65 @@ def create_materials(filepath, material_libs, unique_materials, unique_material_
# Absolute path - c:\.. etc would work here
image= obj_image_load(imagepath, DIR, IMAGE_SEARCH)
has_data = image.has_data
texture.image = image
if image:
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
# XXX bitmask won't work?
blender_material.add_texture(texture, "UV", ("COLOR", "ALPHA"))
texture.mipmap = True
texture.interpolation = True
texture.use_alpha = True
blender_material.z_transparency = True
blender_material.alpha = 0.0
# 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)
blender_material.add_texture(texture, "UV", "COLOR")
# 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
blender_material.add_texture(texture, "UV", "AMBIENT")
# 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)
blender_material.add_texture(texture, "UV", "SPECULAR")
# 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)
blender_material.add_texture(texture, "UV", "NORMAL")
# 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.add_texture(texture, "UV", "ALPHA")
blender_material.z_transparency = True
blender_material.alpha = 0.0
# 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)
blender_material.add_texture(texture, "UV", "REFLECTION")
# 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 )
material_libs.append( temp_mtl )
del temp_mtl
#Create new materials
@@ -578,7 +621,7 @@ def create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_l
me.add_geometry(0, len(edges))
# edges is (should be) a list of (a, b) tuples
# edges should be a list of (a, b) tuples
me.edges.foreach_set("verts", unpack_list(edges))
# me_edges.extend( edges )
@@ -713,6 +756,7 @@ def get_float_func(filepath):
return float
def load_obj(filepath,
context,
CLAMP_SIZE= 0.0,
CREATE_FGONS= True,
CREATE_SMOOTH_GROUPS= True,
@@ -733,8 +777,9 @@ def load_obj(filepath,
if SPLIT_OBJECTS or SPLIT_GROUPS or SPLIT_MATERIALS:
POLYGROUPS = False
time_main= sys.time()
time_main= bpy.sys.time()
# time_main= sys.time()
verts_loc= []
verts_tex= []
@@ -772,7 +817,8 @@ def load_obj(filepath,
context_multi_line= ''
print '\tparsing obj file "%s"...' % filepath,
time_sub= sys.time()
time_sub= bpy.sys.time()
# time_sub= sys.time()
file= open(filepath, 'rU')
for line in file: #.xreadlines():
@@ -980,15 +1026,17 @@ def load_obj(filepath,
'''
file.close()
time_new= sys.time()
time_new= bpy.sys.time()
# time_new= sys.time()
print '%.4f sec' % (time_new-time_sub)
time_sub= time_new
print '\tloading materials and images...',
create_materials(filepath, material_libs, unique_materials, unique_material_images, IMAGE_SEARCH)
time_new= sys.time()
time_new= bpy.sys.time()
# time_new= sys.time()
print '%.4f sec' % (time_new-time_sub)
time_sub= time_new
@@ -996,8 +1044,12 @@ def load_obj(filepath,
verts_loc[:] = [(v[0], v[2], -v[1]) for v in verts_loc]
# deselect all
scn = bpy.data.scenes.active
scn.objects.selected = []
if context.selected_objects:
bpy.ops.OBJECT_OT_select_all_toggle()
scene = context.scene
# scn = bpy.data.scenes.active
# scn.objects.selected = []
new_objects= [] # put new objects here
print '\tbuilding geometry...\n\tverts:%i faces:%i materials: %i smoothgroups:%i ...' % ( len(verts_loc), len(faces), len(unique_materials), len(unique_smooth_groups) ),
@@ -1007,7 +1059,7 @@ def load_obj(filepath,
for verts_loc_split, faces_split, unique_materials_split, dataname in split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP, SPLIT_MATERIALS):
# Create meshes from the data, warning 'vertex_groups' wont support splitting
create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc_split, verts_tex, faces_split, unique_materials_split, unique_material_images, unique_smooth_groups, vertex_groups, dataname)
create_mesh(scene, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc_split, verts_tex, faces_split, unique_materials_split, unique_material_images, unique_smooth_groups, vertex_groups, dataname)
# nurbs support
# for context_nurbs in nurbs:
@@ -1039,8 +1091,9 @@ def load_obj(filepath,
#if not ROTATE_X90:
# for ob in new_objects:
# ob.RotX = -1.570796326794896558
time_new= sys.time()
time_new= bpy.sys.time()
# time_new= sys.time()
print '%.4f sec' % (time_new-time_sub)
print 'finished importing: "%s" in %.4f sec.' % (filepath, (time_new-time_main))
@@ -1292,7 +1345,10 @@ else:
# can convert now: edge flags, edges: lines 508-528
# ngon (uses python module BPyMesh): 384-414
# nurbs: 947-
# clamp size: cannot get bound box with RNA - neither I can write RNA struct function that returns it -
# again, RNA limitation
# warning: uses bpy.sys.exists
# NEXT clamp size: get bound box with RNA
# get back to l 140 (here)
# search image in bpy.config.textureDir - load_image
# replaced BPyImage.comprehensiveImageLoad with a simplified version that only checks additional directory specified, but doesn't search dirs recursively (obj_image_load)
# bitmask won't work? - 132
# uses operator bpy.ops.OBJECT_OT_select_all_toggle() to deselect all (not necessary?)
# uses bpy.sys.exists and bpy.sys.time()

View File

@@ -66,6 +66,29 @@ static int rna_Image_dirty_get(PointerRNA *ptr)
return 0;
}
static int rna_Image_has_data_get(PointerRNA *ptr)
{
Image *im= (Image*)ptr->data;
if (im->ibufs.first)
return 1;
return 0;
}
static int rna_Image_depth_get(PointerRNA *ptr)
{
Image *im= (Image*)ptr->data;
ImBuf *ibuf= BKE_image_get_ibuf(im, NULL);
if (!ibuf) return 0;
if (ibuf->rect_float)
return 128;
return ibuf->depth;
}
#else
static void rna_def_imageuser(BlenderRNA *brna)
@@ -275,6 +298,20 @@ static void rna_def_image(BlenderRNA *brna)
RNA_def_property_boolean_sdna(prop, NULL, "tpageflag", IMA_CLAMP_V);
RNA_def_property_ui_text(prop, "Clamp Y", "Disable texture repeating vertically.");
RNA_def_property_update(prop, NC_IMAGE|ND_DISPLAY, NULL);
/*
Image.has_data and Image.depth are temporary,
Update import_obj.py when they are replaced (Arystan)
*/
prop= RNA_def_property(srna, "has_data", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_funcs(prop, "rna_Image_has_data_get", NULL);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Has data", "True if this image has data.");
prop= RNA_def_property(srna, "depth", PROP_INT, PROP_NONE);
RNA_def_property_int_funcs(prop, "rna_Image_depth_get", NULL, NULL);
RNA_def_property_ui_text(prop, "Depth", "Image bit depth.");
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
}
void RNA_def_image(BlenderRNA *brna)

View File

@@ -33,6 +33,8 @@
#include "RNA_types.h"
#include "DNA_object_types.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#ifdef RNA_RUNTIME
@@ -41,8 +43,7 @@
#include "BKE_library.h"
#include "BKE_object.h"
#include "BKE_material.h"
#include "DNA_mesh_types.h"
#include "BKE_image.h"
static Mesh *rna_Main_add_mesh(Main *main, char *name)
{
@@ -102,6 +103,11 @@ struct Tex *rna_Main_add_texture(Main *main, char *name)
/* TODO: remove texture? */
struct Image *rna_Main_add_image(Main *main, char *filename)
{
return BKE_add_image_file(filename, 0);
}
#else
void RNA_api_main(StructRNA *srna)
@@ -164,6 +170,13 @@ void RNA_api_main(StructRNA *srna)
parm= RNA_def_pointer(func, "texture", "Texture", "", "New texture.");
RNA_def_function_return(func, parm);
func= RNA_def_function(srna, "add_image", "rna_Main_add_image");
RNA_def_function_ui_description(func, "Add a new image.");
parm= RNA_def_string(func, "filename", "", 0, "", "Filename to load image from.");
RNA_def_property_flag(parm, PROP_REQUIRED);
parm= RNA_def_pointer(func, "image", "Image", "", "New image.");
RNA_def_function_return(func, parm);
}
#endif

View File

@@ -1082,6 +1082,11 @@ void RNA_def_material(BlenderRNA *brna)
RNA_def_property_boolean_sdna(prop, NULL, "mode", MA_TANGENT_V);
RNA_def_property_ui_text(prop, "Tangent Shading", "Use the material's tangent vector instead of the normal for shading - for anisotropic shading effects");
RNA_def_property_update(prop, NC_MATERIAL|ND_SHADING, NULL);
prop= RNA_def_property(srna, "z_transparency", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "mode", MA_ZTRA);
RNA_def_property_ui_text(prop, "ZTransp", "Z-buffer transparency");
RNA_def_property_update(prop, NC_MATERIAL|ND_SHADING, NULL);
/* nested structs */
prop= RNA_def_property(srna, "raytrace_mirror", PROP_POINTER, PROP_NEVER_NULL);