poll() as a python '@staticmethod' was too limiting and didnt allow useful base class...
[blender.git] / release / scripts / templates / operator_uv.py
1 import bpy
2
3 def main(context):
4     obj = context.active_object
5     mesh = obj.data
6
7     is_editmode = (obj.mode == 'EDIT')
8     if is_editmode:
9         bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
10
11
12     if not mesh.active_uv_texture:
13         bpy.ops.mesh.uv_texture_add()
14
15     # adjust UVs
16     for i, uv in enumerate(mesh.active_uv_texture.data):
17         uvs = uv.uv1, uv.uv2, uv.uv3, uv.uv4
18         for j, v_idx in enumerate(mesh.faces[i].verts):
19             if uv.select_uv[j]:
20                 # apply the location of the vertex as a UV
21                 uvs[j][:] = mesh.verts[v_idx].co.xy
22
23
24     if is_editmode:
25         bpy.ops.object.mode_set(mode='EDIT', toggle=False)
26
27
28 class UvOperator(bpy.types.Operator):
29     '''UV Operator description'''
30     bl_idname = "uv.simple_operator"
31     bl_label = "Simple UV Operator"
32
33     @classmethod
34     def poll(cls, context):
35         obj = context.active_object
36         return (obj and obj.type == 'MESH')
37
38     def execute(self, context):
39         main(context)
40         return {'FINISHED'}
41
42
43 if __name__ == "__main__":
44     bpy.ops.uv.simple_operator()