1
$\begingroup$

What am I missing or doing wrong? I made this script to add a cylinder ( just to learn ) but the adjust last operation menu does not work all. What do i need to do to get this linked to the cylinder? In the end I want to add some functions same as current add cylinder but also other funtions.

I guess there is no link between the bl-idname and the cylinder

This is my code

    bl_info = {
    "name": "New Object",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 80, 1),
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_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

radius = 1
depth = 2
location = (0, 0, 0)
rotation = (0, 0, 0)

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

    def execute(self, context):

        bpy.ops.mesh.primitive_cylinder_add(radius=radius, depth=depth, location=location, rotation=rotation)

        return {'FINISHED'}


# Registration
def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Add Object123",
        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()
$\endgroup$
4
$\begingroup$

That's pretty close. Just add real properties to the operator class. Minimal example:

enter image description here

import bpy
from bpy.types import Operator
from bpy.props import IntProperty, FloatProperty, FloatVectorProperty


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

    my_radius : FloatProperty()
    my_depth : IntProperty()
    my_location : FloatVectorProperty()
    my_rotation : FloatVectorProperty()

    def execute(self, context):
        # Call primitve_add
        bpy.ops.mesh.primitive_cylinder_add(
            radius = self.my_radius, 
            depth = self.my_depth, 
            location = self.my_location, 
            rotation = self.my_rotation)

        return {'FINISHED'}


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)

def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)

if __name__ == "__main__":
    register()

Further reading: https://docs.blender.org/api/current/bpy.props.html

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your answer This was what i was looking for. May i ask you where i can find documentation about FloatVectorProperty? For example what kind of property is align? $\endgroup$ – DGRL Dec 15 '19 at 9:52
  • 1
    $\begingroup$ Added the link to the API, there you have a list of all available properties. Properties are basically data types (int, float, string etc) so in case of "align" it depends what do you want to do (Yes/Nope? -> Bool). Also I'd suggest read How to create a custom UI? @DGRL $\endgroup$ – brockmann Dec 15 '19 at 10:34
  • 2
    $\begingroup$ Maybe keep the AddObjectHelper subclass to deal with the usual suspects loc, rot, scale, align. $\endgroup$ – batFINGER Dec 15 '19 at 10:43
2
$\begingroup$

With the AddObjectHelper class

enter image description here

Further to @brockmann's answer

The AddObjectHelper class sets up some of the usual suspects for us, including location, rotation and align. It appears you have used the text editor > templates > python > Addon add object template, so let's stick with it in.

If we simply ourselves add

rotation : FloatVectorProperty()

the rotation will always show in the base unit of radians. 360 degrees = 2pi radians. In scene unit settings degrees is the default set to display in the UI

rotation : FloatVectorProperty(unit='ROTATION')

is required in the property definition to make it behave like other rotation props. Just as unit='LENGTH' will add metres etc.

All this is done for us by the helper class..

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

    # set other args like name and description for all
    vertices : IntProperty(
            name="Vertices",
            description="Number of Vertices",
            min=0,
            default=32)
    radius : IntProperty(default=2)
    depth : FloatProperty(default=2)

    def execute(self, context):

        bpy.ops.mesh.primitive_cylinder_add(
                vertices=self.vertices,
                radius=self.radius, 
                depth=self.depth, 
                # those defined in AddObjectHelper
                align=self.align, 
                location=self.location, 
                rotation=self.rotation)

        return {'FINISHED'}

Note: I've adjusted the classname OBJECT_OT_add_cylinderand bl_idname = "object.add_cylinder" to match desired naming convention

One issue tho is that its draw method puts its stuff first, so may need to add your own draw method to re-sort as desired

    def draw(self, context):
        layout = self.layout
        layout.prop(self, "vertices")
        layout.prop(self, "radius")
        # and so on

Other examples based on add object template

https://blender.stackexchange.com/a/133880/15543

https://blender.stackexchange.com/a/102239/15543

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.