0
$\begingroup$

I am new to blender and am using python within blender to do the following:

  1. Read in list of coordinate data for bezier curve objects - DONE
  2. For each unique object within the dataset, take that objects multiple xyz coordinates and plot a connected closed bezier curve from it. - NOT DONE

My Code for Point 2 is shown below:

for i in unique_plotables:
# Creating CurveData
    curveData = bpy.data.curves.new('Slice Object %s' %(i), type='CURVE')
    curveData.dimensions = '3D'
    curveData.resolution_u = 20
# Subsetting my dataset of xyz coords to only coords belonging to desired object
# When the blender_id variable in my dataset = the unique_plotable value the loop is on
    df_object = object3_df[object3_df['blender_id']==i]
# Dropping variables not needed from dataset
    df_object = df_object.drop(['blender_id'], axis = 1)
# Remaining variables left in dataset include x_coord, y_coord, z_coord
# Convert this to a array
    coords = df_object.to_numpy()
# Create new spline for CurveData of Bezier type
    bez = curveData.splines.new('BEZIER')
# Add a number of bezier_points equal to the number of coordinates we have
    count = len(coords)
# Add this many points to the Bez spline
    bez.bezier_points.add(count-1)
    bezier_points_range = range(count)
    bez_points = bez.bezier_points
# For each point in the bezier points, make the .co for the point be equal to the vectorized version of coordinates when the index of coordinates is equal to the bezier point number.
    for j in bezier_points_range:
        bez_point = bez_points[j]
        coord = coords[j]
        bez_point.co = Vector(coord)
# Create the object and link it to the scene
    od = bpy.data.objects.new('Slice Object %s' %(i), curveData)
    od.location=(0,0,0)
    od.data.resolution_u=20
    od.data.bevel_resolution=20
    #od.data.fill_mode = 'FULL'
    od.data.bevel_depth=0.1
    bpy.context.scene.collection.objects.link(od)

In my test data, there are 4 variables: x_coord, y_coord, z_coord, and blender_id. There is one blender_id thus one unique plotable object with 406 rows of x,y,z coordinates. So imagine this loop firing once and making one object with 406 points

When I run this code, it executes fine, but the resulting image in my active screen appears to show each point of the bezier spline not connecting to the previous or next, but rather, leading in a straight line back to 0,0,0

What I would like: Each point to connect to the next and prior point, not extending back to the 0,0,0.

Example image: enter image description here I have hand drawn in the red line indicating what I am trying to have done, and you can hopefully see that instead of this, each point is separate and alone and extending back to 0,0,0

Resources I have checked with include the python blender API documentation, this other post: Poly / Bezier curve from a list of coordinates and Creating curves in Python as well as this medium article https://behreajj.medium.com/scripting-curves-in-blender-with-python-c487097efd13 particularly this persons code snippets when they discuss closed bezier curves from data.

I am unfortunately unable to figure out what is wrong with my code and would appreciate assistance. If I am lacking in any required info please let me know and I apologize.

$\endgroup$
1
$\begingroup$

I found a fix to this I believe, credited to rfabbri on another post here (First link in my question) Comment: "For Bezier, the default (0,0,0) handles won't be adequate, you may want to set them to the points as well: .bezier_points[i].handle_left = .bezier_points[i].handle_right = .bezier_points[i].co"

So I added the last line you see here:

 for j in bezier_points_range:
        bez_point = bez_points[j]
        coord = coords[j]
        bez_point.co = Vector(coord)
        bez_point.handle_left=bez_point.handle_right = bez_point.co

And the resulting image appears to be what I am aiming for.

$\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.