Created
December 24, 2020 21:32
-
-
Save JakeCoxon/2927e8ea0d7fa3902be163b83e6ddb22 to your computer and use it in GitHub Desktop.
Blender addon to distribute objects along an axis
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
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
############################################################################ | |
# Installation info | |
# | |
# Download file somewhere to your computer. | |
# In Blender go to edit > preferences > addons | |
# Click install and select the file. | |
# This copies the file to Blender so you may delete the original | |
bl_info = { | |
"name": "Distribute Objects", | |
"description": "Distribute objects along an axis", | |
"author": "Jake Coxon", | |
"version": (0, 1, 0, 20201224), | |
"blender": (2, 91, 0), | |
"category": "UI" | |
} | |
import bpy | |
import mathutils | |
import math | |
from functools import partial | |
from bpy.props import * | |
from mathutils import Vector, Matrix | |
class DistributeObjectsOperator(bpy.types.Operator): | |
bl_idname = "distribute.distributeobjects" | |
bl_label = "Distribute Objects" | |
bl_options = {'REGISTER', 'UNDO'} | |
axis: EnumProperty( | |
name="Axis", | |
description="Axis selection", | |
items= [('x', "X", ""),('y', "Y", ""),('z', "Z","")], | |
default='x' | |
) | |
margin: bpy.props.FloatProperty(name="Margin") | |
def execute(self, contecxt): | |
objs = bpy.context.selected_objects | |
MD = 0 | |
if len(objs) <= 1: | |
return {'CANCELLED'} | |
first = objs[0] | |
gap = abs(self.margin) | |
direction = Vector((0, 0, 0)) | |
setattr(direction, self.axis, 1) | |
if self.margin < 0: | |
direction *= -1 | |
def axis(vec): | |
return getattr(vec, self.axis) | |
def point_to_world_coords(obj, t): | |
return obj.matrix_world @ Vector((*t, 0)) | |
def world_bounds(obj): | |
return [axis(point_to_world_coords(obj, q)) for q in obj.bound_box] | |
new_position = first.location + direction * min(world_bounds(first)) | |
for obj in objs: | |
new_position -= direction * min(world_bounds(obj)) | |
obj.location = new_position | |
new_position += direction * (max(world_bounds(obj)) + gap) | |
return {'FINISHED'} | |
def invoke(self, context, event): | |
if len(context.selected_objects) <= 1: | |
self.report({'WARNING'}, "Not enough objects selected") | |
return {'CANCELLED'} | |
self.execute(context) | |
return context.window_manager.invoke_props_popup(self, event) | |
def menu_func(self, context): | |
self.layout.operator(DistributeObjectsOperator.bl_idname) | |
def register(): | |
bpy.types.VIEW3D_MT_object.append(menu_func) | |
bpy.utils.register_class(DistributeObjectsOperator) | |
def unregister(): | |
bpy.types.VIEW3D_MT_object.remove(menu_func) | |
bpy.utils.unregister_class(DistributeObjectsOperator) | |
if __name__ == "__main__": | |
register() |
Hey @JakeCoxon, I feel very silly asking this, but how do you actually pull up the UI to use the tool..... Download went smoothly but actually accessing the tool is proving to be difficult
it's here for me
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You and me both... I just started using blender and I was missing the option to evenly space out objects.. Somehow came acros this handy dandy script that would allow me to, but I have no idea how to use it :/
There is nothing in my sidebars, submenus, contextmenus, nothing. I feel extremely dumb.