~ubuntu-branches/ubuntu/lucid/blender/lucid

« back to all changes in this revision

Viewing changes to release/scripts/import_obj.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2009-08-06 22:32:19 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806223219-8z4eej1u8levu4pz
Tags: 2.49a+dfsg-0ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: Build-depend on python-2.6 rather than python-2.5.
  - debian/misc/*.desktop: Add Spanish translation to .desktop 
    files.
  - debian/pyversions: 2.6.
  - debian/rules: Clean *.o of source/blender/python/api2_2x/
* New upstream release (LP: #382153).
* Refreshed patches:
  - 01_sanitize_sys.patch
  - 02_tmp_in_HOME
  - 10_use_systemwide_ftgl
  - 70_portability_platform_detection
* Removed patches merged upstream:
  - 30_fix_python_syntax_warning
  - 90_ubuntu_ffmpeg_52_changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
"""
4
4
Name: 'Wavefront (.obj)...'
5
 
Blender: 248
 
5
Blender: 249
6
6
Group: 'Import'
7
7
Tooltip: 'Load a Wavefront OBJ File, Shift: batch import all dir.'
8
8
"""
9
9
 
10
 
__author__= "Campbell Barton", "Jiri Hnidek"
 
10
__author__= "Campbell Barton", "Jiri Hnidek", "Paolo Ciccone"
11
11
__url__= ['http://wiki.blender.org/index.php/Scripts/Manual/Import/wavefront_obj', 'blender.org', 'blenderartists.org']
12
 
__version__= "2.1"
 
12
__version__= "2.12"
13
13
 
14
14
__bpydoc__= """\
15
15
This script imports a Wavefront OBJ files to Blender.
21
21
 
22
22
# ***** BEGIN GPL LICENSE BLOCK *****
23
23
#
24
 
# Script copyright (C) Campbell J Barton 2007
 
24
# Script copyright (C) Campbell J Barton 2007-2009
 
25
# - V2.12- bspline import/export added (funded by PolyDimensions GmbH)
25
26
#
26
27
# This program is free software; you can redistribute it and/or
27
28
# modify it under the terms of the GNU General Public License
40
41
# ***** END GPL LICENCE BLOCK *****
41
42
# --------------------------------------------------------------------------
42
43
 
43
 
from Blender import *
 
44
from Blender import Mesh, Draw, Window, Texture, Material, sys
44
45
import bpy
45
46
import BPyMesh
46
47
import BPyImage
49
50
try:            import os
50
51
except:         os= False
51
52
 
52
 
 
53
53
# Generic path functions
54
54
def stripFile(path):
55
55
        '''Return directory, where the file is'''
167
167
        del temp_mtl
168
168
        
169
169
        #Create new materials
170
 
        for name in unique_materials.iterkeys():
171
 
                unique_materials[name]= bpy.data.materials.new(name)
172
 
                
173
 
                unique_material_images[name]= None, False # assign None to all material images to start with, add to later.
 
170
        for name in unique_materials: # .keys()
 
171
                if name != None:
 
172
                        unique_materials[name]= bpy.data.materials.new(name)
 
173
                        unique_material_images[name]= None, False # assign None to all material images to start with, add to later.
174
174
                
175
175
        unique_materials[None]= None
 
176
        unique_material_images[None]= None, False
176
177
        
177
178
        for libname in material_libs:
178
179
                mtlpath= DIR + libname
320
321
        return [(value[0], value[1], value[2], key_to_name(key)) for key, value in face_split_dict.iteritems()]
321
322
 
322
323
 
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):
324
325
        '''
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
424
425
                                        sharp_edges[key]= None
425
426
        
426
427
        
427
 
        # mat the material names to an index
428
 
        material_mapping= dict([(name, i) for i, name in enumerate(unique_materials.keys())])
 
428
        # map the material names to an index
 
429
        material_mapping= dict([(name, i) for i, name in enumerate(unique_materials)]) # enumerate over unique_materials keys()
429
430
        
430
431
        materials= [None] * len(unique_materials)
431
432
        
530
531
        ob= scn.objects.new(me)
531
532
        new_objects.append(ob)
532
533
 
 
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)
 
540
 
 
541
 
 
542
def create_nurbs(scn, context_nurbs, vert_loc, new_objects):
 
543
        '''
 
544
        Add nurbs object to blender, only support one type at the moment
 
545
        '''
 
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)
 
553
        
 
554
        if cstype == None:
 
555
                print '\tWarning, cstype not found'
 
556
                return
 
557
        if cstype != 'bspline':
 
558
                print '\tWarning, cstype is not supported (only bspline)'
 
559
                return
 
560
        if not curv_idx:
 
561
                print '\tWarning, curv argument empty or not set'
 
562
                return
 
563
        if len(deg) > 1 or parm_v:
 
564
                print '\tWarning, surfaces not supported'
 
565
                return
 
566
        
 
567
        cu = bpy.data.curves.new(name, 'Curve')
 
568
        cu.flag |= 1 # 3D curve
 
569
        
 
570
        nu = None
 
571
        for pt in curv_idx:
 
572
                
 
573
                pt = vert_loc[pt]
 
574
                pt = (pt[0], pt[1], pt[2], 1.0)
 
575
                
 
576
                if nu == None:
 
577
                        nu = cu.appendNurb(pt)
 
578
                else:
 
579
                        nu.append(pt)
 
580
        
 
581
        nu.orderU = deg[0]+1
 
582
        
 
583
        # get for endpoint flag from the weighting
 
584
        if curv_range and len(parm_u) > deg[0]+1:
 
585
                do_endpoints = True
 
586
                for i in xrange(deg[0]+1):
 
587
                        
 
588
                        if abs(parm_u[i]-curv_range[0]) > 0.0001:
 
589
                                do_endpoints = False
 
590
                                break
 
591
                        
 
592
                        if abs(parm_u[-(i+1)]-curv_range[1]) > 0.0001:
 
593
                                do_endpoints = False
 
594
                                break
 
595
                        
 
596
        else:
 
597
                do_endpoints = False
 
598
        
 
599
        if do_endpoints:
 
600
                nu.flagU |= 2
 
601
        
 
602
        
 
603
        # close
 
604
        '''
 
605
        do_closed = False
 
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)]
 
609
                        
 
610
                        if curv_idx[i]==curv_idx[-(i+1)]:
 
611
                                do_closed = True
 
612
                                break
 
613
        
 
614
        if do_closed:
 
615
                nu.flagU |= 1
 
616
        '''
 
617
        
 
618
        ob = scn.objects.new(cu)
 
619
        new_objects.append(ob)
 
620
 
 
621
 
 
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
 
626
                else:
 
627
                        line_split[-1]= line_split[-1][:-1] # remove the \ from the end last number
 
628
                return True
 
629
        return False
 
630
 
 
631
 
 
632
 
533
633
def get_float_func(filepath):
534
634
        '''
535
635
        find the float function for this obj file
537
637
        '''
538
638
        file= open(filepath, 'rU')
539
639
        for line in file: #.xreadlines():
 
640
                line = line.lstrip()
540
641
                if line.startswith('v'): # vn vt v 
541
642
                        if ',' in line:
542
643
                                return lambda f: float(f.replace(',', '.'))
546
647
        # incase all vert values were ints 
547
648
        return float
548
649
 
549
 
def load_obj(filepath, CLAMP_SIZE= 0.0, CREATE_FGONS= True, CREATE_SMOOTH_GROUPS= True, CREATE_EDGES= True, SPLIT_OBJECTS= True, SPLIT_GROUPS= True, SPLIT_MATERIALS= True, IMAGE_SEARCH=True):
 
650
def load_obj(filepath,
 
651
                                                 CLAMP_SIZE= 0.0, 
 
652
                                                 CREATE_FGONS= True, 
 
653
                                                 CREATE_SMOOTH_GROUPS= True, 
 
654
                                                 CREATE_EDGES= True, 
 
655
                                                 SPLIT_OBJECTS= True, 
 
656
                                                 SPLIT_GROUPS= True, 
 
657
                                                 SPLIT_MATERIALS= True, 
 
658
                                                 ROTATE_X90= True, 
 
659
                                                 IMAGE_SEARCH=True,
 
660
                                                 POLYGROUPS=False):
550
661
        '''
551
662
        Called by the user interface or another script.
552
663
        load_obj(path) - should give acceptable results.
555
666
        '''
556
667
        print '\nimporting obj "%s"' % filepath
557
668
        
 
669
        if SPLIT_OBJECTS or SPLIT_GROUPS or SPLIT_MATERIALS:
 
670
                POLYGROUPS = False
 
671
        
558
672
        time_main= sys.time()
559
673
        
560
674
        verts_loc= []
561
675
        verts_tex= []
562
676
        faces= [] # tuples of the faces
563
677
        material_libs= [] # filanems to material libs this uses
564
 
        
 
678
        vertex_groups = {} # when POLYGROUPS is true
565
679
        
566
680
        # Get the string to float conversion func for this file- is 'float' for almost all files.
567
681
        float_func= get_float_func(filepath)
570
684
        context_material= None
571
685
        context_smooth_group= None
572
686
        context_object= None
 
687
        context_vgroup = None
573
688
        
 
689
        # Nurbs
 
690
        context_nurbs = {}
 
691
        nurbs = []
 
692
        context_parm = '' # used by nurbs too but could be used elsewhere
 
693
 
574
694
        has_ngons= False
575
695
        # has_smoothgroups= False - is explicit with len(unique_smooth_groups) being > 0
576
696
        
584
704
        # it means they are multiline- 
585
705
        # since we use xreadline we cant skip to the next line
586
706
        # so we need to know weather 
587
 
        multi_line_face= False
 
707
        context_multi_line= ''
588
708
        
589
 
        print '\tpassing obj file "%s"...' % filepath,
 
709
        print '\tparsing obj file "%s"...' % filepath,
590
710
        time_sub= sys.time()
 
711
 
591
712
        file= open(filepath, 'rU')
592
713
        for line in file: #.xreadlines():
 
714
                line = line.lstrip() # rare cases there is white space at the start of the line
593
715
                
594
716
                if line.startswith('v '):
595
717
                        line_split= line.split()
605
727
                
606
728
                # Handel faces lines (as faces) and the second+ lines of fa multiline face here
607
729
                # use 'f' not 'f ' because some objs (very rare have 'fo ' for faces)
608
 
                elif line.startswith('f') or (line.startswith('l ') and CREATE_EDGES) or multi_line_face:
 
730
                elif line.startswith('f') or context_multi_line == 'f':
609
731
                        
610
 
                        if multi_line_face:
 
732
                        if context_multi_line:
611
733
                                # use face_vert_loc_indicies and face_vert_tex_indicies previously defined and used the obj_face
612
734
                                line_split= line.split()
613
 
                                multi_line_face= False
614
735
                                
615
736
                        else:
616
737
                                line_split= line[2:].split()
626
747
                                context_object\
627
748
                                ))
628
749
                        
629
 
                        if line_split[-1][-1]== '\\':
630
 
                                multi_line_face= True
631
 
                                if len(line_split[-1])==1:
632
 
                                        line_split.pop() # remove the \ item
633
 
                                else:
634
 
                                        line_split[-1]= line_split[-1][:-1] # remove the \ from the end last number
635
 
                        
636
 
                        isline= line.startswith('l')
 
750
                        if strip_slash(line_split):
 
751
                                context_multi_line = 'f'
 
752
                        else:
 
753
                                context_multi_line = ''
637
754
                        
638
755
                        for v in line_split:
639
756
                                obj_vert= v.split('/')
640
757
                                
641
758
                                vert_loc_index= int(obj_vert[0])-1
 
759
                                # Add the vertex to the current group
 
760
                                # *warning*, this wont work for files that have groups defined around verts
 
761
                                if      POLYGROUPS and context_vgroup:
 
762
                                        vertex_groups[context_vgroup].append(vert_loc_index)
642
763
                                
643
764
                                # Make relative negative vert indicies absolute
644
765
                                if vert_loc_index < 0:
646
767
                                
647
768
                                face_vert_loc_indicies.append(vert_loc_index)
648
769
                                
649
 
                                if not isline:
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
653
 
                                                
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
658
 
                                                
659
 
                                                face_vert_tex_indicies.append(vert_tex_index)
660
 
                                        else:
661
 
                                                # dummy
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
 
773
                                        
 
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
 
778
                                        
 
779
                                        face_vert_tex_indicies.append(vert_tex_index)
 
780
                                else:
 
781
                                        # dummy
 
782
                                        face_vert_tex_indicies.append(0)
663
783
                        
664
784
                        if len(face_vert_loc_indicies) > 4:
665
785
                                has_ngons= True
666
 
                        
 
786
                
 
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
 
789
                        
 
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()
 
793
                                
 
794
                        else:
 
795
                                line_split= line[2:].split()
 
796
                                face_vert_loc_indicies= []
 
797
                                face_vert_tex_indicies= []
 
798
                                
 
799
                                # Instance a face
 
800
                                faces.append((\
 
801
                                face_vert_loc_indicies,\
 
802
                                face_vert_tex_indicies,\
 
803
                                context_material,\
 
804
                                context_smooth_group,\
 
805
                                context_object\
 
806
                                ))
 
807
                        
 
808
                        if strip_slash(line_split):
 
809
                                context_multi_line = 'l'
 
810
                        else:
 
811
                                context_multi_line = ''
 
812
                        
 
813
                        isline= line.startswith('l')
 
814
                        
 
815
                        for v in line_split:
 
816
                                vert_loc_index= int(v)-1
 
817
                                
 
818
                                # Make relative negative vert indicies absolute
 
819
                                if vert_loc_index < 0:
 
820
                                        vert_loc_index= len(verts_loc) + vert_loc_index + 1
 
821
                                
 
822
                                face_vert_loc_indicies.append(vert_loc_index)
 
823
                
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
 
842
                        elif POLYGROUPS:
 
843
                                context_vgroup = line_value(line.split())
 
844
                                if context_vgroup and context_vgroup != '(null)':
 
845
                                        vertex_groups.setdefault(context_vgroup, [])
 
846
                                else:
 
847
                                        context_vgroup = None # dont assign a vgroup
685
848
                
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
 
854
                        
 
855
                        
 
856
                        # Nurbs support
 
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()
 
861
                        
 
862
                        curv_idx = context_nurbs['curv_idx'] = context_nurbs.get('curv_idx', []) # incase were multiline
 
863
                        
 
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
 
867
                        
 
868
                        if strip_slash(line_split):
 
869
                                context_multi_line = 'curv'
 
870
                        else:
 
871
                                context_multi_line = ''
 
872
                                
 
873
                        
 
874
                        for i in line_split:
 
875
                                vert_loc_index = int(i)-1
 
876
                                
 
877
                                if vert_loc_index < 0:
 
878
                                        vert_loc_index= len(verts_loc) + vert_loc_index + 1
 
879
                                
 
880
                                curv_idx.append(vert_loc_index)
 
881
                        
 
882
                elif line.startswith('parm') or context_multi_line == 'parm':
 
883
                        line_split= line.split()
 
884
                        
 
885
                        if context_multi_line:
 
886
                                context_multi_line = ''
 
887
                        else:
 
888
                                context_parm = line_split[1]
 
889
                                line_split[0:2] = [] # remove first 2
 
890
                        
 
891
                        if strip_slash(line_split):
 
892
                                context_multi_line = 'parm'
 
893
                        else:
 
894
                                context_multi_line = ''
 
895
                        
 
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 ?
 
901
                
 
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
 
906
                        if context_object:
 
907
                                context_nurbs['name'] = context_object
 
908
                        nurbs.append(context_nurbs)
 
909
                        context_nurbs = {}
 
910
                        context_parm = ''
691
911
                
692
912
                ''' # How to use usemap? depricated?
693
913
                elif line.startswith('usema'): # usemap or usemat
707
927
        print '%.4f sec' % (time_new-time_sub)
708
928
        time_sub= time_new
709
929
        
 
930
        if not ROTATE_X90:
 
931
                verts_loc[:] = [(v[0], v[2], -v[1]) for v in verts_loc]
710
932
        
711
933
        # deselect all
712
934
        scn = bpy.data.scenes.active
719
941
        else:                                                           SPLIT_OB_OR_GROUP = False
720
942
        
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)
 
946
        
 
947
        # nurbs support
 
948
        for context_nurbs in nurbs:
 
949
                create_nurbs(scn, context_nurbs, verts_loc, new_objects)
 
950
        
724
951
        
725
952
        axis_min= [ 1000000000]*3
726
953
        axis_max= [-1000000000]*3
743
970
                for ob in new_objects:
744
971
                        ob.setSize(scale, scale, scale)
745
972
        
 
973
        # Better rotate the vert locations
 
974
        #if not ROTATE_X90:
 
975
        #       for ob in new_objects:
 
976
        #               ob.RotX = -1.570796326794896558
 
977
        
746
978
        time_new= sys.time()
747
979
        
748
980
        print '%.4f sec' % (time_new-time_sub)
756
988
        if BPyMessages.Error_NoFile(filepath):
757
989
                return
758
990
        
759
 
        global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, CLAMP_SIZE, IMAGE_SEARCH, KEEP_VERT_ORDER
 
991
        global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, CLAMP_SIZE, IMAGE_SEARCH, POLYGROUPS, KEEP_VERT_ORDER, ROTATE_X90
760
992
        
761
993
        CREATE_SMOOTH_GROUPS= Draw.Create(0)
762
994
        CREATE_FGONS= Draw.Create(1)
766
998
        SPLIT_MATERIALS= Draw.Create(0)
767
999
        CLAMP_SIZE= Draw.Create(10.0)
768
1000
        IMAGE_SEARCH= Draw.Create(1)
 
1001
        POLYGROUPS= Draw.Create(0)
769
1002
        KEEP_VERT_ORDER= Draw.Create(1)
 
1003
        ROTATE_X90= Draw.Create(1)
770
1004
        
771
1005
        
772
1006
        # Get USER Options
815
1049
                        GLOBALS['EVENT'] = e
816
1050
                
817
1051
                def do_split(e,v):
818
 
                        global SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, KEEP_VERT_ORDER
 
1052
                        global SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, KEEP_VERT_ORDER, POLYGROUPS
819
1053
                        if SPLIT_OBJECTS.val or SPLIT_GROUPS.val or SPLIT_MATERIALS.val:
820
1054
                                KEEP_VERT_ORDER.val = 0
 
1055
                                POLYGROUPS.val = 0
821
1056
                        else:
822
1057
                                KEEP_VERT_ORDER.val = 1
823
1058
                        
829
1064
                                if not (SPLIT_OBJECTS.val or SPLIT_GROUPS.val or SPLIT_MATERIALS.val):
830
1065
                                        KEEP_VERT_ORDER.val = 1
831
1066
                        
 
1067
                def do_polygroups(e,v):
 
1068
                        global SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, KEEP_VERT_ORDER, POLYGROUPS
 
1069
                        if POLYGROUPS.val:
 
1070
                                SPLIT_OBJECTS.val = SPLIT_GROUPS.val = SPLIT_MATERIALS.val = 0
 
1071
                        
832
1072
                def do_help(e,v):
833
1073
                        url = __url__[0]
834
1074
                        print 'Trying to open web browser with documentation at this address...'
847
1087
                        ui_x -= 165
848
1088
                        ui_y -= 90
849
1089
                        
850
 
                        global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, CLAMP_SIZE, IMAGE_SEARCH, KEEP_VERT_ORDER
 
1090
                        global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, CLAMP_SIZE, IMAGE_SEARCH, POLYGROUPS, KEEP_VERT_ORDER, ROTATE_X90
851
1091
                        
852
1092
                        Draw.Label('Import...', ui_x+9, ui_y+159, 220, 21)
853
1093
                        Draw.BeginAlign()
858
1098
                        
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)
864
1104
                        Draw.EndAlign()
865
1105
                        
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)
 
1108
                        
 
1109
                        ROTATE_X90 = Draw.Toggle('-X90', EVENT_REDRAW, ui_x+302, ui_y+89, 38, 21, ROTATE_X90.val, 'Rotate X 90.')
868
1110
                        
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)
918
1161
                          SPLIT_OBJECTS.val,\
919
1162
                          SPLIT_GROUPS.val,\
920
1163
                          SPLIT_MATERIALS.val,\
 
1164
                          ROTATE_X90.val,\
921
1165
                          IMAGE_SEARCH.val,\
 
1166
                          POLYGROUPS.val
922
1167
                        )
923
1168
        
924
1169
        else: # Normal load
930
1175
                  SPLIT_OBJECTS.val,\
931
1176
                  SPLIT_GROUPS.val,\
932
1177
                  SPLIT_MATERIALS.val,\
 
1178
                  ROTATE_X90.val,\
933
1179
                  IMAGE_SEARCH.val,\
 
1180
                  POLYGROUPS.val
934
1181
                )
935
1182
        
936
1183
        Window.WaitCursor(0)
947
1194
        else:
948
1195
                Window.FileSelector(load_obj_ui, 'Import a Wavefront OBJ', '*.obj')
949
1196
 
950
 
 
 
1197
        # For testing compatibility
951
1198
'''
952
 
# For testing compatibility
953
1199
else:
954
1200
        # DEBUG ONLY
955
1201
        TIME= sys.time()
 
1202
        DIR = '/fe/obj'
956
1203
        import os
957
1204
        print 'Searching for files'
958
 
        os.system('find /fe/obj -iname "*.obj" > /tmp/temp3ds_list')
959
 
        
960
 
        print '...Done'
961
 
        file= open('/tmp/temp3ds_list', 'rU')
962
 
        lines= file.readlines()
963
 
        file.close()
964
 
 
965
 
        def between(v,a,b):
966
 
                if v <= max(a,b) and v >= min(a,b):
967
 
                        return True             
968
 
                return False
969
 
                
970
 
        for i, _obj in enumerate(lines):
971
 
                if between(i, 0,20):
972
 
                        _obj= _obj[:-1]
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)
 
1205
        def fileList(path):
 
1206
                for dirpath, dirnames, filenames in os.walk(path):
 
1207
                        for filename in filenames:
 
1208
                                yield os.path.join(dirpath, filename)
 
1209
        
 
1210
        files = [f for f in fileList(DIR) if f.lower().endswith('.obj')]
 
1211
        files.sort()
 
1212
        
 
1213
        for i, obj_file in enumerate(files):
 
1214
                if 0 < i < 20:
 
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)
978
1219
 
979
1220
        print 'TOTAL TIME: %.6f' % (sys.time() - TIME)
980
1221
'''