acoolrocket

Blender - Import each FBXs stuff into respective collection with a null

Sep 11th, 2026
33
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. """Import every FBX in a directory into an individual collection.
  2.  
  3. Run this script from Blender 5.2's Scripting workspace. Each FBX is imported
  4. using the native FBX importer defaults, then its imported objects are moved to
  5. a collection named after the FBX and parented to a ``<FBX name> Null`` empty.
  6. """
  7.  
  8. from pathlib import Path
  9.  
  10. import bpy
  11.  
  12.  
  13. FBX_DIRECTORY = Path(r"K:\Videos\Weapons\Starship Troopers Guns\All FBXs")
  14.  
  15.  
  16. def get_or_create_collection(name: str) -> bpy.types.Collection:
  17. """Return a scene-linked collection with *name*."""
  18. collection = bpy.data.collections.get(name)
  19. if collection is None:
  20. collection = bpy.data.collections.new(name)
  21. bpy.context.scene.collection.children.link(collection)
  22. return collection
  23.  
  24.  
  25. def move_to_collection(obj: bpy.types.Object, collection: bpy.types.Collection) -> None:
  26. """Make *collection* the object's only collection."""
  27. for current_collection in tuple(obj.users_collection):
  28. current_collection.objects.unlink(obj)
  29. collection.objects.link(obj)
  30.  
  31.  
  32. def import_fbx(fbx_path: Path) -> None:
  33. """Import one FBX and organize every object created by that import."""
  34. objects_before = set(bpy.data.objects)
  35.  
  36. # Supplying only filepath deliberately uses Blender 5.2's native importer
  37. # default settings for every FBX.
  38. bpy.ops.wm.fbx_import(filepath=str(fbx_path))
  39.  
  40. imported_objects = [obj for obj in bpy.data.objects if obj not in objects_before]
  41. if not imported_objects:
  42. print(f"No objects were imported from: {fbx_path}")
  43. return
  44.  
  45. asset_name = fbx_path.stem
  46. collection = get_or_create_collection(asset_name)
  47.  
  48. empty = bpy.data.objects.new(f"{asset_name} Null", None)
  49. empty.empty_display_type = 'PLAIN_AXES'
  50. collection.objects.link(empty)
  51.  
  52. for obj in imported_objects:
  53. move_to_collection(obj, collection)
  54. # The empty is at the world origin, so this preserves imported transforms
  55. # while making every imported object a child of the asset empty.
  56. obj.parent = empty
  57. obj.matrix_parent_inverse = empty.matrix_world.inverted()
  58.  
  59. print(f"Imported {fbx_path.name}: {len(imported_objects)} objects -> {collection.name}")
  60.  
  61.  
  62. def main() -> None:
  63. if not FBX_DIRECTORY.is_dir():
  64. raise FileNotFoundError(f"FBX directory does not exist: {FBX_DIRECTORY}")
  65.  
  66. fbx_files = sorted(
  67. (path for path in FBX_DIRECTORY.iterdir() if path.is_file() and path.suffix.lower() == ".fbx"),
  68. key=lambda path: path.name.casefold(),
  69. )
  70. if not fbx_files:
  71. print(f"No FBX files found in: {FBX_DIRECTORY}")
  72. return
  73.  
  74. for fbx_path in fbx_files:
  75. import_fbx(fbx_path)
  76.  
  77. print(f"Finished importing {len(fbx_files)} FBX file(s).")
  78.  
  79.  
  80. if __name__ == "__main__":
  81. main()
  82.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment