Last active
March 27, 2024 11:45
-
-
Save todashuta/94a90e15df3b97e65c70baa3ec73d488 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #################### | |
| # Batch Import OBJ # | |
| #################### | |
| import bpy | |
| def f(**kwargs): | |
| print(kwargs) | |
| def main(directory, obj_import_options): | |
| from glob import glob | |
| obj_files = glob(directory + "/**/*.obj", recursive=True) | |
| num_files = len(obj_files) | |
| wm = bpy.context.window_manager | |
| wm.progress_begin(0, num_files) | |
| for i,path in enumerate(obj_files, 1): | |
| wm.progress_update(i-1) | |
| print(f"[{ str(i).rjust(len(str(num_files))) }/{num_files}] Import {path} ...") | |
| bpy.ops.wm.obj_import(filepath=path) | |
| #f(filepath=path, **obj_import_options) | |
| wm.progress_end() | |
| class BatchImportOBJ(bpy.types.Operator): | |
| bl_idname = "wm.batch_import_obj" | |
| bl_label = "Batch Import OBJ" | |
| directory: bpy.props.StringProperty( | |
| name="Directory Path", | |
| default="", | |
| maxlen=1024, | |
| subtype='FILE_PATH', | |
| description="", | |
| ) | |
| filter_folder: bpy.props.BoolProperty( | |
| default=True, | |
| options={'HIDDEN'}, | |
| ) | |
| # bpy.ops.wm.obj_import() options | |
| clamp_size: bpy.props.FloatProperty( | |
| name="Clamp Bounding Box", | |
| min=0.0, | |
| max=1000.0, | |
| default=0.0, | |
| ) | |
| forward_axis: bpy.props.EnumProperty( | |
| name="Forward Axis", | |
| items=( | |
| ("X", "X", ""), | |
| ("Y", "Y", ""), | |
| ("Z", "Z", ""), | |
| ("NEGATIVE_X", "-X", ""), | |
| ("NEGATIVE_Y", "-Y", ""), | |
| ("NEGATIVE_Z", "-Z", ""), | |
| ), | |
| default="NEGATIVE_Z", | |
| ) | |
| up_axis: bpy.props.EnumProperty( | |
| name="Up Axis", | |
| items=( | |
| ("X", "X", ""), | |
| ("Y", "Y", ""), | |
| ("Z", "Z", ""), | |
| ("NEGATIVE_X", "-X", ""), | |
| ("NEGATIVE_Y", "-Y", ""), | |
| ("NEGATIVE_Z", "-Z", ""), | |
| ), | |
| default="Y", | |
| ) | |
| import_vertex_groups: bpy.props.BoolProperty( | |
| name="Vertex Groups", | |
| default=False, | |
| ) | |
| validate_meshes: bpy.props.BoolProperty( | |
| name="Validate Meshes", | |
| default=False, | |
| ) | |
| @classmethod | |
| def poll(cls, context): | |
| return bpy.ops.wm.obj_import.poll() | |
| def invoke(self, context, event): | |
| # ファイルエクスプローラーを表示する | |
| # 参考URL: https://docs.blender.org/api/current/bpy.types.WindowManager.html#bpy.types.WindowManager.fileselect_add | |
| context.window_manager.fileselect_add(self) | |
| return {'RUNNING_MODAL'} | |
| def execute(self, context): | |
| obj_import_options = { | |
| "clamp_size": self.clamp_size, | |
| "forward_axis": self.forward_axis, | |
| "up_axis": self.up_axis, | |
| "import_vertex_groups": self.import_vertex_groups, | |
| "validate_meshes": self.validate_meshes, | |
| } | |
| main(self.directory, obj_import_options) | |
| return {'FINISHED'} | |
| def register(): | |
| bpy.utils.register_class(BatchImportOBJ) | |
| if __name__ == "__main__": | |
| register() | |
| bpy.ops.wm.batch_import_obj('INVOKE_DEFAULT') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment