2
$\begingroup$

I placed some bezier curves in a scene. This scene is getting exported as an .obj file later with a scaling factor of 11 and +X-axis as Forward and +Z-Axis as Up. as In a further project some insects shall move along these curves. Now I need the handle and control points of these bezier curves in world coordinates so I can use these coordinates to calculate the bezier curves in my project. I wrote a script for that:

import bpy
import os

ob = bpy.context.object
currPath = os.path.splitext(bpy.data.filepath)[0]+".txt"
file = open(currPath, "w")

for curves in bpy.data.curves:
    for splines in curves.splines:
        for x in range(len(splines.bezier_points)): 
            file.write("p ")
            handle_left = ob.matrix_world * splines.bezier_points[x].handle_left
            handle_left *= 11
            co = ob.matrix_world * splines.bezier_points[x].co
            co *= 11
            handle_right = ob.matrix_world * splines.bezier_points[x].handle_right
            handle_right *= 11
            file.write("%.3f " % (handle_left.x))
            file.write("%.3f " % (handle_left.y))
            file.write("%.3f " % (handle_left.z)) 
            file.write("%.3f " % (co.x)) 
            file.write("%.3f " % (co.y)) 
            file.write("%.3f " % (co.z))
            file.write("%.3f " % (handle_right.x))
            file.write("%.3f " % (handle_right.y)) 
            file.write("%.3f " % (handle_right.z))
            file.write("\n")
    file.write("n\n")

file.close()

The bezier curves are not at the position where they should be in my project. Is something wrong with the script?

$\endgroup$
  • $\begingroup$ This looks correct, suggest you don't export add curves in bpy.data.curves, instead export curves from the active scene, since its possible there are curves which are not visible in the 3D-view being exported. $\endgroup$ – ideasman42 May 12 '14 at 2:37
  • $\begingroup$ The export looks right - are you re-creating the curves right in the other project? Are they randomly wrong or is there a pattern to how much they are out? $\endgroup$ – sambler May 12 '14 at 5:44
  • $\begingroup$ @sambler: Yes I'm recreating them in the other project. It seems there is a pattern to how much they are out. What is bothering me is the fact that I use one matrix to transform the points of all bezier curves. Shouldn't every curve has its own matrix? And another problem is the changing of the axis. Don't I have to flip some values in the script according to my export of the whole scene? $\endgroup$ – Stan May 12 '14 at 13:01
2
$\begingroup$

Now I have found a way to export the bezier curves in world coordinates. To do so I name all my bezier curves "BezierCurve01", "BezierCurve02",...,"BezierCurve18" and so on. I also have to make sure that the curves are in exactly this order in the table bpy.data.curves. This is achieved by simply renaming every curve. Then I can run the following script to get the desired output:

import bpy
import os

currPath = os.path.splitext(bpy.data.filepath)[0]+".txt"
file = open(currPath, "w") 
i = 1

for curves in bpy.data.curves:
    idx = str(i)
    if i < 10:
      bezier = "BezierCurve0"+idx
    else:
      bezier = "BezierCurve"+idx
    ob = bpy.data.objects.get(bezier, None)
    for splines in curves.splines:
        for x in range(len(splines.bezier_points)): 
            file.write("p ")
            handle_left = ob.matrix_world * splines.bezier_points[x].handle_left
            handle_left *= 11
            co = ob.matrix_world * splines.bezier_points[x].co
            co *= 11
            handle_right = ob.matrix_world * splines.bezier_points[x].handle_right
            handle_right *= 11
            file.write("%.3f " % (handle_left.y))
            file.write("%.3f " % (-handle_left.x))
            file.write("%.3f " % (handle_left.z)) 
            file.write("%.3f " % (co.y)) 
            file.write("%.3f " % (-co.x)) 
            file.write("%.3f " % (co.z))
            file.write("%.3f " % (handle_right.y))
            file.write("%.3f " % (-handle_right.x)) 
            file.write("%.3f " % (handle_right.z))
            file.write("\n")
    file.write("n\n")
    i = i + 1

file.close()

This only works for 20 curves and has to be adjusted properly for more.

$\endgroup$
2
$\begingroup$

thanks for your work so i decide to post my work here....

now it will exports all bezier_points in all curve(the data will be used in a three.js project

import bpy
import os


currPath = os.path.splitext(bpy.data.filepath)[0]+ ".curves.js"
file = open(currPath, "w") 

file.write('var curves = {\n')
for ob in bpy.data.objects.values() : 
  if ob.type == 'CURVE' :
    file.write( '"%s":\n' % ob.name)
    for spline in ob.data.splines :
      if len(spline.bezier_points) > 0 :
        file.write("[")
        for bezier_point in spline.bezier_points.values() : 
          handle_left  = ob.matrix_world * bezier_point.handle_left
          co           = ob.matrix_world * bezier_point.co
          handle_right = ob.matrix_world * bezier_point.handle_right

          file.write("[[%.3f, %.3f, %.3f],  " % (handle_left.x, handle_left.y, handle_left.z ))
          file.write("[%.3f, %.3f, %.3f],  " % (co.x, co.y, co.z ))
          file.write("[%.3f, %.3f, %.3f]],\n  " % (handle_right.x, handle_right.y, handle_right.z ))

    file.write("],\n")
file.write("}\n")
file.close()
$\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.