blender/scripts/templates_py/addon_add_object.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.2 KiB
Python
Raw Permalink Normal View History

# To make this add-on installable, create an extension with it:
# https://docs.blender.org/manual/en/latest/advanced/extensions/getting_started.html
import bpy
2012-04-11 08:22:31 +00:00
from bpy.types import Operator
from bpy.props import FloatVectorProperty
2012-04-11 08:22:31 +00:00
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector
def add_object(self, context):
scale_x = self.scale.x
scale_y = self.scale.y
2018-06-26 19:41:37 +02:00
verts = [
Vector((-1 * scale_x, 1 * scale_y, 0)),
Vector((1 * scale_x, 1 * scale_y, 0)),
Vector((1 * scale_x, -1 * scale_y, 0)),
Vector((-1 * scale_x, -1 * scale_y, 0)),
]
edges = []
faces = [[0, 1, 2, 3]]
2012-04-11 08:22:31 +00:00
mesh = bpy.data.meshes.new(name="New Object Mesh")
mesh.from_pydata(verts, edges, faces)
# useful for development when the mesh may be invalid.
# mesh.validate(verbose=True)
2012-04-11 08:22:31 +00:00
object_data_add(context, mesh, operator=self)
2012-04-11 08:22:31 +00:00
class OBJECT_OT_add_object(Operator, AddObjectHelper):
"""Create a new Mesh Object"""
bl_idname = "mesh.add_object"
bl_label = "Add Mesh Object"
bl_options = {'REGISTER', 'UNDO'}
scale: FloatVectorProperty(
2018-06-26 19:41:37 +02:00
name="scale",
default=(1.0, 1.0, 1.0),
subtype='TRANSLATION',
description="scaling",
)
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 Object",
icon='PLUGIN',
)
# This allows you to right click on a button and link to documentation
2012-08-25 15:00:41 +00:00
def add_object_manual_map():
url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
2012-08-25 15:00:41 +00:00
url_manual_mapping = (
("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
2018-06-26 19:41:37 +02:00
)
2012-08-25 15:00:41 +00:00
return url_manual_prefix, url_manual_mapping
def register():
bpy.utils.register_class(OBJECT_OT_add_object)
2012-08-25 15:00:41 +00:00
bpy.utils.register_manual_map(add_object_manual_map)
bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)
2012-09-26 21:19:51 +00:00
def unregister():
bpy.utils.unregister_class(OBJECT_OT_add_object)
2012-08-25 15:00:41 +00:00
bpy.utils.unregister_manual_map(add_object_manual_map)
bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)
2012-04-11 08:22:31 +00:00
if __name__ == "__main__":
register()