1
$\begingroup$

With the following code, I can export a bezier curve to a file, and with the default bezier curve it works, but as once i add points to it and attempt to run the script it gives an Attribute error.enter image description here

import bpy
import mathutils
import numpy
from string import Template

template = Template("[gd_resource type=\"Curve3D\" format=2]\n\n[resource]\n_data = {\n\"points\": PoolVector3Array($P),\n\"tilts\": PoolRealArray($Z)\n}")

def XYZ_to_XZY(vector):
    temp = mathutils.Vector((0.0, 0.0, 0.0))
    temp.x = vector.x
    temp.y = vector.z
    temp.z = -vector.y
    return temp
if __name__ == "__main__":
    curve = bpy.context.object
    print(curve)
    points_string = " "
    zeros_string = " "
    for point in curve.data.splines.active.bezier_points: # point is a group of 3 vectors for handles and center

        zeros_string += "0, 0, 0, "
        adj_hl = point.handle_left - point.co
        adj_hr = point.handle_right - point.co

        adj_co = XYZ_to_XZY(point.co)
        adj_hl = XYZ_to_XZY(adj_hl)
        adj_hr = XYZ_to_XZY(adj_hr)

        for comp in adj_hl:
            compf = numpy.float32(comp)
            compf = round(compf, 5)
            points_string += str(compf) + ", " #each 3 components of each 3 vectors in left center and right 

        for comp in adj_hr:
            compf = numpy.float32(comp)
            compf = round(compf, 5)
            points_string += str(compf) + ", "

        for comp in adj_co:
            compf = numpy.float32(comp)
            compf = round(compf, 5)
            points_string += str(compf) + ", "

    points_string = points_string[:len(points_string) - 2] + ' '
    zeros_string = zeros_string[:len(zeros_string) - 2] + ' '

    result = template.safe_substitute(P = points_string, Z = zeros_string)

    file = open("curve.tres", "w+")
    file.write(result)
    file.close()
$\endgroup$
  • $\begingroup$ The error message is notifying you that curve.data.splines.active is None Perhaps you could loop splines. for spline in curve.data.splines: instead. $\endgroup$ – batFINGER Dec 2 '19 at 5:46

Your Answer

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

Browse other questions tagged or ask your own question.