0
$\begingroup$

The 2.8 documentation for the context override is found here: https://docs.blender.org/api/blender2.8/bpy.ops.html?highlight=object_bases

override['selected_bases'] = list(bpy.context.scene.object_bases)

is even listed there, but throwing

bpy.context.scene.object_bases

into the python console throws:

Traceback (most recent call last): File "", line 1, in AttributeError: 'Scene' object has no attribute 'object_bases'

I want to merge objects as described by CodemanX here: How to join objects with Python? It worked in 2.79, doesnt in 2.80 and throws:

AttributeError: 'Scene' object has no attribute 'object_bases'

My code snippet:

    proxyname = "ProxyName"
    # create empty new object
    proxymesh = bpy.data.objects.new(proxyname, bpy.data.meshes.new(proxyname))
    bpy.context.collection.objects.link(proxymesh)
    proxymesh.matrix_world = bpy.context.active_object.matrix_world
    # make new, flattened object from all meshes in selection
    duplicants = []
    duplicants.append(proxymesh)
    for obj in bpy.context.selected_objects:
        if obj.type == "MESH":
            duplicant = bpy.data.objects.new("duplicant", obj.to_mesh(bpy.context.depsgraph, True, calc_undeformed=False))
            bpy.context.collection.objects.link(duplicant)
            duplicant.matrix_world = obj.matrix_world
            duplicants.append(duplicant)
    # attach flattened objects to empty object
    ctx = bpy.context.copy()
    ctx['active_object'] = proxymesh
    ctx['selected_objects'] = duplicants
    ctx['selected_editable_bases'] = [bpy.context.scene.object_bases[duplicant.name] for duplicant in duplicants]

So basically, I would like to know how to be able to iterate through the scene's object bases in 2.80?

$\endgroup$
3
$\begingroup$

Appears no need for bases in 2.8 join operator override.

Testing this in python console. Have duped the default cube 3 times, the last dupe "Cube.003" is active and only object selected.

>>> C.object
bpy.data.objects['Cube.003']


>>> C.selected_objects
[bpy.data.objects['Cube.003']]

A list of all the mesh objects in scene to join

>>> obs = [o for o in C.scene.objects if o.type == 'MESH']
>>> obs
[bpy.data.objects['Cube'], bpy.data.objects['Cube.001'], bpy.data.objects['Cube.002'], bpy.data.objects['Cube.003']]

Make a context override dictionary, with only object, active_object, selected_objects, selected_editable_objects as members (possibly don't require "object")

>>> c = {}

>>> c["object"] = c["active_object"] = C.object
>>> c["selected_objects"] = c["selected_editable_objects"] = obs

Run the operator with this override

>>> bpy.ops.object.join(c)
{'FINISHED'}

Resulting scene after running. All four cubes are joined as one "Cube.003"

>>> C.scene.objects[:]
[bpy.data.objects['Lamp'], bpy.data.objects['Camera'], bpy.data.objects['Cube.003']]

Risky look at obs since three of the objects no longer exist.

>>> obs
[<bpy_struct, Object invalid>, <bpy_struct, Object invalid>, <bpy_struct, Object invalid>, bpy.data.objects['Cube.003']]

Related

poll() failed, context incorrect? - Example: bpy.ops.view3d.background_image_add()

Scripting -- Can't figure out how to use correct contexts?

Reliable way to find out the required context for an operator?

How to find an operator's source code?

| improve this answer | |
$\endgroup$
  • $\begingroup$ How did you find out which context members you have to pass to an operator? $\endgroup$ – schlamar Mar 1 '19 at 8:01
  • $\begingroup$ Added some links to answer. In this case I grepped the addons folder for "bases" which gave me an inkling that it was no longer required, (commented out in print3d addon for example) confirmed by testing in console. Would be extremely handy to have operators required context members in the docs, but since they are generated from source would be a fair task. $\endgroup$ – batFINGER Mar 1 '19 at 8:18

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.