I am trying to program an add-on for Blender that would make a perfect circle segment.
Pure mathematics. No bmesh or bpy.
The green vertices are my result and the blue circle is what I am after
With h1 = (h + (sqrt(a**2 - (math.cos(angle)*a)**2) - a))
you can calculate the height offset of all vertices, but the operator menu will not allow you to change the height anymore.
So verts1.append((math.cos(angle)*a, math.sin(angle)*h, 0.0))
will allow you to change height and diameter but the outcome is wrong. As sees on the screenshot above.
While verts1.append((math.cos(angle)*a, h1, 0.0))
moves all vertices up or down as you can see in this screenshot below.
My goal is to keep the lowest vertices on the X axis as in the first screenshot.
The circle is radius 1000mm (1M) and moved -500 Y. The height of the segment is 500 and diameter is 1732mm
This is the code so far. Upon running a new "button" will appear under add --> mesh --> Segment circle
bl_info = {
"name": "segment",
"author": "The Bold Guy",
"version": (1, 0),
"blender": (2, 80, 1),
"location": "View3D > Add > Mesh > segment",
"description": "Adds a new Mesh Object",
"warning": "",
"wiki_url": "",
"category": "Add Mesh",
}
import bpy
import bmesh
import math
import mathutils
from mathutils import Matrix
from bpy import types
from bpy.props import (
StringProperty,
BoolProperty,
IntProperty,
IntVectorProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (
Panel,
Menu,
Operator,
PropertyGroup,
)
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector
from math import degrees, radians, pi, tan, asin, sin, cos, acos, sqrt
def add_plane_mesh(self, context):
mesh = bpy.data.meshes.new("Vert")
verts = []
edges = []
faces = []
h = self.height
a = self.diam/2
h1 = []
for i in range(self.segments+1):
angle = (math.pi * (i / self.segments))
##Calculate the offset of vertices on any given point of circle (180 degrees)
#(math.cos(angle)*a)
##Calculating Height of an Arc at Any Point
h1 = (h + (sqrt(a**2 - (math.cos(angle)*a)**2) - a))
##make list X,Y,Z for vertices to be added
verts.append((math.cos(angle)*a, math.sin(angle)*h, 0.0))
mesh.from_pydata(verts, edges, faces)
return mesh
def add_plane_mesh1(self, context):
return {"FINISHED"}
class OBJECT_Oval_pipe_add_object(Operator, AddObjectHelper):
"""Create a new Mesh Object"""
bl_idname = "tools.add_segment"
bl_label = "Add segment"
bl_options = {"REGISTER", "UNDO", 'PRESET'}
segments: IntProperty(
name="Segments",
# unit='LENGTH',
min=1,
default=8,
)
angle: IntProperty(
name="Segments",
# unit='LENGTH',
min=0,
max=360,
default=180,
)
height: FloatProperty(
attr='height', name="Height", unit="LENGTH", default=1000 / 1000 ,
)
diam: FloatProperty(
attr='diam', name="length", unit="LENGTH", default=2000 / 1000,
)
def execute(self, context):
object_data_add(context, add_plane_mesh(self, context), operator=self)
return {"FINISHED"}
# Registration
def add_object_button(self, context):
self.layout.operator(
OBJECT_Oval_pipe_add_object.bl_idname, text="Segment Circle", icon="PLUGIN"
)
# This allows you to right click on a button and link to documentation
def add_object_manual_map():
url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
url_manual_mapping = (
("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
)
return url_manual_prefix, url_manual_mapping
def register():
bpy.utils.register_class(OBJECT_Oval_pipe_add_object)
bpy.utils.register_manual_map(add_object_manual_map)
bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)
def unregister():
bpy.utils.unregister_class(OBJECT_Oval_pipe_add_object)
bpy.utils.unregister_manual_map(add_object_manual_map)
bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)
if __name__ == "__main__":
register()