AddObject Helper addon

I made a little addon ripped from code I used in a personal inhouse addon that I made for making objects a set size and naming them based on the name field and the size, and add them to a collection based on the root name if needed. I hope this is something that people might find useful as I do.

Determine if using IN or mm, input the measurements desired and choose the primitive to be made. Generate the object and then if need be, add it and other selected objects to a collection based on that name.

I am just now learning from watching videos how to use properties, so I am sure someone will point out the weaknesses in the code - and I look forward to it, as I want to learn the right way to do things.

https://youtu.be/PIqZPeiW3zs

3 Likes

Grats on your 1st addon.

A few notes / observations.

PEP8

Your addon mentions pep8 compliance however you did not actually check it.
http://pep8online.com/

Imports

Verify what you actually need to import and what is extra.

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

Pre-defined Enum Property and operator

enum1

Because you are using a pre-defined enumerator property you can make better use of the required fields.

enum2

In this case:

  • Identifier is the icon name
  • Name is the friendly name
  • Description is the actual end command of bpy.ops.mesh.

As such your operator button can reflect exactly which operation is going to occur.
(reference How to structure add-on code?)

panel

And you no longer need to test:

if mytool.my_enum == 'OP1':
    bpy.ops.mesh.primitive_cube_add()
if mytool.my_enum == 'OP2':
    bpy.ops.mesh.primitive_plane_add()
if mytool.my_enum == 'OP3':
    bpy.ops.mesh.primitive_cylinder_add()
if mytool.my_enum == 'OP4':
    bpy.ops.mesh.primitive_uv_sphere_add()
if mytool.my_enum == 'OP5':
    bpy.ops.mesh.primitive_torus_add()
if mytool.my_enum == 'OP6':
    bpy.ops.mesh.primitive_monkey_add()
if mytool.my_enum == 'OP7':
    bpy.ops.mesh.primitive_cone_add()
if mytool.my_enum == 'OP8':
    bpy.ops.mesh.primitive_circle_add()

Instead:

operator_class

Your operator becomes simplified by passing a simple string argument to it to perform exactly the operation you desire.

operator_call

Similar can be done with your unit scaling calculation simply passing the scale factor to the operator.
This has the advantage of:

  • Not changing the users units settings
  • The scaling can be passed on the initial object creation instead of applied as a second bpy.ops command
    • bpy.ops.mesh.primitive_cube_add(scale=(2, 1, 1))
    • Automatically does the apply scale transform such that the object will be unit scale (1,1,1) with dimensions (2,1,1)

Register Unregister

Be careful with your indentation:

You are trying to add and delete your pointer every iteration of the for loop.

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
        bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MyAddProps)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
        del bpy.types.Scene.my_tool

Should be:

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MyAddProps)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.my_tool
2 Likes

Scale versus Dimension - the usage for me is exactly that, the creation of an object at a specific measurement and not just at a scale that is ‘like’ size of one that measurement. I mean that for my use, I add the measurement to match dieline art that I have to build to for matching carton designs, and that’s the reason behind the manipulation of the units and the specific measurement input.

Thank you for all this - I am going to have to try to ‘catch up’ in reading this and applying all of it, but this is gold, thankyou for taking the time to help teach me.