minor changes to templates\13
[blender.git] / release / scripts / templates_py / operator_mesh_uv.py
1 import bpy
2 import bmesh
3
4
5 def main(context):
6     obj = context.active_object
7     me = obj.data
8     bm = bmesh.from_edit_mesh(me)
9
10     uv_layer = bm.loops.layers.uv.verify()
11     bm.faces.layers.tex.verify()  # currently blender needs both layers.
12
13     # adjust UVs
14     for f in bm.faces:
15         for l in f.loops:
16             luv = l[uv_layer]
17             if luv.select:
18                 # apply the location of the vertex as a UV
19                 luv.uv = l.vert.co.xy
20
21     bmesh.update_edit_mesh(me)
22
23
24 class UvOperator(bpy.types.Operator):
25     """UV Operator description"""
26     bl_idname = "uv.simple_operator"
27     bl_label = "Simple UV Operator"
28
29     @classmethod
30     def poll(cls, context):
31         return (context.mode == 'EDIT_MESH')
32
33     def execute(self, context):
34         main(context)
35         return {'FINISHED'}
36
37
38 def register():
39     bpy.utils.register_class(UvOperator)
40
41
42 def unregister():
43     bpy.utils.unregister_class(UvOperator)
44
45
46 if __name__ == "__main__":
47     register()
48
49     # test call
50     bpy.ops.uv.simple_operator()