Blender add-on that defines a new mesh object

This script is a Blender add-on that defines a new mesh object with a simple square shape.

Blender add-on that defines a new mesh
Blender add-on that defines a new mesh 
  

bl_info = {
    "name": "New Object",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "doc_url": "",
    "category": "Add Mesh",
}

import bpy
from bpy.types import Operator
from bpy.props import FloatVectorProperty
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

    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]]

    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)
    object_data_add(context, mesh, operator=self)


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(
        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
def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    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()
    
    
    

Let's break down the key components:


1. Addon Information (`bl_info`):

  •    name: The name of the add-on.
  •    author:The author's name.
  •    version: The version number of the add-on.
  •    blender:The Blender version compatibility.
  •    location:The location where the add-on can be accessed in Blender.
  •    description: A brief description of what the add-on does.
  •    warning: Any warnings or important notes about the add-on.
  •    doc_url: A URL for documentation (currently not provided).
  •    category: The category under which the add-on is listed.

2. Import Statements:

  • bpy`: The Blender Python API, providing access to Blender functionality.
  • Operator`: A base class for creating operators (tools or commands).
  • FloatVectorProperty`: A property type for representing a vector of floating-point values.
  • AddObjectHelper`: A helper class for creating new objects.

3. add_object` Function:

  •    - Creates a new mesh object with a square shape based on the specified scale.
  •    - Defines the vertices, edges, and faces of the square.
  •    - Creates a new mesh using the provided data.
  •    - Adds the object to the scene.

4. OBJECT_OT_add_object` Operator Class:

  •    - Subclass of the `Operator` class and `AddObjectHelper`.
  •    - Represents the operator for creating a new mesh object.
  •    - Defines a property (`scale`) for controlling the size of the object.

5. execute` Method:

  •    - Executes the operator by calling the `add_object` function with the specified scale.

6. Registration Functions (`register` and `unregister`):

  •    - Register and unregister the operator and UI elements.

7. UI Integration (`add_object_button` and `add_object_manual_map`):

  •    - Adds a button to the 3D View toolbar for accessing the new object creation operator.
  •    - Provides a link to the documentation for the operator.

8. Script Execution (`if __name__ == "__main__":):

   - Registers the add-on when the script is executed.

This script essentially adds a new mesh object with a square shape to the Blender scene. You can find and use this tool in the "Add" menu under "Mesh" in the 3D View. If you have any specific questions or if there's anything specific you'd like to know or modify, feel free to ask!

Here are a few modifications you might consider for this script:


### 1. **Change the Default Shape:**
   - Instead of a square, you could modify the script to create a different default shape. For example, a cube or a different polygon.

### 2. **Add Parameters to the Operator:**
   - You can add more parameters to the operator to give the user more control. For example, parameters to control the number of sides, radius, or any other relevant properties.

### 3. **Change the Object Name:**
   - You can modify the script to use a different name for the created object. Update the `name` parameter in the line `mesh = bpy.data.meshes.new(name="New Object Mesh")`.

### 4. **Adjust Default Scale:**
   - Modify the default scale of the object by changing the `default` values in the `FloatVectorProperty` definition.

### 5. **Modify UI Text and Icon:**
   - Change the text displayed on the button and the icon used in the toolbar. Update the `text` and `icon` parameters in the `self.layout.operator` line inside the `add_object_button` function.

 6.Link to a Different Documentation URL:

 If you have specific documentation for your modified script, you can change the `url_manual_mapping` in the `add_object_manual_map` function to link to the appropriate documentation.

Example Modification (Changing Default Shape to a Circle):

     # Modify the verts, edges, and faces to create a circle
verts = [
    Vector((scale_x * cos(angle), scale_y * sin(angle), 0))
    for angle in [radians(i * 15) for i in range(24)]
]
edges = []
faces = [list(range(len(verts)))]

# Modify the object name
mesh = bpy.data.meshes.new(name="New Circle Mesh")

# Modify the text on the button
text="Add Circle"

# Modify the documentation URL mapping
url_manual_mapping = (
    ("bpy.ops.mesh.add_object", "scene_layout/object/circle.html"),
)              
  

Remember to test any modifications in Blender to ensure they work as expected. If you have specific changes in mind or need help with a particular modification, feel free to provide more details!

Latest Technology Posts

Loading...

Latest News

Loading...