diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-12-30 05:39:55 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-12-30 05:39:55 +0400 |
commit | 33955940e4931e2184434393e0f8c15c36a5c3c6 (patch) | |
tree | 7b3c10343162a256e85ff4346f57122032715e82 /release/scripts/templates_py/operator_mesh_add.py | |
parent | e12354c4c5850864f925d22f53ec31578384bc63 (diff) |
add templates menu for OSL, use preprocessor directive color for decorators in python.
Diffstat (limited to 'release/scripts/templates_py/operator_mesh_add.py')
-rw-r--r-- | release/scripts/templates_py/operator_mesh_add.py | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/release/scripts/templates_py/operator_mesh_add.py b/release/scripts/templates_py/operator_mesh_add.py new file mode 100644 index 00000000000..fa248cb9005 --- /dev/null +++ b/release/scripts/templates_py/operator_mesh_add.py @@ -0,0 +1,122 @@ +import bpy +import bmesh + + +def add_box(width, height, depth): + """ + This function takes inputs and returns vertex and face arrays. + no actual mesh data creation is done here. + """ + + verts = [(+1.0, +1.0, -1.0), + (+1.0, -1.0, -1.0), + (-1.0, -1.0, -1.0), + (-1.0, +1.0, -1.0), + (+1.0, +1.0, +1.0), + (+1.0, -1.0, +1.0), + (-1.0, -1.0, +1.0), + (-1.0, +1.0, +1.0), + ] + + faces = [(0, 1, 2, 3), + (4, 7, 6, 5), + (0, 4, 5, 1), + (1, 5, 6, 2), + (2, 6, 7, 3), + (4, 0, 3, 7), + ] + + # apply size + for i, v in enumerate(verts): + verts[i] = v[0] * width, v[1] * depth, v[2] * height + + return verts, faces + + +from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty + + +class AddBox(bpy.types.Operator): + """Add a simple box mesh""" + bl_idname = "mesh.primitive_box_add" + bl_label = "Add Box" + bl_options = {'REGISTER', 'UNDO'} + + width = FloatProperty( + name="Width", + description="Box Width", + min=0.01, max=100.0, + default=1.0, + ) + height = FloatProperty( + name="Height", + description="Box Height", + min=0.01, max=100.0, + default=1.0, + ) + depth = FloatProperty( + name="Depth", + description="Box Depth", + min=0.01, max=100.0, + default=1.0, + ) + + # generic transform props + view_align = BoolProperty( + name="Align to View", + default=False, + ) + location = FloatVectorProperty( + name="Location", + subtype='TRANSLATION', + ) + rotation = FloatVectorProperty( + name="Rotation", + subtype='EULER', + ) + + def execute(self, context): + + verts_loc, faces = add_box(self.width, + self.height, + self.depth, + ) + + mesh = bpy.data.meshes.new("Box") + + bm = bmesh.new() + + for v_co in verts_loc: + bm.verts.new(v_co) + + for f_idx in faces: + bm.faces.new([bm.verts[i] for i in f_idx]) + + bm.to_mesh(mesh) + mesh.update() + + # add the mesh as an object into the scene with this utility module + from bpy_extras import object_utils + object_utils.object_data_add(context, mesh, operator=self) + + return {'FINISHED'} + + +def menu_func(self, context): + self.layout.operator(AddBox.bl_idname, icon='MESH_CUBE') + + +def register(): + bpy.utils.register_class(AddBox) + bpy.types.INFO_MT_mesh_add.append(menu_func) + + +def unregister(): + bpy.utils.unregister_class(AddBox) + bpy.types.INFO_MT_mesh_add.remove(menu_func) + +if __name__ == "__main__": + register() + + # test call + bpy.ops.mesh.primitive_box_add() |