addon_add_object.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. bl_info = {
  2. "name": "New Object",
  3. "author": "Your Name Here",
  4. "version": (1, 0),
  5. "blender": (2, 80, 0),
  6. "location": "View3D > Add > Mesh > New Object",
  7. "description": "Adds a new Mesh Object",
  8. "warning": "",
  9. "wiki_url": "",
  10. "category": "Add Mesh",
  11. }
  12. import bpy
  13. from bpy.types import Operator
  14. from bpy.props import FloatVectorProperty
  15. from bpy_extras.object_utils import AddObjectHelper, object_data_add
  16. from mathutils import Vector
  17. def add_object(self, context):
  18. scale_x = self.scale.x
  19. scale_y = self.scale.y
  20. verts = [
  21. Vector((-1 * scale_x, 1 * scale_y, 0)),
  22. Vector((1 * scale_x, 1 * scale_y, 0)),
  23. Vector((1 * scale_x, -1 * scale_y, 0)),
  24. Vector((-1 * scale_x, -1 * scale_y, 0)),
  25. ]
  26. edges = []
  27. faces = [[0, 1, 2, 3]]
  28. mesh = bpy.data.meshes.new(name="New Object Mesh")
  29. mesh.from_pydata(verts, edges, faces)
  30. # useful for development when the mesh may be invalid.
  31. # mesh.validate(verbose=True)
  32. object_data_add(context, mesh, operator=self)
  33. class OBJECT_OT_add_object(Operator, AddObjectHelper):
  34. """Create a new Mesh Object"""
  35. bl_idname = "mesh.add_object"
  36. bl_label = "Add Mesh Object"
  37. bl_options = {'REGISTER', 'UNDO'}
  38. scale: FloatVectorProperty(
  39. name="scale",
  40. default=(1.0, 1.0, 1.0),
  41. subtype='TRANSLATION',
  42. description="scaling",
  43. )
  44. def execute(self, context):
  45. add_object(self, context)
  46. return {'FINISHED'}
  47. # Registration
  48. def add_object_button(self, context):
  49. self.layout.operator(
  50. OBJECT_OT_add_object.bl_idname,
  51. text="Add Object",
  52. icon='PLUGIN')
  53. # This allows you to right click on a button and link to documentation
  54. def add_object_manual_map():
  55. url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
  56. url_manual_mapping = (
  57. ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
  58. )
  59. return url_manual_prefix, url_manual_mapping
  60. def register():
  61. bpy.utils.register_class(OBJECT_OT_add_object)
  62. bpy.utils.register_manual_map(add_object_manual_map)
  63. bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)
  64. def unregister():
  65. bpy.utils.unregister_class(OBJECT_OT_add_object)
  66. bpy.utils.unregister_manual_map(add_object_manual_map)
  67. bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)
  68. if __name__ == "__main__":
  69. register()