Open1

blenderのcurveでControlPointを取得

黒狐黒狐

pythonのコードでベジエ曲線のコントロールポイントの情報を取得する方法がわかったので自分用にメモ
参照先:
 How can I know the data paths of certain point of bezier curve?

import bpy

ob = bpy.context.object
assert ob.type == 'CURVE' # throw error if it's not a curve

curve = ob.data
spline = curve.splines.active # let's assume there's only one
assert spline.type == 'BEZIER' # throw error if it's not a bezier

print(len(spline.bezier_points)) # print number of points
print(spline.bezier_points[0].co) # print coordinate of first point
print(spline.bezier_points[0].handle_left) # ... and its left handle's coordinate
print(spline.bezier_points[0].handle_right) # ... as well as the right one

SplineとCurveは別の概念のようです・・。Splineでないと・・できた!
activeにしなくても、これなら名前からアクセスできるよ!

import bpy

cv = bpy.data.curves['BezierCurve']

print('-------')

for sp in cv.splines:

    #sp = cv.splines[0]
    print('----------------------')

    # spでもOK. curveを取得してsplinesの0にいってそこからいろいろ調べられるみたい。
    print(len(sp.bezier_points)) # print number of points
    
    for id, p in enumerate(sp.bezier_points):
        
        print("id:" + str(id))
        print("co:" + str(p.co)) # print coordinate of first point
        print("handle_left:" + str(p.handle_left)) # ... and its left handle's coordinate
        print("handle_right:" + str(p.handle_right)) # ... as well as the right one)

実行結果:
2
id:0
co:<Vector (-1.0000, 0.0000, 0.0000)>
handle_left:<Vector (-1.6308, -0.3195, 0.0000)>
handle_right:<Vector (0.3708, 0.6944, 0.0000)>
id:1
co:<Vector (1.0000, 0.0000, 0.0000)>
handle_left:<Vector (0.0000, 0.0000, 0.0000)>
handle_right:<Vector (2.0000, 0.0000, 0.0000)>

4
id:0
co:<Vector (0.3460, 0.7885, -0.1149)>
handle_left:<Vector (0.0669, 0.7885, -0.1149)>
handle_right:<Vector (0.6251, 0.7885, -0.1149)>
id:1
co:<Vector (1.1643, 0.6105, -0.1149)>
handle_left:<Vector (0.9451, 0.7833, -0.1149)>
handle_right:<Vector (1.2559, 0.5382, -0.1149)>
id:2
co:<Vector (1.7408, 0.2713, -0.1149)>
handle_left:<Vector (1.6272, 0.2981, -0.1149)>
handle_right:<Vector (1.7726, 0.2638, -0.1149)>
id:3
co:<Vector (1.8723, 0.2501, -0.1149)>
handle_left:<Vector (1.8577, 0.2793, -0.1149)>
handle_right:<Vector (1.8869, 0.2209, -0.1149)>