Hourglass Mesh Script

I have a few questions about this script I’ve written, and sorry if they’re big questions.

First off, here’s the script:

import bpy

# Position the vertices
verts = [(0, 0, -2), (0, 1, -2), (0.7, 0.7, -2), (1, 0, -2), (0.7, -0.7, -2), (0, -1, -2), (-0.7, -0.7, -2), (-1, 0, -2), (-0.7, 0.7, -2), (0, 1, -1.9), (0.7, 0.7, -1.9), (1, 0, -1.9), (0.7, -0.7, -1.9), (0, -1, -1.9), (-0.7, -0.7, -1.9), (-1, 0, -1.9), (-0.7, 0.7, -1.9), (0, 0.6, -1.9), (0.42, 0.42, -1.9), (0.6, 0, -1.9), (0.42, -0.42, -1.9), (0, -0.6, -1.9), (-0.42, -0.42, -1.9), (-0.6, 0, -1.9), (-0.42, 0.42, -1.9), (0, 0.6, -0.7), (0.42, 0.42, -0.7), (0.6, 0, -0.7), (0.42, -0.42, -0.7), (0, -0.6, -0.7), (-0.42, -0.42, -0.7), (-0.6, 0, -0.7), (-0.42, 0.42, -0.7), (0, 0.5, -0.4), (0.35, 0.35, -0.4), (0.5, 0, -0.4), (0.35, -0.35, -0.4), (0, -0.5, -0.4), (-0.35, -0.35, -0.4), (-0.5, 0, -0.4), (-0.35, 0.35, -0.4), (0, 0.3, -0.2), (0.22, 0.22, -0.2), (0.3, 0, -0.2), (0.22, -0.22, -0.2), (0, -0.3, -0.2), (-0.22, -0.22, -0.2), (-0.3, 0, -0.2), (-0.22, 0.22, -0.2), (0, 0.1, -0.1), (0.07, 0.07, -0.1), (0.1, 0, -0.1), (0.07, -0.07, -0.1), (0, -0.1, -0.1), (-0.07, -0.07, -0.1), (-0.1, 0, -0.1), (-0.07, 0.07, -0.1), (0, 0.1, 0), (0.07, 0.07, 0), (0.1, 0, 0), (0.07, -0.07, 0), (0, -0.1, 0), (-0.07, -0.07, 0), (-0.1, 0, 0), (-0.07, 0.07, 0)]

# Connect the faces
faces = [(1, 0, 2), (2, 0, 3), (3, 0, 4), (4, 0, 5), (5, 0, 6), (6, 0, 7), (7, 0, 8), (8, 0, 1), (9, 1, 2, 10), (10, 2, 3, 11), (11, 3, 4, 12), (12, 4, 5, 13), (13, 5, 6, 14), (14, 6, 7, 15), (15, 7, 8, 16), (16, 8, 1, 9), (9, 17, 18, 10), (10, 18, 19, 11), (11, 19, 20, 12), (12, 20, 21, 13), (13, 21, 22, 14), (14, 22, 23, 15), (15, 23, 24, 16), (16, 24, 17, 9), (17, 25, 26, 18), (18, 26, 27, 19), (19, 27, 28, 20), (20, 28, 29, 21), (21, 29, 30, 22), (22, 30, 31, 23), (23, 31, 32, 24), (24, 32, 25, 17), (25, 33, 34, 26), (26, 34, 35, 27), (27, 35, 36, 28), (28, 36, 37, 29), (29, 37, 38, 30), (30, 38, 39, 31), (31, 39, 40, 32), (32, 40, 33, 25), (33, 41, 42, 34), (34, 42, 43, 35), (35, 43, 44, 36), (36, 44, 45, 37), (37, 45, 46, 38), (38, 46, 47, 39), (39, 47, 48, 40), (40, 48, 41, 33), (41, 49, 50, 42), (42, 50, 51, 43), (43, 51, 52, 44), (44, 52, 53, 45), (45, 53, 54, 46), (46, 54, 55, 47), (47, 55, 56, 48), (48, 56, 49, 41), (49, 57, 58, 50), (50, 58, 59, 51), (51, 59, 60, 52), (52, 60, 61, 53), (53, 61, 62, 54), (54, 62, 63, 55), (55, 63, 64, 56), (56, 64, 57, 49)]

# Create new mesh data
mesh = bpy.data.meshes.new("Hourglass")

# Create new object
object = bpy.data.objects.new("Hourglass", mesh)

# Link object to scene
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)

# Shape the mesh using the data established previously
mesh.from_pydata(verts, [], faces)

# Update the mesh so it will become visible in wireframe view, and will display an outline when selected
mesh.update(calc_edges=True)

# Apply a Mirror Modifier
mod = object.modifiers.new(name = "Mirror", type="MIRROR")
mod.use_x = False
mod.use_z = True

Questions:

1.) How do I make it so that when the hourglass is added, it’s selected by default?
2.) How can I add this to the ‘Add Mesh’ menu? I’ve been following some of the scripting tutorials on blendercookie.com, but I still can’t figure out how to achieve this.

Thanks for your help!

to get it into the add mesh menu, make a function like this:

def Plane_button(self, context):
    self.layout.operator(MyMeshClass.bl_idname, text="plane", icon="PLUGIN")

and put the following line in the register function

bpy.types.INFO_MT_mesh_add.append(Plane_button)

note that this is only possible when making an add-on, BlenderCookie has some tutorials on that too. in those bl_idname and register are explained

from what i know when having an addon the newmade object is automaticaly selected :wink:

Thanks! I’ll try it out and see if it’s selected when I use it as an add-on :yes:

hi, there’s a template for add mesh addons in blender.
Text editor > Templates > addon add object

that will show you the correct way.
also you can look at other add mesh scripts to see how they do it :slight_smile:

To make sure it’s selected, you can just add:

object.select = True