# Python context error NameError: name 'context' is not defined

The following is a script that is supposed to access UV info but it fails right away in both Object and Edit modes. I fist tried using Bl-2.69.8, then 2.69 then 2.68 and it happens the same every time. I've also tried adding bpy. to the beginning of context but the result is the same.

Can anyone clear this up?

• Should be bpy.context, it's probably treating context as a unique val which is not defined. Jan 21 '14 at 9:53

What you're looking for is bpy.context, so the first line should be:

obj = bpy.context.active_object


It refers to the bpy module being imported in the first line.

You can also use this at the beginning of your scripts:

import bpy
from bpy import context


The entire script should look like this:

import bpy
import bmesh

ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)

uv_layer = bm.loops.layers.uv.verify()
bm.faces.layers.tex.verify()

for f in bm.faces:
for l in f.loops:
luv = l[uv_layer]
if luv.select:
luv.uv = l.vert.co.xy

bmesh.update_edit_mesh(me)

• @MarcClintDion bpy, not .bpy. Dot not preceded by identifier (accessor), or succeeded by number (floating point), is syntax error.