1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ The TypeError will also indicate which line is at fault. What is the purpose of your script? It doesn't seem to do anything except assigning to some unused variables. $\endgroup$ – dr. Sybren Sep 15 '17 at 20:59
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Please accept this answer, so that your question is marked as "answered". $\endgroup$ – dr. Sybren Sep 16 '17 at 22:00
0
$\begingroup$

A suggestion on how to "write to a library and export to obj every hidden object". Note I've assumed also you only want to export mesh objects.

Creates a dummy scene, appends each object, writes to lib and exports scene to obj. Then removes the scene.

import bpy
from os import path
file_path = "/tmp" # some file path.
context = bpy.context
scene = context.scene
# all hidden mesh obs in context scene
mesh_objects = (o for o in scene.objects if o.type == 'MESH' and o.hide)
# all hidden mesh obs in file
#mesh_objects = (o for o in bpy.data.objects if o.type == 'MESH' and o.hide)
# all  mesh obs in file
#mesh_objects = (o for o in bpy.data.objects if o.type == 'MESH')
# add a dummy scene
dummy = bpy.data.scenes.new("dummy")
# make it context
context.screen.scene = dummy

for o in mesh_objects:
    #link to dummy scene
    dummy.objects.link(o)

    bpy.data.libraries.write(
        path.join(file_path, "%s.blend" % o.name),
        {o},
        relative_remap = True
    )

    bpy.ops.export_scene.obj(
        filepath=path.join(file_path, "%s.obj" % o.name),
        use_selection = False, # only one obj in scene
        use_mesh_modifiers = True,
        use_materials = False
        )
    #unlink o
    dummy.objects.unlink(o)

# remove dummy scene
bpy.data.scenes.remove(dummy)
$\endgroup$
  • $\begingroup$ You may want to make mesh_objects a generator expression instead of a list comprehension. That'll save a bit of memory :) $\endgroup$ – dr. Sybren Sep 17 '17 at 19:47
  • 1
    $\begingroup$ @dr.Sybren great suggestion thankyou. $\endgroup$ – batFINGER Sep 18 '17 at 2:09

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.