Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created December 15, 2022 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGArtPython/57cabad1123e30480b1ce4eabe93e716 to your computer and use it in GitHub Desktop.
Save CGArtPython/57cabad1123e30480b1ce4eabe93e716 to your computer and use it in GitHub Desktop.
Example of turing the script from this tutorial https://youtu.be/uOQ-CPcaqMo into a simple add-on
"""
Example of turing the script from this tutorial
https://youtu.be/uOQ-CPcaqMo
into a simple add-on.
This code is based on the addon_add_object.py Python template distributed with BLender under the GNU GPL license.
An explanation of this script can be found in this tutorial: https://youtu.be/x3QRp2k013k
"""
bl_info = {
"name": "New Ico Spheres in a circle",
"author": "Your Name Here",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > New Object",
"description": "Adds Add Ico Spheres in a circle",
"warning": "",
"doc_url": "",
"category": "Add Mesh",
}
# give Python access to Blender's functionality
import bpy
from bpy.types import Operator
from bpy.props import IntProperty
# extend Python's math functionality
import math
def add_ico_spheres(self):
angle_step = math.tau / self.ico_count
radius = 2
# create a list of vert coordinates
vert_coordinates = list()
# repeat code in a loop
for i in range(self.ico_count):
# calculate current current_angle
current_angle = angle_step * i
# calculate coordinate
x = radius * math.cos(current_angle)
y = radius * math.sin(current_angle)
# visualize what we are doing
bpy.ops.mesh.primitive_ico_sphere_add(radius=0.05, location=(x, y, 0))
# add current coordinate to list
vert_coordinates.append((x, y, 0))
class MESH_OT_add_object(Operator):
"""Add Ico Spheres in a circle"""
bl_idname = "mesh.add_ico_spheres_in_a_circle"
bl_label = "Add Ico Spheres in a circle"
bl_options = {'REGISTER', 'UNDO'}
ico_count: IntProperty(
name="Ico Sphere count",
default=32,
min=1,
description="The number of Ico Spheres",
)
def execute(self, context):
add_ico_spheres(self)
return {'FINISHED'}
# Registration
def add_object_button(self, context):
self.layout.operator(
MESH_OT_add_object.bl_idname,
text="Add Ico Spheres in a circle",
icon='PLUGIN')
def register():
bpy.utils.register_class(MESH_OT_add_object)
bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)
def unregister():
bpy.utils.unregister_class(MESH_OT_add_object)
bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment