Created
April 24, 2024 23:27
-
-
Save GREEB/e34fc1a23324c4f94ae1b763bab37575 to your computer and use it in GitHub Desktop.
import and space in grid blender, good for kenny assets
This file contains 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
import bpy | |
import os | |
from mathutils import Vector | |
# Directory containing your GLB files | |
directory = r"C:\Users\crazyUser\models\gltf" | |
# List all files in the directory | |
files = os.listdir(directory) | |
# Define grid parameters | |
grid_spacing_x = 1 | |
grid_spacing_y = 1 | |
grid_rows = 10 | |
# Initialize offset for parent objects | |
parent_offset = Vector((0, 0, 0)) | |
# Import GLB files and offset each imported object | |
for i, file in enumerate(files): | |
if file.endswith(".glb"): | |
filepath = os.path.join(directory, file) | |
bpy.ops.import_scene.gltf(filepath=filepath) | |
# Offset the parent objects | |
parent_objects = [obj for obj in bpy.context.selected_objects if obj.parent is None] | |
for obj in parent_objects: | |
obj.location.x = parent_offset.x + (i % grid_rows) * grid_spacing_x | |
obj.location.y = parent_offset.y + (i // grid_rows) * grid_spacing_y | |
obj.location.z = parent_offset.z | |
# Increment the offset for the next parent object | |
parent_offset.x += grid_spacing_x | |
if (i + 1) % grid_rows == 0: | |
parent_offset.x = 0 | |
parent_offset.y += grid_spacing_y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.blender.org/api/current/bpy.ops.import_scene.html