Welcome to mirror list, hosted at ThFree Co, Russian Federation.

operator_uv.py « templates « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdd0b993f8b448222773d7b10332c08adb655d3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import bpy


def main(context):
    obj = context.active_object
    mesh = obj.data

    is_editmode = (obj.mode == 'EDIT')
    if is_editmode:
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

    if not mesh.uv_textures:
        uvtex = bpy.ops.mesh.uv_texture_add()
    else:
        uvtex = mesh.uv_textures.active

    # adjust UVs
    for i, uv in enumerate(uvtex.data):
        uvs = uv.uv1, uv.uv2, uv.uv3, uv.uv4
        for j, v_idx in enumerate(mesh.faces[i].vertices):
            if uv.select_uv[j]:
                # apply the location of the vertex as a UV
                uvs[j][:] = mesh.vertices[v_idx].co.xy

    if is_editmode:
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)


class UvOperator(bpy.types.Operator):
    """UV Operator description"""
    bl_idname = "uv.simple_operator"
    bl_label = "Simple UV Operator"

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj and obj.type == 'MESH')

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(UvOperator)


def unregister():
    bpy.utils.unregister_class(UvOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.uv.simple_operator()