Advertisement
Guest User

pythonBlenderRecalculateNormals

a guest
Jan 31st, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import os
  2. import bpy
  3.  
  4. base_to_obj_dir = os.path.join('C:\\', 'mypath')
  5.  
  6. def reset_blend():
  7.     # simply can be
  8.     # bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)
  9.     bpy.ops.wm.read_factory_settings()
  10.  
  11.     for scene in bpy.data.scenes:
  12.         for obj in scene.objects:
  13.             scene.objects.unlink(obj)
  14.  
  15.     # only worry about data in the startup scene
  16.     for bpy_data_iter in (
  17.             bpy.data.objects,
  18.             bpy.data.meshes,
  19.             bpy.data.lamps,
  20.             bpy.data.cameras,
  21.             ):
  22.         for id_data in bpy_data_iter:
  23.             bpy_data_iter.remove(id_data)
  24.  
  25. def main():
  26.     path = os.path.join(base_to_obj_dir, "02828884")
  27.     obj_list = sorted(os.listdir(path))
  28.    
  29.     # loop through the strings in obj_list and add the files to the scene
  30.     for item in obj_list:
  31.         reset_blend()
  32.         source_to_file = os.path.join(path, item, "model.obj")
  33.         target_to_file = os.path.join(path, item, "model_r.obj")
  34.         bpy.ops.import_scene.obj(filepath=source_to_file)
  35.        
  36.         bpy.ops.import_scene.obj(filepath=source_to_file)
  37.         obj_objects = bpy.context.selected_objects[:]
  38.         #obj_objects = [ o for o in bpy.context.scene.objects if o.select ]
  39.         print("Starting--------------------------------------------")
  40.         for obj in obj_objects:
  41.             bpy.ops.object.select_all(action='DESELECT')
  42.             obj.select = True
  43.             bpy.context.scene.objects.active = obj
  44.             # go edit mode
  45.             bpy.ops.object.mode_set(mode='EDIT')
  46.             # select al faces
  47.             bpy.ops.mesh.select_all(action='SELECT')
  48.             # recalculate outside normals
  49.             bpy.ops.mesh.normals_make_consistent(inside=False)
  50.             # go object mode again
  51.             bpy.ops.object.editmode_toggle()
  52.         print("Ending--------------------------------------------")
  53.        
  54.         bpy.ops.export_scene.obj(filepath=target_to_file,use_materials=True)
  55.    
  56. if __name__ == "__main__":
  57.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement