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$
2
$\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?

$\endgroup$
  • $\begingroup$ How did you find out which context members you have to pass to an operator? $\endgroup$ – schlamar Mar 1 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 at 8:18
0
$\begingroup$

Try to change active Collection , in "Master collection" i try to select default "Collection" and this script not work(why i dont know) , but in another child collection it works. for me this work but not in principal collectio (this script is modified , is by CodemanX, i select first object by another way :

import bpy

scene = bpy.context.scene.collection 

obs = []
for ob in scene.objects:

    if ob.type == 'MESH':
        obs.append(ob)

ctx = bpy.context.copy()

ctx['selected_editable_bases'] = [scene.object_bases[ob.name] for ob in obs]

bpy.ops.object.join(ctx)
$\endgroup$
0
$\begingroup$

i found solution for problem: this is for children of an object, i create mesh empty object first "null", children are object children of 1 object now is very fast. in your case follow this example and change your object

obs = []
null.select_set(state=True)
obs.append(null)
for c in children:


    c.select_set(state=True)
    obs.append(c)      



ctx = bpy.context.copy()
ctx['active_object'] = obs[0]
ctx['selected_objects'] = obs



bpy.ops.object.join(ctx)
$\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.