Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import bpy
  2. import os
  3.  
  4. from bpy.props import(StringProperty,
  5. PointerProperty,
  6. )
  7.  
  8. from bpy.types import(Panel,
  9. Operator,
  10. AddonPreferences,
  11. PropertyGroup,
  12. )
  13.  
  14. class MySettings(PropertyGroup):
  15.  
  16. path = StringProperty(
  17. name="",
  18. description ="Path to Directory",
  19. default ="",
  20. maxlen=1024,
  21. subtype='DIR_PATH')
  22.  
  23.  
  24. class WMImporterPanel(Panel):
  25. bl_idname = "OBJECT_PT_WMImporter"
  26. bl_category = "WorldMachine Importer"
  27. bl_label = "WM Importer"
  28. bl_space_type = 'VIEW_3D'
  29. bl_region_type = 'TOOLS'
  30. bl_context = 'objectmode'
  31.  
  32.  
  33. def draw(self, context):
  34. layout = self.layout
  35. col = layout.column(align=True)
  36. scn = context.scene
  37.  
  38.  
  39. #Selecting Directory/Folder path
  40. col.label(text="Directory of Files")
  41. col.separator()
  42. dirpath = col.prop(scn.my_tool, "path", text="")
  43.  
  44.  
  45. #Button to perform import of meshes
  46. col.separator()
  47. col.operator("wmimport.importmeshes", text = "Import Meshes")
  48.  
  49. class OBJECT_OT_ImportButton(Operator):
  50. bl_idname = "wmimport.importmeshes"
  51. bl_label ="Import Meshes"
  52.  
  53. def execute(self,context):
  54. scn = context.scene
  55. dirpath = scn.my_tool.path
  56.  
  57. #print(dirpath)
  58.  
  59. file_list = os.listdir(dirpath)
  60.  
  61. obj_list = [item for item in file_list if item[-3:] == 'obj']
  62. #for file in obj_list:
  63. # print (file)
  64.  
  65.  
  66. # Detect Naming Convention
  67. # Need Help
  68. # I want to pull the first file and rip the format from it.
  69. # Files come in the format of <filename>_x000_y000.obj
  70. # I want to use the filename to name each object imported, so if I called it Island
  71. # the object in blender will be called something like Island(X,Y)
  72. # where X = number following x in filename, and Y likewise.
  73.  
  74. for item in obj_list:
  75.  
  76. #Detect Location
  77. # Need Help
  78. #I want to get the X and Y value in the filename, so I can use that to
  79. #increment the object in corrosponding directions.
  80.  
  81.  
  82. fullpath = os.path.join(dirpath,item)
  83. bpy.ops.import_scene.obj(filepath=fullpath, axis_forward='Y', axis_up='Z')
  84. # Should I assign this obj to a variable?
  85. # newObj = bpy.ops.import_scene.obj(filepath=fullpath, axis_forward='Y', axis_up='Z')
  86.  
  87. return{'FINISHED'}
  88.  
  89. def register():
  90. bpy.utils.register_module(__name__)
  91. bpy.types.Scene.my_tool = PointerProperty(type=MySettings)
  92.  
  93. def unregister():
  94. bpy.utils.unregister_module(__name__)
  95. del bpy.types.Scene.my_tool
  96.  
  97. if __name__ == "__main__":
  98. register()
  99.  
  100. def get_nameXY(item):
  101. name_set = item.split('_') #splits the string at every '_', returns a list
  102. x = name_set[1][1:] #gets the second value in the list starting at the second character
  103. y = name_set[2][1:-4] #gets the third value in the list starting at the second character and omitting the last four characters
  104. name = name_set[0] + '(' + x + ',' + y + ')' #combine the first value with x and y
  105. return name, float(x), float(y) #return the name with x and y as floats
  106.  
  107. for item in obj_list:
  108.  
  109. #Detect Location
  110. name, x, y = get_nameXY(item)
  111. for obj in bpy.data.objects:
  112. obj.select = False #deselect all objects so imported object will be the only one selected
  113.  
  114. fullpath = os.path.join(dirpath,item)
  115. bpy.ops.import_scene.obj(filepath=fullpath, axis_forward='Y', axis_up='Z')
  116.  
  117. new_obj = bpy.context.selected_objects[0] # get object from list of selected
  118. new_obj.name = name #set the new name
  119. new_obj.location = (x,y,0)#set the location
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement