Skip to main content
added 113 characters in body
Source Link
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

The function object_data_add is designed for use in conjunction with the AddObjectHelper class. Passed via It creates a new object, linked to the operator=selfcontext argument such that the newly created object can be aligned andcollection objects, linked to our new mesh, transformed using thevia location, rotation and align properties of the operatoroperator.

That is why it is passed (context, mesh, operator=self) , eg if the new object is ob the code of object_data_add(...) could include

ob.data = mesh
ob.location = operator.location

The function object_data_add is designed for use in conjunction with the AddObjectHelper class. Passed via the operator=self argument such that the newly created object can be aligned and transformed using the properties of the operator, eg if the new object is ob the code of object_data_add(...) could include

ob.location = operator.location

The function object_data_add is designed for use in conjunction with the AddObjectHelper class. It creates a new object, linked to the context collection objects, linked to our new mesh, transformed via location, rotation and align properties of the operator.

That is why it is passed (context, mesh, operator=self) , eg if the new object is ob the code of object_data_add(...) could include

ob.data = mesh
ob.location = operator.location
Source Link
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

Question COde:

  • Makes a new mesh.

  • Makes a new object that links to new empty mesh

  • Links object to collection makes it active and selected

  • Links the empty mesh to object again (new object is now context)

  • Makes the bmesh and loads into mesh

Makes the mesh ok and adds a new object linked to that mesh to scene collections Ok... but it is not being transformed / aligned at all.

I dont get it tbh When i use object_data_add the location and rotation and align is just working fine on the second mesh that is placed ( let me be clear i want only 1 mesh to be placed) Why is it not working on the mesh i created? Im not talking about scaling but the 3 basic properties (location , rotation and align)

Because that is being done for you by the imported

AddObjectHelper and object_data_add

The function object_data_add is designed for use in conjunction with the AddObjectHelper class. Passed via the operator=self argument such that the newly created object can be aligned and transformed using the properties of the operator, eg if the new object is ob the code of object_data_add(...) could include

ob.location = operator.location

to position the object. The code is available to peruse 2.8x/scripts/modules/bpy_extras/object_utils.py

Changes

  • Conformed to operator naming standard

  • Removed un-needed imports

  • Removed un-needed documentation links

  • Changed around the methodology somewhat. The function add_plane_mesh() takes no argument and returns a mesh, regardless of operator or context. This makes it very re-usable, for example if addon is in a folder named "test_addon" could

        from test_addon import add_plane_object
    

    to use elsewhere. Moved object_data_add(...) into the operator's execute method conveniently with self and context as arguments.

Amended question code.

bl_info = {
    "name": "Test object",
    "author": "DGRL",
    "version": (1, 0),
    "blender": (2, 80, 1),
    "location": "View3D > Add > Mesh > DGRL",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
}

import bpy
import bmesh
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add


def add_plane_mesh():

    verts = [(1, 1, 0), (-1, 1, 0), (-1, 0.9, 0), (-0.25, 0.9, 0),] 

    mesh = bpy.data.meshes.new("mesh") 
    bm = bmesh.new()

    bm.faces.new(bm.verts.new(v) for v in verts)
    bm.to_mesh(mesh)  
    bm.free()
    return mesh

class MESH_OT_add_object(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_object" 
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}


    def execute(self, context):
        object_data_add(
                context, 
                add_plane_mesh(), 
                operator=self)

        return {'FINISHED'}

def add_object_button(self, context):
    self.layout.operator(
        MESH_OT_add_object.bl_idname,
        text="Add Object",
        icon='PLUGIN')


def register():
    bpy.utils.register_class(MESH_OT_add_object)        
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(MESH_OT_add_object)        
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()