Parent a Nurbs curbe and a camera

I am trying and I just don’t understand where the problem is.
In other part of the code I have the same structure but no problem.

Everything works except those 2 are not parented.

#to create curve
#this for reference
#coords_list = [[0,1,2], [1,2,3], [-3,2,1], [0,0,-4]]
#create_curve(coords_list)
#cordo camera=(0,1,2)

def create_curve(coords_list, cordocam):
    #add curve
    crv = bpy.data.curves.new('crv', 'CURVE')
    crv.dimensions = '3D'
    crv.resolution_u = 2
    spline = crv.splines.new(type='NURBS')
    spline.points.add(len(coords_list) - 1) 
    for p, new_co in zip(spline.points, coords_list):
        p.co = (new_co + [1.0])
    spline.use_endpoint_u = True
    spline.use_endpoint_v = True
    obj = bpy.data.objects.new('CurveNya', crv)
    bpy.data.collections['Collection'].objects.link(obj)
    newCurve=bpy.context.object
    #add camera
    bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(cordocam), rotation=(0, 0, 0))
    cameraNew=bpy.context.object
    #Donc parent Curve and camera
    bpy.ops.object.select_all(action='DESELECT')
    newCurve.select_set(True)
    cameraNew.select_set(True)
    bpy.context.view_layer.objects.active = newCurve
    bpy.ops.object.parent_set(type='FOLLOW') 
    return cameraNew

    
    

When you solve your own problem, you have to share.

As always with difficult to solve problems, the solution was extremely simple.

This Normally works.

def create_curve(coords_list, cordocam):
    #add curve
    crv = bpy.data.curves.new('crv', 'CURVE')
    crv.dimensions = '3D'
    crv.resolution_u = 2
    spline = crv.splines.new(type='NURBS')
    spline.points.add(len(coords_list) - 1) 
    for p, new_co in zip(spline.points, coords_list):
        p.co = (new_co + [1.0])
    spline.use_endpoint_u = True
    spline.use_endpoint_v = True
    newCurve = bpy.data.objects.new('CurveNya', crv)
    bpy.data.collections['Collection'].objects.link(newCurve)
    print(newCurve)
    #add camera
    bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(cordocam), rotation=(0, 0, 0))
    cameraNew=bpy.context.object
    print(cameraNew)
    #Donc parent Curve and camera
    bpy.ops.object.select_all(action='DESELECT')
    newCurve.select_set(True)
    cameraNew.select_set(True)
    bpy.context.view_layer.objects.active = newCurve
    bpy.ops.object.parent_set(type='FOLLOW') 
    return cameraNew
1 Like