how to access bpy.data.objects thru bpy.context.scene.objects I'm trying to run this script in a Blender text editor:

import bpy

object_dir = "/somedir"

object_all = bpy.context.scene.objects

for ob in object_all:

    if ob.hide == True:

        object_name = ob.name

        #print(object_name)
        #data_blocks = set(bpy.data.objects[object_name])

        data_blocks = set(ob)


but when I do I get "TypeError: 'Object' object is not iterable".

What's the correct way to access a bpy.data.object thru a bpy.context?
Thanks in advance.



Alright, I was a bit vague. Here's the code:

import bpy

object_dir = "/Users/max/Developer/Content/3D/objects/"

object_all = bpy.context.scene.objects

for ob in object_all:
    if ob.hide == True:
        object_name = ob.name
        print(object_name)

        data_blocks = set(bpy.data.objects[object_name])

        bpy.data.libraries.write(
            object_dir + "_" + object_name + ".blend",
            data_blocks,
            relative_remap = True
        )

        bpy.ops.export_scene.obj(
            filepath = object_dir + "_" + object_name + ".obj",
            use_selection = True,
            use_mesh_modifiers = True,
            use_materials = False
        )


what I want to do is simply write to a library and export to obj every hidden object I have. The error:


  line 12, in  TypeError: 'Object' object is not iterable Error: Python script fail, look in the console for now...


error is referred to this line:


  data_blocks = set(bpy.data.objects[object_name])


Thanks again.
That line of code is overly complex. ob is already a datablock, so you don't have to go through the hoop of getting its name and then looking it up somewhere else. Furthermore, this approach will break when you have linked datablocks from other blend files (then names aren't necessarily unique).

To create a set, you have to either use a set literal like {'abc', 'def'}, or pass an iterable to set(). This is why you get the error message that Object is not iterable. Use either data_blocks = {ob} or data_blocks = set([ob]). I would say the first method is easier to read. It's probably also faster, because you don't have to create an intermediary list first.