#! bpy
"""
Name: 'curves.py'
Blender: 27x
Group: 'Basics'
Tooltip: 'Show path objects'
"""
import bpy


def add_curves():
    """Create one of every type"""
    
    bpy.ops.curve.primitive_bezier_circle_add(location=(-3, 5, 0))
    bpy.ops.curve.primitive_bezier_curve_add(location=(3, 5, 0))
    bpy.ops.curve.primitive_nurbs_circle_add(location=(-3, 2, 0))
    bpy.ops.curve.primitive_nurbs_curve_add(location=(3, 2, 0))
    bpy.ops.curve.primitive_nurbs_path_add(location=(6, 1, 0))
    

def bezier_circle():
    """ Create bezier circls  
    
    - all control points are deactivated
    - activate the fourth control point 
    """
    
    bpy.ops.curve.primitive_bezier_circle_add(location=(3,-2,0),
                                              rotation=(0, 0, 0))
    bpy.ops.object.mode_set(mode = 'EDIT')
    print(bpy.context.selected_objects)
    bcircle= bpy.context.selected_objects[0]
    bcircle.name = "BezierCircle"    
    for i in range(0,4):
        bcircle.data.splines[0].bezier_points[i].select_control_point = False
        bcircle.data.splines[0].bezier_points[i].select_right_handle = False
        bcircle.data.splines[0].bezier_points[i].select_left_handle = False

    bcircle.data.splines[0].bezier_points[3].select_control_point = True
    bpy.ops.object.mode_set(mode = 'OBJECT')
    for i in bcircle.data.splines[0].bezier_points:
         print(i.co, i.select_control_point)

def bezier_circle_transform():
    """ Circle with transformation """
    
    bpy.ops.curve.primitive_bezier_circle_add(location=(-3,-2,0),
                                              rotation=(0, 0, 0))
    bpy.ops.object.mode_set(mode = 'EDIT')
    bcircle= bpy.context.selected_objects[0]
    bcircle.name = "BezierCircleTransformed"    
    for i in range(0,4):
        bcircle.data.splines[0].bezier_points[i].select_control_point = False
        bcircle.data.splines[0].bezier_points[i].select_right_handle = False
        bcircle.data.splines[0].bezier_points[i].select_left_handle = False
    bcircle.data.splines[0].bezier_points[3].select_control_point = True
    # not working!
    #bpy.ops.curve.extrude_move(CURVE_OT_extrude={"mode":'TRANSLATION'}, 
    #                           TRANSFORM_OT_translate={"value":(-1, -1, 0)})

    for i in bcircle.data.splines[0].bezier_points:
        print(i.co, i.select_control_point)


if __name__ == "__main__":
    # check poll() to avoid exception. 
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode = 'OBJECT')

    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()


    bpy.ops.object.select_by_type(type='CURVE')
    bpy.ops.object.delete()

    add_curves()   
    bezier_circle()
    bezier_circle_transform()

    # check poll() to avoid exception.
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode = 'OBJECT')
    bpy.context.area.type = 'VIEW_3D'
    bpy.ops.view3d.viewnumpad(type='TOP', align_active=False)