[#22884] slow import wavefont/.obj file
authorCampbell Barton <ideasman42@gmail.com>
Fri, 13 Aug 2010 15:07:57 +0000 (15:07 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 13 Aug 2010 15:07:57 +0000 (15:07 +0000)
- dont run mesh update functions on zero user meshes (a bit of a cheap trick but works well), made in last commit by mistake.
- dont do a scenegraph update on linking an object, run scene.update() after linking objects.
- remove split material option since we have enough slots.

release/scripts/io/import_scene_obj.py
source/blender/makesrna/intern/rna_scene.c

index 3c976c4cf92185f8561f8a5b06d0f9ac6b4a01fe..03adc8be395099704430863319288efb467f0df9 100644 (file)
@@ -503,7 +503,7 @@ def create_materials(filepath, material_libs, unique_materials, unique_material_
 
 
 
-def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP, SPLIT_MATERIALS):
+def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP):
     '''
     Takes vert_loc and faces, and separates into multiple sets of
     (verts_loc, faces, unique_materials, dataname)
@@ -511,41 +511,24 @@ def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP,
 
     filename = os.path.splitext((os.path.basename(filepath)))[0]
 
-    if not SPLIT_OB_OR_GROUP and not SPLIT_MATERIALS:
+    if not SPLIT_OB_OR_GROUP:
         # 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:
+        if not key:
             return filename # assume its a string. make sure this is true if the splitting code is changed
         else:
             return key
 
     # Return a key that makes the faces unique.
-    if SPLIT_OB_OR_GROUP and not SPLIT_MATERIALS:
-        def face_key(face):
-            return face[4] # object
-
-    elif not SPLIT_OB_OR_GROUP and SPLIT_MATERIALS:
-        def face_key(face):
-            return face[2] # material
-
-    else: # Both
-        def face_key(face):
-            return face[4], face[2] # object,material
-
-
     face_split_dict= {}
 
     oldkey= -1 # initialize to a value that will never match the key
 
     for face in faces:
-
-        key= face_key(face)
+        key= face[4]
 
         if oldkey != key:
             # Check the key has changed.
@@ -570,7 +553,6 @@ def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP,
                 vert_remap[i]= new_index # set the new remapped index so we only add once and can reference next time.
                 face_vert_loc_indicies[enum] = new_index # remap to the local index
                 verts_split.append( verts_loc[i] ) # add the vert to the local verts
-
             else:
                 face_vert_loc_indicies[enum] = vert_remap[i] # remap to the local index
 
@@ -580,12 +562,11 @@ def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP,
 
         faces_split.append(face)
 
-
     # remove one of the itemas and reorder
     return [(value[0], value[1], value[2], key_to_name(key)) for key, value in list(face_split_dict.items())]
 
 
-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):
+def create_mesh(new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc, verts_tex, faces, unique_materials, unique_material_images, unique_smooth_groups, vertex_groups, dataname):
     '''
     Takes all the data gathered and generates a mesh, adding the new object to new_objects
     deals with fgons, sharp edges and assigning materials
@@ -844,7 +825,6 @@ def create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_l
 #      me.calcNormals()
 
     ob= bpy.data.objects.new("Mesh", me)
-    scn.objects.link(ob)
     new_objects.append(ob)
 
     # Create the vertex groups. No need to have the flag passed here since we test for the
@@ -858,7 +838,7 @@ def create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_l
 #              me.assignVertsToGroup(group_name, group_indicies, 1.00, Mesh.AssignModes.REPLACE)
 
 
-def create_nurbs(scn, context_nurbs, vert_loc, new_objects):
+def create_nurbs(context_nurbs, vert_loc, new_objects):
     '''
     Add nurbs object to blender, only support one type at the moment
     '''
@@ -933,8 +913,9 @@ def create_nurbs(scn, context_nurbs, vert_loc, new_objects):
     if do_closed:
         nu.flagU |= 1
     '''
+    
+    ob= bpy.data.objects.new("Mesh", me)
 
-    ob = scn.objects.new(cu)
     new_objects.append(ob)
 
 
@@ -1259,7 +1240,6 @@ def load_obj(filepath,
 #              bpy.ops.OBJECT_OT_select_all()
 
     scene = context.scene
-#      scn = bpy.data.scenes.active
 #      scn.objects.selected = []
     new_objects= [] # put new objects here
 
@@ -1268,14 +1248,20 @@ def load_obj(filepath,
     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, SPLIT_MATERIALS):
+    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(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)
+        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(scn, context_nurbs, verts_loc, new_objects)
 
+    # Create new obj
+    for obj in new_objects:
+        scene.objects.link(obj)
+
+    scene.update()
+
 
     axis_min= [ 1000000000]*3
     axis_max= [-1000000000]*3
@@ -1317,14 +1303,13 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
     if BPyMessages.Error_NoFile(filepath):
         return
 
-    global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, CLAMP_SIZE, IMAGE_SEARCH, POLYGROUPS, KEEP_VERT_ORDER, ROTATE_X90
+    global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, CLAMP_SIZE, IMAGE_SEARCH, POLYGROUPS, KEEP_VERT_ORDER, ROTATE_X90
 
     CREATE_SMOOTH_GROUPS= Draw.Create(0)
     CREATE_FGONS= Draw.Create(1)
     CREATE_EDGES= Draw.Create(1)
     SPLIT_OBJECTS= Draw.Create(0)
     SPLIT_GROUPS= Draw.Create(0)
-    SPLIT_MATERIALS= Draw.Create(0)
     CLAMP_SIZE= Draw.Create(10.0)
     IMAGE_SEARCH= Draw.Create(1)
     POLYGROUPS= Draw.Create(0)
@@ -1343,7 +1328,6 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
     'Separate objects from obj...',\
     ('Object', SPLIT_OBJECTS, 'Import OBJ Objects into Blender Objects'),\
     ('Group', SPLIT_GROUPS, 'Import OBJ Groups into Blender Objects'),\
-    ('Split Materials', SPLIT_MATERIALS, 'Import each material into a separate mesh'),\
     'Options...',\
     ('Keep Vert Order', KEEP_VERT_ORDER, 'Keep vert and face order, disables some other options.'),\
     ('Clamp Scale:', CLAMP_SIZE, 0.0, 1000.0, 'Clamp the size to this maximum (Zero to Disable)'),\
@@ -1356,7 +1340,6 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
     if KEEP_VERT_ORDER.val:
         SPLIT_OBJECTS.val = False
         SPLIT_GROUPS.val = False
-        SPLIT_MATERIALS.val = False
     '''
 
 
@@ -1378,25 +1361,25 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
             GLOBALS['EVENT'] = e
 
         def do_split(e,v):
-            global SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, KEEP_VERT_ORDER, POLYGROUPS
-            if SPLIT_OBJECTS.val or SPLIT_GROUPS.val or SPLIT_MATERIALS.val:
+            global SPLIT_OBJECTS, SPLIT_GROUPS, KEEP_VERT_ORDER, POLYGROUPS
+            if SPLIT_OBJECTS.val or SPLIT_GROUPS.val:
                 KEEP_VERT_ORDER.val = 0
                 POLYGROUPS.val = 0
             else:
                 KEEP_VERT_ORDER.val = 1
 
         def do_vertorder(e,v):
-            global SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, KEEP_VERT_ORDER
+            global SPLIT_OBJECTS, SPLIT_GROUPS, KEEP_VERT_ORDER
             if KEEP_VERT_ORDER.val:
-                SPLIT_OBJECTS.val = SPLIT_GROUPS.val = SPLIT_MATERIALS.val = 0
+                SPLIT_OBJECTS.val = SPLIT_GROUPS.val = 0
             else:
-                if not (SPLIT_OBJECTS.val or SPLIT_GROUPS.val or SPLIT_MATERIALS.val):
+                if not (SPLIT_OBJECTS.val or SPLIT_GROUPS.val):
                     KEEP_VERT_ORDER.val = 1
 
         def do_polygroups(e,v):
-            global SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, KEEP_VERT_ORDER, POLYGROUPS
+            global SPLIT_OBJECTS, SPLIT_GROUPS, KEEP_VERT_ORDER, POLYGROUPS
             if POLYGROUPS.val:
-                SPLIT_OBJECTS.val = SPLIT_GROUPS.val = SPLIT_MATERIALS.val = 0
+                SPLIT_OBJECTS.val = SPLIT_GROUPS.val = 0
 
         def do_help(e,v):
             url = __url__[0]
@@ -1416,7 +1399,7 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
             ui_x -= 165
             ui_y -= 90
 
-            global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, SPLIT_MATERIALS, CLAMP_SIZE, IMAGE_SEARCH, POLYGROUPS, KEEP_VERT_ORDER, ROTATE_X90
+            global CREATE_SMOOTH_GROUPS, CREATE_FGONS, CREATE_EDGES, SPLIT_OBJECTS, SPLIT_GROUPS, CLAMP_SIZE, IMAGE_SEARCH, POLYGROUPS, KEEP_VERT_ORDER, ROTATE_X90
 
             Draw.Label('Import...', ui_x+9, ui_y+159, 220, 21)
             Draw.BeginAlign()
@@ -1429,7 +1412,6 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
             Draw.BeginAlign()
             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)
             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)
-            SPLIT_MATERIALS = Draw.Toggle('Split Materials', EVENT_REDRAW, ui_x+119, ui_y+89, 60, 21, SPLIT_MATERIALS.val, 'Import each material into a separate mesh', do_split)
             Draw.EndAlign()
 
             # Only used for user feedback
@@ -1489,7 +1471,6 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
               CREATE_EDGES.val,\
               SPLIT_OBJECTS.val,\
               SPLIT_GROUPS.val,\
-              SPLIT_MATERIALS.val,\
               ROTATE_X90.val,\
               IMAGE_SEARCH.val,\
               POLYGROUPS.val
@@ -1503,7 +1484,6 @@ def load_obj_ui(filepath, BATCH_LOAD= False):
           CREATE_EDGES.val,\
           SPLIT_OBJECTS.val,\
           SPLIT_GROUPS.val,\
-          SPLIT_MATERIALS.val,\
           ROTATE_X90.val,\
           IMAGE_SEARCH.val,\
           POLYGROUPS.val
@@ -1567,7 +1547,6 @@ class IMPORT_OT_obj(bpy.types.Operator):
     CREATE_EDGES = BoolProperty(name="Lines as Edges", description="Import lines and faces with 2 verts as edge", default= True)
     SPLIT_OBJECTS = BoolProperty(name="Object", description="Import OBJ Objects into Blender Objects", default= True)
     SPLIT_GROUPS = BoolProperty(name="Group", description="Import OBJ Groups into Blender Objects", default= True)
-    SPLIT_MATERIALS = BoolProperty(name="Split Materials", description="Import each material into a separate mesh", default= False)
     # old comment: only used for user feedback
     # disabled this option because in old code a handler for it disabled SPLIT* params, it's not passed to load_obj
     # KEEP_VERT_ORDER = BoolProperty(name="Keep Vert Order", description="Keep vert and face order, disables split options, enable for morph targets", default= True)
@@ -1588,7 +1567,6 @@ class IMPORT_OT_obj(bpy.types.Operator):
                  self.properties.CREATE_EDGES,
                  self.properties.SPLIT_OBJECTS,
                  self.properties.SPLIT_GROUPS,
-                 self.properties.SPLIT_MATERIALS,
                  self.properties.ROTATE_X90,
                  self.properties.IMAGE_SEARCH,
                  self.properties.POLYGROUPS)
index ca388951e68de9c8e31263134f4704be2792c623..0526263de9b57afae9e1bd30244645534602814d 100644 (file)
@@ -207,7 +207,8 @@ static Base *rna_Scene_object_link(Scene *scene, bContext *C, ReportList *report
 
        ob->recalc |= OB_RECALC_ALL;
 
-       DAG_scene_sort(G.main, scene);
+       /* slows down importers too much, run scene.update() */
+       /* DAG_scene_sort(G.main, scene); */
 
        return base;
 }
@@ -2827,7 +2828,7 @@ static void rna_def_scene_objects(BlenderRNA *brna, PropertyRNA *cprop)
        RNA_def_struct_ui_text(srna, "Scene Objects", "Collection of scene objects");
 
        func= RNA_def_function(srna, "link", "rna_Scene_object_link");
-       RNA_def_function_ui_description(func, "Link object to scene.");
+       RNA_def_function_ui_description(func, "Link object to scene, run scene.update() after.");
        RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS);
        parm= RNA_def_pointer(func, "object", "Object", "", "Object to add to scene.");
        RNA_def_property_flag(parm, PROP_REQUIRED);