0
$\begingroup$

I'm working on a script that will need to know which way a curve is facing, meaning if all the points on the curve share the same X value, it's facing the X direction. I have the script below that will correctly show the global space vector positions of each control point of the curve.

import bpy

obj = bpy.context.active_object
curve = obj.data
spline = curve.splines[0]
# setup array for points to be written to
pathPoints = []
# Iterate through the control points in the spline and append them to the array
for i in range(0, len(spline.bezier_points)):
    pathPoints.append(obj.matrix_world @ spline.bezier_points[i].co)

# determine if path is facing X, Y, or Z
# TODO

# DEBUGGING
print(pathPoints)

$\endgroup$
  • 1
    $\begingroup$ global = obj.matrix_world @ local (Use * for pre 2.8) $\endgroup$ – batFINGER Aug 18 at 15:03
  • $\begingroup$ @batFINGER so I'd do something like pathPoints.append(spline.bezier_points[i].co @ obj.matrix_world) ? $\endgroup$ – Jeff Lange Aug 18 at 18:50
  • $\begingroup$ @batFINGER I got pathPoints.append(obj.matrix_world @ spline.bezier_points[i].co) to work, but how do I determine what plane is the major plane? $\endgroup$ – Jeff Lange Aug 19 at 5:01
0
$\begingroup$

Here's the code that I got working to solve this problem:

import bpy

obj = bpy.context.active_object
curve = obj.data
spline = curve.splines[0]
# setup array for points to be written to
xGlobalPathPoints = []
yGlobalPathPoints = []
zGlobalPathPoints = []
# Iterate through the control points in the spline and append them to the array
for i in range(0, len(spline.bezier_points)):
    if float("{:.3f}".format(float(obj.location.x)))+0 != 0:
        xGlobalPathPoint = obj.location.x * spline.bezier_points[i].co.x
    else:
        xGlobalPathPoint = spline.bezier_points[i].co.x
    xGlobalPathPoint = "{:.3f}".format(float(xGlobalPathPoint))
    xGlobalPathPoints.append(float(xGlobalPathPoint) + 0)
    if float("{:.3f}".format(float(obj.location.y)))+0 != 0:
        yGlobalPathPoint = obj.location.y * spline.bezier_points[i].co.y
    else:
        yGlobalPathPoint = spline.bezier_points[i].co.y
    yGlobalPathPoint = "{:.3f}".format(float(yGlobalPathPoint))
    yGlobalPathPoints.append(float(yGlobalPathPoint) + 0)
    if float("{:.3f}".format(float(obj.location.z)))+0 != 0:
        zGlobalPathPoint = obj.location.z * spline.bezier_points[i].co.z
    else:
        zGlobalPathPoint = spline.bezier_points[i].co.z
    zGlobalPathPoint = "{:.3f}".format(float(zGlobalPathPoint))
    zGlobalPathPoints.append(float(zGlobalPathPoint) + 0)
# determine if path is facing X, Y, or Z
if len(set(xGlobalPathPoints))==1:
    print("faces X")
if len(set(yGlobalPathPoints))==1:
    print("faces Y")
if len(set(zGlobalPathPoints))==1:
    print("faces Z")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.