Created
June 6, 2025 14:19
-
-
Save dirkk0/ad472e68bd858b68c7d6e9d5c4986001 to your computer and use it in GitHub Desktop.
Blender script to make new objects snap to the grid (because the asset browser doesn't do this)
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
import bpy | |
import math | |
# Define grid size | |
GRID_SIZE = 1.0 | |
# Track previously known object names | |
last_object_names = set(obj.name for obj in bpy.context.scene.objects) | |
def snap_to_grid(obj, grid_size=GRID_SIZE): | |
"""Snap an object's location to the nearest grid point.""" | |
loc = obj.location | |
snapped = [round(coord / grid_size) * grid_size for coord in loc] | |
obj.location = snapped | |
def check_new_objects_and_snap(): | |
global last_object_names | |
current_names = set(obj.name for obj in bpy.context.scene.objects) | |
new_object_names = current_names - last_object_names | |
if new_object_names: | |
for name in new_object_names: | |
obj = bpy.context.scene.objects.get(name) | |
if obj is not None: | |
print(f"Snapping new object: {obj.name}") | |
snap_to_grid(obj) | |
last_object_names = current_names | |
return 0.5 # Repeat every 0.5 seconds | |
# Start the timer | |
bpy.app.timers.register(check_new_objects_and_snap) | |
print("Auto-grid snapper started.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment