Code Edit for 2.8x, for 2.7x see previous revision.
- The collection
bpy.data.objects is all objects in a blend file,
whether linked to context scene, another scene or no scene. If not in
the context scene cannot be the context object. Use
- I put the "convenience variable"
context in test scripts, so I can
paste later into panel or operator code where it is passed as a
parameter to most methods, in which case better not to use path addressing from
bpy IMO it gets tedious reading
- Add a UV map named "LightMap" if the mesh doesn't already have one.
- Blender uses radians not degrees as the native unit of rotation. This appears to be one of the anomalies.
- Remember to select faces to be unwrapped.
from math import radians
context = bpy.context
scene = context.scene
vl = context.view_layer
# deselect all to make sure select one at a time
for obj in scene.objects:
if (obj.type == 'MESH'):
vl.objects.active = obj
lm = obj.data.uv_layers.get("LightMap")
if not lm:
lm = obj.data.uv_layers.new(name="LightMap")
lm.active = True
bpy.ops.mesh.select_all(action='SELECT') # for all faces
bpy.ops.uv.smart_project(angle_limit=66, island_margin = 0.02)
Resultant UVMap "LightMap" on Suzanne, using limit angle
radians(66) interpreted as 1.152 degrees