If there is a curve selected, I want to get the 3d position on the curve via the percentage. For example: get the 3d postion on the curve at 1/5 of the curve length. get the 3d postion on the curve at 2/5 of the curve length.
$\begingroup$
$\endgroup$
2
-
$\begingroup$ what is the type of the curve in [‘RESOLUTION’, ‘SEGMENTS’, ‘SPLINE’]? $\endgroup$– X YCommented Jul 21, 2022 at 11:40
-
$\begingroup$ bezier curve. I want to get uniform positions along curve, then put mesh vertices to the positions. The 3d coordinates should be in global space. $\endgroup$– Ling ZACommented Jul 21, 2022 at 11:47
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
2
Get position of n% length of curve
import bpy
# create curve and empty
bpy.ops.curve.primitive_bezier_curve_add(enter_editmode=False, align='WORLD', location=(2, 0, 0), scale=(1, 1, 1))
curve = bpy.context.object
curve.data.resolution_u = 64
bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
empty = bpy.context.object
# make constraint
bpy.ops.object.constraint_add(type='FOLLOW_PATH')
empty.constraints["Follow Path"].target = curve
bpy.ops.constraint.followpath_path_animate(constraint="Follow Path", owner='OBJECT')
curve.data.path_duration = 100
# get position of 50% length
curve.data.eval_time = 50
bpy.context.view_layer.update()
matrix = empty.matrix_local
print(matrix[0][3], matrix[1][3], matrix[2][3])
-
$\begingroup$ awesome! It works well. But is there no direct API to access the positions on curve? $\endgroup$– Ling ZACommented Jul 21, 2022 at 13:59
-
$\begingroup$ It seems not. But you can access
splines[0].bezier_points
to get the control points to calculate the position, but the math is not easy. $\endgroup$– X YCommented Jul 21, 2022 at 14:40