320
321
return [(value[0], value[1], value[2], key_to_name(key)) for key, value in face_split_dict.iteritems()]
323
def create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc, verts_tex, faces, unique_materials, unique_material_images, unique_smooth_groups, dataname):
324
def create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc, verts_tex, faces, unique_materials, unique_material_images, unique_smooth_groups, vertex_groups, dataname):
325
326
Takes all the data gathered and generates a mesh, adding the new object to new_objects
326
327
deals with fgons, sharp edges and assigning materials
530
531
ob= scn.objects.new(me)
531
532
new_objects.append(ob)
534
# Create the vertex groups. No need to have the flag passed here since we test for the
535
# content of the vertex_groups. If the user selects to NOT have vertex groups saved then
536
# the following test will never run
537
for group_name, group_indicies in vertex_groups.iteritems():
538
me.addVertGroup(group_name)
539
me.assignVertsToGroup(group_name, group_indicies,1.00, Mesh.AssignModes.REPLACE)
542
def create_nurbs(scn, context_nurbs, vert_loc, new_objects):
544
Add nurbs object to blender, only support one type at the moment
546
deg = context_nurbs.get('deg', (3,))
547
curv_range = context_nurbs.get('curv_range', None)
548
curv_idx = context_nurbs.get('curv_idx', [])
549
parm_u = context_nurbs.get('parm_u', [])
550
parm_v = context_nurbs.get('parm_v', [])
551
name = context_nurbs.get('name', 'ObjNurb')
552
cstype = context_nurbs.get('cstype', None)
555
print '\tWarning, cstype not found'
557
if cstype != 'bspline':
558
print '\tWarning, cstype is not supported (only bspline)'
561
print '\tWarning, curv argument empty or not set'
563
if len(deg) > 1 or parm_v:
564
print '\tWarning, surfaces not supported'
567
cu = bpy.data.curves.new(name, 'Curve')
568
cu.flag |= 1 # 3D curve
574
pt = (pt[0], pt[1], pt[2], 1.0)
577
nu = cu.appendNurb(pt)
583
# get for endpoint flag from the weighting
584
if curv_range and len(parm_u) > deg[0]+1:
586
for i in xrange(deg[0]+1):
588
if abs(parm_u[i]-curv_range[0]) > 0.0001:
592
if abs(parm_u[-(i+1)]-curv_range[1]) > 0.0001:
606
if len(parm_u) > deg[0]+1:
607
for i in xrange(deg[0]+1):
608
#print curv_idx[i], curv_idx[-(i+1)]
610
if curv_idx[i]==curv_idx[-(i+1)]:
618
ob = scn.objects.new(cu)
619
new_objects.append(ob)
622
def strip_slash(line_split):
623
if line_split[-1][-1]== '\\':
624
if len(line_split[-1])==1:
625
line_split.pop() # remove the \ item
627
line_split[-1]= line_split[-1][:-1] # remove the \ from the end last number
533
633
def get_float_func(filepath):
535
635
find the float function for this obj file
647
768
face_vert_loc_indicies.append(vert_loc_index)
650
if len(obj_vert)>1 and obj_vert[1]:
651
# formatting for faces with normals and textures us
652
# loc_index/tex_index/nor_index
654
vert_tex_index= int(obj_vert[1])-1
655
# Make relative negative vert indicies absolute
656
if vert_tex_index < 0:
657
vert_tex_index= len(verts_tex) + vert_tex_index + 1
659
face_vert_tex_indicies.append(vert_tex_index)
662
face_vert_tex_indicies.append(0)
770
if len(obj_vert)>1 and obj_vert[1]:
771
# formatting for faces with normals and textures us
772
# loc_index/tex_index/nor_index
774
vert_tex_index= int(obj_vert[1])-1
775
# Make relative negative vert indicies absolute
776
if vert_tex_index < 0:
777
vert_tex_index= len(verts_tex) + vert_tex_index + 1
779
face_vert_tex_indicies.append(vert_tex_index)
782
face_vert_tex_indicies.append(0)
664
784
if len(face_vert_loc_indicies) > 4:
787
elif CREATE_EDGES and (line.startswith('l ') or context_multi_line == 'l'):
788
# very similar to the face load function above with some parts removed
790
if context_multi_line:
791
# use face_vert_loc_indicies and face_vert_tex_indicies previously defined and used the obj_face
792
line_split= line.split()
795
line_split= line[2:].split()
796
face_vert_loc_indicies= []
797
face_vert_tex_indicies= []
801
face_vert_loc_indicies,\
802
face_vert_tex_indicies,\
804
context_smooth_group,\
808
if strip_slash(line_split):
809
context_multi_line = 'l'
811
context_multi_line = ''
813
isline= line.startswith('l')
816
vert_loc_index= int(v)-1
818
# Make relative negative vert indicies absolute
819
if vert_loc_index < 0:
820
vert_loc_index= len(verts_loc) + vert_loc_index + 1
822
face_vert_loc_indicies.append(vert_loc_index)
667
824
elif line.startswith('s'):
668
825
if CREATE_SMOOTH_GROUPS:
669
826
context_smooth_group= line_value(line.split())
682
839
context_object= line_value(line.split())
683
840
# print 'context_object', context_object
684
841
# unique_obects[context_object]= None
843
context_vgroup = line_value(line.split())
844
if context_vgroup and context_vgroup != '(null)':
845
vertex_groups.setdefault(context_vgroup, [])
847
context_vgroup = None # dont assign a vgroup
686
849
elif line.startswith('usemtl'):
687
850
context_material= line_value(line.split())
688
851
unique_materials[context_material]= None
689
852
elif line.startswith('mtllib'): # usemap or usemat
690
853
material_libs.extend( line.split()[1:] ) # can have multiple mtllib filenames per line
857
elif line.startswith('cstype '):
858
context_nurbs['cstype']= line_value(line.split()) # 'rat bspline' / 'bspline'
859
elif line.startswith('curv ') or context_multi_line == 'curv':
860
line_split= line.split()
862
curv_idx = context_nurbs['curv_idx'] = context_nurbs.get('curv_idx', []) # incase were multiline
864
if not context_multi_line:
865
context_nurbs['curv_range'] = float_func(line_split[1]), float_func(line_split[2])
866
line_split[0:3] = [] # remove first 3 items
868
if strip_slash(line_split):
869
context_multi_line = 'curv'
871
context_multi_line = ''
875
vert_loc_index = int(i)-1
877
if vert_loc_index < 0:
878
vert_loc_index= len(verts_loc) + vert_loc_index + 1
880
curv_idx.append(vert_loc_index)
882
elif line.startswith('parm') or context_multi_line == 'parm':
883
line_split= line.split()
885
if context_multi_line:
886
context_multi_line = ''
888
context_parm = line_split[1]
889
line_split[0:2] = [] # remove first 2
891
if strip_slash(line_split):
892
context_multi_line = 'parm'
894
context_multi_line = ''
896
if context_parm.lower() == 'u':
897
context_nurbs.setdefault('parm_u', []).extend( [float_func(f) for f in line_split] )
898
elif context_parm.lower() == 'v': # surfaces not suported yet
899
context_nurbs.setdefault('parm_v', []).extend( [float_func(f) for f in line_split] )
900
# else: # may want to support other parm's ?
902
elif line.startswith('deg '):
903
context_nurbs['deg']= [int(i) for i in line.split()[1:]]
904
elif line.startswith('end'):
905
# Add the nurbs curve
907
context_nurbs['name'] = context_object
908
nurbs.append(context_nurbs)
692
912
''' # How to use usemap? depricated?
693
913
elif line.startswith('usema'): # usemap or usemat
719
941
else: SPLIT_OB_OR_GROUP = False
721
943
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):
722
# Create meshes from the data
723
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, dataname)
944
# Create meshes from the data, warning 'vertex_groups' wont support splitting
945
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)
948
for context_nurbs in nurbs:
949
create_nurbs(scn, context_nurbs, verts_loc, new_objects)
725
952
axis_min= [ 1000000000]*3
726
953
axis_max= [-1000000000]*3
859
1099
Draw.Label('Separate objects by OBJ...', ui_x+9, ui_y+110, 220, 20)
860
1100
Draw.BeginAlign()
861
SPLIT_OBJECTS = Draw.Toggle('Object', EVENT_REDRAW, ui_x+9, ui_y+89, 70, 21, SPLIT_OBJECTS.val, 'Import OBJ Objects into Blender Objects', do_split)
862
SPLIT_GROUPS = Draw.Toggle('Group', EVENT_REDRAW, ui_x+79, ui_y+89, 70, 21, SPLIT_GROUPS.val, 'Import OBJ Groups into Blender Objects', do_split)
863
SPLIT_MATERIALS = Draw.Toggle('Material', EVENT_REDRAW, ui_x+149, ui_y+89, 70, 21, SPLIT_MATERIALS.val, 'Import each material into a seperate mesh (Avoids > 16 per mesh error)', do_split)
1101
SPLIT_OBJECTS = Draw.Toggle('Object', EVENT_REDRAW, ui_x+9, ui_y+89, 55, 21, SPLIT_OBJECTS.val, 'Import OBJ Objects into Blender Objects', do_split)
1102
SPLIT_GROUPS = Draw.Toggle('Group', EVENT_REDRAW, ui_x+64, ui_y+89, 55, 21, SPLIT_GROUPS.val, 'Import OBJ Groups into Blender Objects', do_split)
1103
SPLIT_MATERIALS = Draw.Toggle('Material', EVENT_REDRAW, ui_x+119, ui_y+89, 60, 21, SPLIT_MATERIALS.val, 'Import each material into a seperate mesh (Avoids > 16 per mesh error)', do_split)
866
1106
# Only used for user feedback
867
KEEP_VERT_ORDER = Draw.Toggle('Keep Vert Order', EVENT_REDRAW, ui_x+229, ui_y+89, 110, 21, KEEP_VERT_ORDER.val, 'Keep vert and face order, disables split options, enable for morph targets', do_vertorder)
1107
KEEP_VERT_ORDER = Draw.Toggle('Keep Vert Order', EVENT_REDRAW, ui_x+184, ui_y+89, 113, 21, KEEP_VERT_ORDER.val, 'Keep vert and face order, disables split options, enable for morph targets', do_vertorder)
1109
ROTATE_X90 = Draw.Toggle('-X90', EVENT_REDRAW, ui_x+302, ui_y+89, 38, 21, ROTATE_X90.val, 'Rotate X 90.')
869
1111
Draw.Label('Options...', ui_x+9, ui_y+60, 211, 20)
870
CLAMP_SIZE = Draw.Number('Clamp Scale: ', EVENT_NONE, ui_x+9, ui_y+39, 211, 21, CLAMP_SIZE.val, 0.0, 1000.0, 'Clamp the size to this maximum (Zero to Disable)')
871
IMAGE_SEARCH = Draw.Toggle('Image Search', EVENT_NONE, ui_x+229, ui_y+39, 110, 21, IMAGE_SEARCH.val, 'Search subdirs for any assosiated images (Warning, may be slow)')
1112
CLAMP_SIZE = Draw.Number('Clamp Scale: ', EVENT_NONE, ui_x+9, ui_y+39, 130, 21, CLAMP_SIZE.val, 0.0, 1000.0, 'Clamp the size to this maximum (Zero to Disable)')
1113
POLYGROUPS = Draw.Toggle('Poly Groups', EVENT_REDRAW, ui_x+144, ui_y+39, 90, 21, POLYGROUPS.val, 'Import OBJ groups as vertex groups.', do_polygroups)
1114
IMAGE_SEARCH = Draw.Toggle('Image Search', EVENT_NONE, ui_x+239, ui_y+39, 100, 21, IMAGE_SEARCH.val, 'Search subdirs for any assosiated images (Warning, may be slow)')
872
1115
Draw.BeginAlign()
873
1116
Draw.PushButton('Online Help', EVENT_REDRAW, ui_x+9, ui_y+9, 110, 21, 'Load the wiki page for this script', do_help)
874
1117
Draw.PushButton('Cancel', EVENT_EXIT, ui_x+119, ui_y+9, 110, 21, '', obj_ui_set_event)
948
1195
Window.FileSelector(load_obj_ui, 'Import a Wavefront OBJ', '*.obj')
1197
# For testing compatibility
952
# For testing compatibility
955
1201
TIME= sys.time()
957
1204
print 'Searching for files'
958
os.system('find /fe/obj -iname "*.obj" > /tmp/temp3ds_list')
961
file= open('/tmp/temp3ds_list', 'rU')
962
lines= file.readlines()
966
if v <= max(a,b) and v >= min(a,b):
970
for i, _obj in enumerate(lines):
973
print 'Importing', _obj, '\nNUMBER', i, 'of', len(lines)
974
_obj_file= _obj.split('/')[-1].split('\\')[-1]
975
newScn= bpy.data.scenes.new(_obj_file)
1206
for dirpath, dirnames, filenames in os.walk(path):
1207
for filename in filenames:
1208
yield os.path.join(dirpath, filename)
1210
files = [f for f in fileList(DIR) if f.lower().endswith('.obj')]
1213
for i, obj_file in enumerate(files):
1215
print 'Importing', obj_file, '\nNUMBER', i, 'of', len(files)
1216
newScn= bpy.data.scenes.new(os.path.basename(obj_file))
976
1217
newScn.makeCurrent()
977
load_obj(_obj, False)
1218
load_obj(obj_file, False, IMAGE_SEARCH=0)
979
1220
print 'TOTAL TIME: %.6f' % (sys.time() - TIME)