Page MenuHome

Add curve support to bpy_extras/object_utils/add_object
Needs ReviewPublic

Authored by Peter Staples (batfinger) on Aug 7 2016, 3:52 PM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

Added support for curve objects using bpy_extras.object_utils.object_data_add.
A couple of operators and id collection methods had to be changed with regard to object type.

Diff Detail

Repository
rB Blender

Event Timeline

Peter Staples (batfinger) retitled this revision from to Add curve support to bpy_extras/object_utils/add_object.
Peter Staples (batfinger) updated this object.
Peter Staples (batfinger) set the repository for this revision to rB Blender.

Can you explain a bit more what exactly this patch fixes? What's the steps to see the issue?

This comment was removed by Peter Staples (batfinger).
Peter Staples (batfinger) added a comment.EditedAug 24 2016, 7:16 AM

A test script, the addon add object script template changed to add a curve object. It will work in OBJECT mode, but throw error in EDIT mode. The submitted diff will fix this.

It is very handy to use the bpy_extras.object_utils to add other types of objects, both in OBJECT and EDIT mode as it automatically creates an active operator draw for the location, rotation, scale and align to view. IMO there seems no reason that this cannot be done for other object types other than mesh.

NOTE: Could be expanded for armatures also.
bl_info = {
    "name": "New Curve",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 75, 0),
    "location": "View3D > Add > Curve > New Object",
    "description": "Adds a new Curve Object",
    "warning": "",
    "wiki_url": "",
    "category": "Add Curve",
    }


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_curve(self, context):
    scale_x = self.scale.x
    scale_y = self.scale.y

    coords = [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)),
            ]

    curve = bpy.data.curves.new("New Curve", 'CURVE')
    curve.dimensions = '3D'
    curve.resolution_u = 2

    # map coords to spline
    polyline = curve.splines.new('POLY')
    polyline.points.add(len(coords) - 1)
    for i, co in enumerate(coords):
        x,y,z = co
        polyline.points[i].co = (x, y, z, 1)
    
    object_data_add(context, curve, operator=self)


class OBJECT_OT_add_curve(Operator, AddObjectHelper):
    """Create a new Curve Object"""
    bl_idname = "curve.add_object"
    bl_label = "Add Curve 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_curve(self, context)

        return {'FINISHED'}


# Registration

def add_curve_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_curve.bl_idname,
        text="Add Curve",
        icon='PLUGIN')

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

    bpy.types.INFO_MT_curve_add.append(add_curve_button)


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

    bpy.types.INFO_MT_curve_add.remove(add_curve_button)


if __name__ == "__main__":
    register()