Skip to content

Instantly share code, notes, and snippets.

@macegr
Created July 9, 2020 03:25
Show Gist options
  • Save macegr/cc6f8c57235378b17209f3d5e45bc10e to your computer and use it in GitHub Desktop.
Save macegr/cc6f8c57235378b17209f3d5e45bc10e to your computer and use it in GitHub Desktop.
bl_info = {
"name": "Polar Zonohedron",
"author": "Rob Bell, Garrett Mace",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > Polar Zonohedron",
"description": "Adds a new Polar Zonohedron",
"warning": "",
"wiki_url": "",
"category": "Add Mesh",
}
import bpy
from bpy.types import Operator
from bpy.props import FloatProperty, IntProperty, BoolProperty
from bpy_extras.object_utils import AddObjectHelper, object_data_add
import bmesh
from mathutils import *
from math import *
def add_object(self, context):
frequency = self.frequency
if self.autoscale:
self.edgelen = (6/sqrt(3))/frequency
edgelen = self.edgelen
mesh = bpy.data.meshes.new("mesh")
pitch = atan(sqrt(2)/2)
bm = bmesh.new()
pts=[None,None,None,None]
pts[0] = Vector([0,0,0])
mat = Matrix.Rotation(-pitch, 3, 'Y')
vector = mat @ Vector([edgelen,0,0])
pts[2] = vector
p_rotate = Matrix.Rotation(2*pi/frequency, 3, 'Z')
for i in range(1,frequency):
pts[1] = pts[0] + vector
pts[3] = p_rotate @ pts[1]
pts[2] = pts[3] + vector
for j in range(0,frequency):
f_rotate = Matrix.Rotation((j*2*pi)/frequency, 3, 'Z')
rotpts = [f_rotate @ p for p in pts]
newverts = []
for pt in rotpts:
newverts.append(bm.verts.new(pt.to_tuple()))
bm.faces.new(reversed(newverts))
pts[0] = pts[3]
height = max([vert.co.z for vert in bm.verts])
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.01)
bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
bmesh.ops.translate(bm, verts=bm.verts, vec = (0,0,-height/2))
bm.to_mesh(mesh)
bm.free()
object_data_add(context, mesh, operator=self)
class OBJECT_OT_add_object(Operator, AddObjectHelper):
"""Create a new Polar Zonohedron"""
bl_idname = "mesh.add_polar_zonohedron"
bl_label = "Add Polar Zonohedron"
bl_options = {'REGISTER', 'UNDO'}
edgelen: FloatProperty(
name="Edge Length",
min=0.01,
max=100.0,
default=1.0,
)
frequency: IntProperty(
name="Frequency",
min=3,
max=100,
default=8,
)
autoscale: BoolProperty(
name="Autoscale",
default=True,
)
def execute(self, context):
add_object(self, context)
return {'FINISHED'}
# Registration
def add_object_button(self, context):
self.layout.operator(
OBJECT_OT_add_object.bl_idname,
text="Add Polar Zonohedron",
icon='PLUGIN')
# This allows you to right click on a button and link to the manual
def add_object_manual_map():
url_manual_prefix = "https://docs.blender.org/manual/en/dev/"
url_manual_mapping = (
("bpy.ops.mesh.add_object", "editors/3dview/object"),
)
return url_manual_prefix, url_manual_mapping
def register():
bpy.utils.register_class(OBJECT_OT_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_OT_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()
bl_info = {
"name": "Polar Zonohedron",
"author": "Rob Bell, Garrett Mace",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > Polar Zonohedron",
"description": "Adds a new Polar Zonohedron",
"warning": "",
"wiki_url": "",
"category": "Add Mesh",
}
import bpy
from bpy.types import Operator
from bpy.props import FloatProperty, IntProperty, BoolProperty
from bpy_extras.object_utils import AddObjectHelper, object_data_add
import bmesh
from mathutils import *
from math import *
def add_object(self, context):
frequency = self.frequency
if self.autoscale:
self.edgelen = (6/sqrt(3))/frequency
edgelen = self.edgelen
mesh = bpy.data.meshes.new("mesh")
pitch = atan(sqrt(2)/2)
bm = bmesh.new()
pts=[None,None,None,None]
pts[0] = Vector([0,0,0])
mat = Matrix.Rotation(-pitch, 3, 'Y')
vector = mat @ Vector([edgelen,0,0])
pts[2] = vector
p_rotate = Matrix.Rotation(2*pi/frequency, 3, 'Z')
for i in range(1,frequency):
pts[1] = pts[0] + vector
pts[3] = p_rotate @ pts[1]
pts[2] = pts[3] + vector
for j in range(0,frequency):
f_rotate = Matrix.Rotation((j*2*pi)/frequency, 3, 'Z')
rotpts = [f_rotate @ p for p in pts]
newverts = []
for pt in rotpts:
newverts.append(bm.verts.new(pt.to_tuple()))
bm.faces.new(reversed(newverts))
pts[0] = pts[3]
height = max([vert.co.z for vert in bm.verts])
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.01)
bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
bmesh.ops.translate(bm, verts=bm.verts, vec = (0,0,-height/2))
bm.to_mesh(mesh)
bm.free()
object_data_add(context, mesh, operator=self)
class OBJECT_OT_add_object(Operator, AddObjectHelper):
"""Create a new Polar Zonohedron"""
bl_idname = "mesh.add_polar_zonohedron"
bl_label = "Add Polar Zonohedron"
bl_options = {'REGISTER', 'UNDO'}
edgelen: FloatProperty(
name="Edge Length",
min=0.01,
max=100.0,
default=1.0,
)
frequency: IntProperty(
name="Frequency",
min=3,
max=100,
default=8,
)
autoscale: BoolProperty(
name="Autoscale",
default=True,
)
def execute(self, context):
add_object(self, context)
return {'FINISHED'}
# Registration
def add_object_button(self, context):
self.layout.operator(
OBJECT_OT_add_object.bl_idname,
text="Add Polar Zonohedron",
icon='PLUGIN')
# This allows you to right click on a button and link to the manual
def add_object_manual_map():
url_manual_prefix = "https://docs.blender.org/manual/en/dev/"
url_manual_mapping = (
("bpy.ops.mesh.add_object", "editors/3dview/object"),
)
return url_manual_prefix, url_manual_mapping
def register():
bpy.utils.register_class(OBJECT_OT_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_OT_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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment