Curves -- Overview

Übersicht: Python & Blender


Objectives

How can a path, a solid or surface be defined through curves? These are the next questions that will be clarified. After a given overview, we will use a path for an animation.

Instructions

Tasks:
  1. Create bezier circle and move one of the points.
  2. Extrude the circle and convert it to a mesh.
  3. Colorize the mesh and use different colors for some faces.

Curves: Types and Properties

Modelling simple objects out of basic forms is not always possible. An other way is to use curves, because they can easily be deformed. There are different types of curves in Blender. Common to all is the calculation of the curve by mathematical formulas. All types can be manipulated with Parameters or so called handles. What is easily done in a the 3D View using a mouse is not so easy implemented in a Python script.

Blender supports firstly the bezier curves, as many other programs using vectors. Another type of curve is the NURBS curve, a generalization of Bezier curves. More information about the Curve types can be found under:

Overview to baic types

Each function creates a basic form. All forms are shown in edit mode, because the handles a visualizing the differences.

Bezier-Circle

/blender-moves/f-pathes/bezier-circle.png

bpy.ops.curve.primitive_bezier_circle_add(location=(-3, 5, 0))

Bezier-Curve

/blender-moves/f-pathes/bezier-curve.png

bpy.ops.curve.primitive_bezier_curve_add(location=(3, 5, 0))

Nurbs-Circle

/blender-moves/f-pathes/nurbs-circle.png

bpy.ops.curve.primitive_nurbs_circle_add(location=(-3, 2, 0))

Nurbs-Curve

/blender-moves/f-pathes/nurbs-curve.png

bpy.ops.curve.primitive_nurbs_curve_add(location=(3, 2, 0))

Nurbs-Path

/blender-moves/f-pathes/nurbs-path.png

bpy.ops.curve.primitive_nurbs_path_add(location=(6, 1, 0))

The complete Script

It creates an example of each path object.

blender-moves/f-pathes/curves.py (Source)

#! 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)


Comments