# AttributeError: __exit__ error on custom operator

I'm new to both python and scripting in Blender, so I am sure this is just my misunderstanding of how python and the Blender API work. I have no idea what this error means.

This is the full traceback for my error:

Traceback (most recent call last):
File "/clipImporter.py", line 103, in execute
File "/clipImporter.py", line 78, in scan_files
AttributeError: __exit__

location: <unknown location>:-1

location: <unknown location>:-1


So as the file name suggests, I am just making an importer to bring in clips, objects from libraries or whatever. I have a similar script working for .blend objects, but trying to do it with movie clips is giving me this error. So I have a suspicion that it is my misuse of datablocks. Here's some more relevant code:

The Function

def scan_files(folderpath, datablock):
exec("bpy.context.scene." + datablock + "_directory_list.clear()")
exec("bpy.context.scene." + datablock + "_directory_index = 0")

folderpath = bpy.path.abspath(folderpath)

for file in os.listdir(folderpath):
filepath = folderpath + "\\" + file
with bpy.data.movieclips.load(filepath) as (data_from, data_to):  # this is line 78...I have a feeling it's movieclips.load
for name in eval("data_from" + datablock):
my_item = eval("bpy.context.scene" + datablock + "_directory_list.add()")
my_item.name = name  # these two correspond to custom props group
my_item.file = file


The Class

def execute(self, context):
if bpy.context.scene.dir_clip:
if context.scene.folderpath_clip:
scan_files(context.scene.folderpath_clip, "clip")  #this is line 103

return{'FINISHED'}


I've been hacking and slashing this guy's script to do what I want if you want to see his logic behind his importing. The scan files function is scanning a directory and loading available options to import.

If you need the whole script let me know and I'll do a pasteall, though I'm sure I could probably solve the error if I even knew what AttributeError: __exit__ actually meant. I do come from a bit of a C# background, so an explanation paralleled to that would be fabulous.

Thanks lovely people!

• see this and this – Chebhou Apr 23 '15 at 19:59

focus on the code around the error:

import bpy

files = ['/home/zeffii/GL_fillet.mpeg', '/home/zeffii/GL_fillet2.mpeg']

for filepath in files:
print(data_from, data_to)


Even from that snippet I get AttributeError: __exit__

I may be wrong, but this suggests to me that there is no context manager (in the python sense, not in the bpy.context sense) to use with x.y.z as (a, b) on the bpy.data.movieclips.load() operator. Hence that error. The documentation confirms this. movieclips.load() does return an object though!

If you just want to get stuff done, rewrite it so it doesn't use the with x.y.z as (a,b) context, instead assign to the left.

import bpy

files = ['/home/zeffii/GL_fillet.mpeg', '/home/zeffii/GL_fillet2.mpeg']

for filepath in files:
# you know what you need here.
print(clip.name)
print(clip.size[:])
print(clip.source)

>>> GL_fillet.mpeg
>>> (1280, 720)
>>> MOVIE
>>> GL_fillet2.mpeg
>>> (1280, 720)
>>> MOVIE


Additionally, there's also a typo I think. The original code has

for name in eval("data_from." + datablock):
# blah..


and you omit a necessary . after data_from..

Your scan_files function for movie_clips might start to look something like:

def scan_files(folderpath, datablock):
exec("bpy.context.scene." + datablock + "_directory_list.clear()")
exec("bpy.context.scene." + datablock + "_directory_index = 0")

folderpath = bpy.path.abspath(folderpath)

for _file in os.listdir(folderpath):
filepath = folderpath + "\\" + file

data_from = ...  # clip.name ?
data_to = ... # clip.source ?

for name in eval("data_from." + datablock):
my_item = eval("bpy.context.scene" + datablock + "_directory_list.add()")
my_item.name = name  # these two correspond to custom props group
my_item.file = _file


Without seeing more of your code (got a github repository?) I'm not able to say more. Looks like an interesting project though.

• Ah, thank you so much! You are absolutely right, don't know how I missed that in the documentation. You are a star, thank you for your very thorough answer. Makes total sense. – cassandroid Apr 24 '15 at 14:59
• The less travelled paths of the documentation are often quite terse, it means you're one of the few people to try stuff with that section of the API. – zeffii Apr 24 '15 at 15:06