Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

Showing posts with label Curves. Show all posts
Showing posts with label Curves. Show all posts

April 17, 2015

Tube Tool Blender 2.74+

Tool to quickly make tubing between selected faces



The repository is available on github, there is a master branch which is most stable and various experimental branches for code i'm not certain about and exploration of ideas. The controls pretty much explain themselves, but I recommend using F6 after triggering the Operator (currently from W specials " add simple tubing" or spacebar and search for 'tube' )

Enjoy, and bug reports to the tracker please. This might be my last Blender add-on for a while.

June 16, 2013

Normalizing spline points. (make equidistant)

In college I had the opportunity to learn 3dsMax to a reasonable level. It is great in many ways, and one modifier was particularly nice: the normalize spline modifier. Normalize spline places the curve points at evenly spaced distances. If you don't know why that is awesome, you'll probably have to see it to understand.

Prompted by a post on blender.stackexchange i've started writing a blender version of the modifier, I don't foresee it to be a full blown replica but time permitting it will at least produce a replica polyline (edge mesh) with BGL overlaying points so interaction can help a user decide what distances the points should occur at.

more later.

preview code:

Note: This only draws the verts using BGL at their current locations. A nice short bgl snippet. https://gist.github.com/zeffii/5796615
.
Using Sverchok and Scripted Node : https://gist.github.com/zeffii/782e84fbdc8e8f23f64b

August 11, 2011

scripted keyframing of Curve Coordinates

after poking around, it seems this is an ok way to script keyframes for a 'POLY' curve.
If you want NURBS or BEZIER, i suggest reading the rotobezier.py addon by zanqdo, available here, especially CURVE_OT_insert_keyframe


May 17, 2011

Blender 2.5x Python Curve from a List of Coordinates.

Given a list of coordinates in the form of Vector((x,y,z)) it is possible to string them together to get a curve shape. Each Point on a curve is has 4 values (x,y,z,w). W=weight and is a value between 0.010 and 100.

It took me a while to get some intuition in this part of blender bpy. This post is my effort to clear it up for myself and hopefully for others who may struggle with it in the future.
import bpy
from mathutils import Vector

w = 1 # weight
cList = [Vector((0,0,0)),Vector((1,0,0)),Vector((2,0,0)),Vector((2,3,0)),
        Vector((0,2,1))]

curvedata = bpy.data.curves.new(name='Curve', type='CURVE')
curvedata.dimensions = '3D'

objectdata = bpy.data.objects.new("ObjCurve", curvedata)
objectdata.location = (0,0,0) #object origin
bpy.context.scene.objects.link(objectdata)

polyline = curvedata.splines.new('POLY')
polyline.points.add(len(cList)-1)
for num in range(len(cList)):
    x, y, z = cList[num]
    polyline.points[num].co = (x, y, z, w)

or a slightly modified version, for turning it into a reusable function
import bpy
from mathutils import Vector

w = 1 # weight
listOfVectors = [Vector((0,0,0)),Vector((1,0,0)),Vector((2,0,0)),Vector((2,3,0)),
        Vector((0,2,1))]

def MakePolyLine(objname, curvename, cList):
    curvedata = bpy.data.curves.new(name=curvename, type='CURVE')
    curvedata.dimensions = '3D'

    objectdata = bpy.data.objects.new(objname, curvedata)
    objectdata.location = (0,0,0) #object origin
    bpy.context.scene.objects.link(objectdata)

    polyline = curvedata.splines.new('POLY')
    polyline.points.add(len(cList)-1)
    for num in range(len(cList)):
        x, y, z = cList[num]
        polyline.points[num].co = (x, y, z, w)

MakePolyLine("NameOfMyCurveObject", "NameOfMyCurve", listOfVectors)

If the list of vectors has only one Vector, then the curve will use one predefined zero vector (Vector((0,0,0))) and draw a straight line from 0,0,0 to your single Vector. Go into edit mode to see where the points are placed.

Each object can have multiple curves associated with it, they are accessed like this
# returns the number of curves associated with this object
>>> len(bpy.context.active_object.data.splines)

# returns the curve information associated with the first spline (index = 0) 
>>> bpy.context.active_object.data.splines[0]
bpy.data.curves["NameOfMyCurve"].splines[0]

# this returns the coordinates on that curve as a list.
>>> [point.co for point in bpy.context.active_object.data.splines[0].points]
if you want a smooth curve from these points, declare at the time of creation what type from these options ('POLY', 'BEZIER', 'BSPLINE', 'CARDINAL', 'NURBS')
import bpy  
from mathutils import Vector  
  
w = 1 # weight  
listOfVectors = [Vector((0,0,0)),Vector((1,0,0)),Vector((2,0,0)),Vector((2,3,0)),  
        Vector((0,2,1))]  
  
def MakePolyLine(objname, curvename, cList):  
    curvedata = bpy.data.curves.new(name=curvename, type='CURVE')  
    curvedata.dimensions = '3D'  
  
    objectdata = bpy.data.objects.new(objname, curvedata)  
    objectdata.location = (0,0,0) #object origin  
    bpy.context.scene.objects.link(objectdata)  
  
    polyline = curvedata.splines.new('NURBS')  
    polyline.points.add(len(cList)-1)  
    for num in range(len(cList)):  
        x, y, z = cList[num]  
        polyline.points[num].co = (x, y, z, w)  
  
    polyline.order_u = len(polyline.points)-1
    polyline.use_endpoint_u = True
    
  
MakePolyLine("NameOfMyCurveObject", "NameOfMyCurve", listOfVectors)
And here a more stripped down version
import bpy  
from mathutils import Vector  

# weight  
w = 1 

# we don't have to use the Vector() notation.  
listOfVectors = [(0,0,0),(1,0,0),(2,0,0),(2,3,0),(0,2,1)]  
  
def MakePolyLine(objname, curvename, cList):  
    curvedata = bpy.data.curves.new(name=curvename, type='CURVE')  
    curvedata.dimensions = '3D'  
  
    objectdata = bpy.data.objects.new(objname, curvedata)  
    objectdata.location = (0,0,0) #object origin  
    bpy.context.scene.objects.link(objectdata)  
  
    polyline = curvedata.splines.new('NURBS')  
    polyline.points.add(len(cList)-1)  
    for num in range(len(cList)):  
        polyline.points[num].co = (cList[num])+(w,)  
  
    polyline.order_u = len(polyline.points)-1
    polyline.use_endpoint_u = True
    
  
MakePolyLine("NameOfMyCurveObject", "NameOfMyCurve", listOfVectors)