0

I manually deleted an object from outliner but my python code still finds that object. Is this because we need to somehow "update" or "synch" the blender's display component with its actual data base?

Thanks!

My python code use bpy.data.curves[0].splines[0] to find the object (a bezier curve). I deleted the first BezierCurve from outliner then added a new BezierCurve object. Then I ran the code through the built-in scripting window. That is when I noticed the operation occurred on the originally deleted BezierCurve.

    import mathutils
    import bpy

    # by zeffi
    # https://github.com/zeffii/rawr/blob/master/blender/scripts/addons_contrib/curve_normalize_spline.py
    def get_points(spline, clean=True):

        knots = spline.bezier_points

        if len(knots) < 2:
            return

        # verts per segment
        r = spline.resolution_u + 1

        # segments in spline
        segments = len(knots)
        print("segments = %d"%segments)
        if not spline.use_cyclic_u:
            segments -= 1

        master_point_list = []
        for i in range(segments):
            inext = (i + 1) % len(knots)

            knot1 = knots[i].co
            handle1 = knots[i].handle_right
            handle2 = knots[inext].handle_left
            knot2 = knots[inext].co

            bezier = knot1, handle1, handle2, knot2, r
            points = mathutils.geometry.interpolate_bezier(*bezier)
            master_point_list.extend(points)

        # some clean up to remove consecutive doubles, this could be smarter...
        if clean:
            old = master_point_list
            good = [v for i, v in enumerate(old[:-1]) if not old[i] == old[i+1]]
            good.append(old[-1])
            return good

        return master_point_list

    spline = bpy.data.curves[0].splines[0]
    points = get_points(spline)

    for i in points:
        bpy.ops.mesh.primitive_uv_sphere_add(size=0.05,location=i)
John
  • 127
  • 1
  • 1
  • 8
  • Your question feels totally unclear,¿what does your __python code__?, ¿are you using bpy or bge? i asumme is bpy but, ¿where in your __python code__ finds __that object__? ¿when your__python code__run? – Strapicarus Oct 30 '17 at 01:07
  • Post your code. – cmomoney Oct 30 '17 at 04:38
  • 2
    You may know that blender data has some persistence, like when you need to save and open a blend file to clear out unused materials. Have a look at this question. – sambler Oct 30 '17 at 09:17
  • 1
    further to @sambler comment, the objects in bpy.data. collections may or may not be linked to any object or to any scene in the file. Referencing them by index IMO is never a good idea. Suggest using spline = context.object.data.splines[0] ie select the curve object as context and run script. – batFINGER Oct 30 '17 at 16:00

1 Answers1

1

Objects are "containers" for data. Mesh objects contain mesh data, curve objects contain curve data, etc. When you deleted the curve, you deleted the container, not the data that it contained. So, in your code your operating on the first curve in the curve data block, which would be the original curve.

Some options you have to resolve this are:

a. Delete the curve from the curve data block b. Find the curve that the curve object contains, and operate on it.

I would do the later:

curve = bpy.data.objects["curve object's name"].data
spline = curve.splines[0]
cmomoney
  • 2,660
  • 12
  • 17