Newer
Older
if context_multi_line:
# use face_vert_loc_indices and face_vert_tex_indices previously defined and used the obj_face
line_split= line.split()
else:
line_split= line[2:].split()
face_vert_loc_indices= []
face_vert_tex_indices= []
# Instance a face
faces.append((\
face_vert_loc_indices,\
face_vert_tex_indices,\
context_material,\
context_smooth_group,\
context_object\
))
if strip_slash(line_split):
context_multi_line = 'l'
else:
context_multi_line = ''
isline= line.startswith('l')
for v in line_split:
vert_loc_index= int(v)-1
# Make relative negative vert indices absolute
if vert_loc_index < 0:
vert_loc_index= len(verts_loc) + vert_loc_index + 1
face_vert_loc_indices.append(vert_loc_index)
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
elif line.startswith('s'):
if CREATE_SMOOTH_GROUPS:
context_smooth_group= line_value(line.split())
if context_smooth_group=='off':
context_smooth_group= None
elif context_smooth_group: # is not None
unique_smooth_groups[context_smooth_group]= None
elif line.startswith('o'):
if SPLIT_OBJECTS:
context_object= line_value(line.split())
# unique_obects[context_object]= None
elif line.startswith('g'):
if SPLIT_GROUPS:
context_object= line_value(line.split())
# print 'context_object', context_object
# unique_obects[context_object]= None
elif POLYGROUPS:
context_vgroup = line_value(line.split())
if context_vgroup and context_vgroup != '(null)':
vertex_groups.setdefault(context_vgroup, [])
else:
context_vgroup = None # dont assign a vgroup
elif line.startswith('usemtl'):
context_material= line_value(line.split())
unique_materials[context_material]= None
elif line.startswith('mtllib'): # usemap or usemat
material_libs = list(set(material_libs) | set(line.split()[1:])) # can have multiple mtllib filenames per line, mtllib can appear more than once, so make sure only occurance of material exists
# Nurbs support
elif line.startswith('cstype '):
context_nurbs['cstype']= line_value(line.split()) # 'rat bspline' / 'bspline'
elif line.startswith('curv ') or context_multi_line == 'curv':
line_split= line.split()
curv_idx = context_nurbs['curv_idx'] = context_nurbs.get('curv_idx', []) # incase were multiline
if not context_multi_line:
context_nurbs['curv_range'] = float_func(line_split[1]), float_func(line_split[2])
line_split[0:3] = [] # remove first 3 items
if strip_slash(line_split):
context_multi_line = 'curv'
else:
context_multi_line = ''
for i in line_split:
vert_loc_index = int(i)-1
if vert_loc_index < 0:
vert_loc_index= len(verts_loc) + vert_loc_index + 1
curv_idx.append(vert_loc_index)
elif line.startswith('parm') or context_multi_line == 'parm':
line_split= line.split()
if context_multi_line:
context_multi_line = ''
else:
context_parm = line_split[1]
line_split[0:2] = [] # remove first 2
if strip_slash(line_split):
context_multi_line = 'parm'
else:
context_multi_line = ''
if context_parm.lower() == 'u':
context_nurbs.setdefault('parm_u', []).extend( [float_func(f) for f in line_split] )
elif context_parm.lower() == 'v': # surfaces not suported yet
context_nurbs.setdefault('parm_v', []).extend( [float_func(f) for f in line_split] )
# else: # may want to support other parm's ?
elif line.startswith('deg '):
context_nurbs['deg']= [int(i) for i in line.split()[1:]]
elif line.startswith('end'):
# Add the nurbs curve
if context_object:
context_nurbs['name'] = context_object
nurbs.append(context_nurbs)
context_nurbs = {}
context_parm = ''
''' # How to use usemap? depricated?
elif line.startswith('usema'): # usemap or usemat
context_image= line_value(line.split())
'''
file.close()
time_new= time.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= time.time()
# time_new= sys.time()
print('%.4f sec' % (time_new-time_sub))
time_sub= time_new
if not ROTATE_X90:
verts_loc[:] = [(v[0], v[2], -v[1]) for v in verts_loc]
# deselect all
bpy.ops.object.select_all(action='DESELECT')
scene = context.scene
# 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) ))
# Split the mesh by objects/materials, may
if SPLIT_OBJECTS or SPLIT_GROUPS: SPLIT_OB_OR_GROUP = True
else: SPLIT_OB_OR_GROUP = False
for verts_loc_split, faces_split, unique_materials_split, dataname in split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP):
# Create meshes from the data, warning 'vertex_groups' wont support splitting
create_mesh(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:
create_nurbs(context_nurbs, verts_loc, new_objects)
# Create new obj
for obj in new_objects:
base = scene.objects.link(obj)
base.select = True
scene.update()
axis_min= [ 1000000000]*3
axis_max= [-1000000000]*3
# if CLAMP_SIZE:
# # Get all object bounds
# for ob in new_objects:
# for v in ob.getBoundBox():
# for axis, value in enumerate(v):
# if axis_min[axis] > value: axis_min[axis]= value
# if axis_max[axis] < value: axis_max[axis]= value
# # Scale objects
# max_axis= max(axis_max[0]-axis_min[0], axis_max[1]-axis_min[1], axis_max[2]-axis_min[2])
# scale= 1.0
# while CLAMP_SIZE < max_axis * scale:
# scale= scale/10.0
# for ob in new_objects:
# ob.setSize(scale, scale, scale)
# Better rotate the vert locations
#if not ROTATE_X90:
# for ob in new_objects:
# ob.RotX = -1.570796326794896558
time_new= time.time()
# time_new= sys.time()
print('finished importing: %r in %.4f sec.' % (filepath, (time_new-time_main)))
return {'FINISHED'}
# NOTES (all line numbers refer to 2.4x import_obj.py, not this file)
# check later: line 489
# can convert now: edge flags, edges: lines 508-528
# ngon (uses python module BPyMesh): 384-414
# 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 bpy.sys.time()
if __name__ == "__main__":
register()