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

world_scale_uv.py « op « magic_uv - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41f258525f3302c60b47c7ee523ea806159f8144 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# SPDX-License-Identifier: GPL-2.0-or-later

# <pep8-80 compliant>

__author__ = "McBuff, Nutti <nutti.metro@gmail.com>"
__status__ = "production"
__version__ = "6.6"
__date__ = "22 Apr 2022"

from math import sqrt

import bpy
from bpy.props import (
    EnumProperty,
    FloatProperty,
    IntVectorProperty,
    BoolProperty,
)
import bmesh
from mathutils import Vector

from .. import common
from ..utils.bl_class_registry import BlClassRegistry
from ..utils.property_class_registry import PropertyClassRegistry
from ..utils import compatibility as compat


def _is_valid_context_for_measure(context):
    # only 'VIEW_3D' space is allowed to execute
    if not common.is_valid_space(context, ['VIEW_3D']):
        return False

    # Multiple objects editing mode is not supported in this feature.
    objs = common.get_uv_editable_objects(context)
    if len(objs) != 1:
        return False

    # only edit mode is allowed to execute
    if context.object.mode != 'EDIT':
        return False

    return True


def _is_valid_context_for_apply(context):
    # only 'VIEW_3D' space is allowed to execute
    if not common.is_valid_space(context, ['VIEW_3D']):
        return False

    objs = common.get_uv_editable_objects(context)
    if not objs:
        return False

    # only edit mode is allowed to execute
    if context.object.mode != 'EDIT':
        return False

    return True


def _measure_wsuv_info(obj, calc_method='MESH',
                       tex_selection_method='FIRST', tex_size=None,
                       only_selected=True):
    mesh_areas = common.measure_mesh_area(obj, calc_method, only_selected)
    uv_areas = common.measure_uv_area(obj, calc_method, tex_selection_method,
                                      tex_size, only_selected)

    if not uv_areas:
        return None, mesh_areas, None

    if len(mesh_areas) != len(uv_areas):
        raise ValueError("mesh_area and uv_area must be same length")

    densities = []
    for mesh_area, uv_area in zip(mesh_areas, uv_areas):
        if mesh_area == 0.0:
            densities.append(0.0)
        else:
            densities.append(sqrt(uv_area) / sqrt(mesh_area))

    return uv_areas, mesh_areas, densities


def _measure_wsuv_info_from_faces(obj, bm, faces, uv_layer, tex_layer,
                                  tex_selection_method='FIRST', tex_size=None):
    mesh_area = common.measure_mesh_area_from_faces(bm, faces)
    uv_area = common.measure_uv_area_from_faces(
        obj, bm, faces, uv_layer, tex_layer, tex_selection_method, tex_size)

    if not uv_area:
        return None, mesh_area, None

    if mesh_area == 0.0:
        density = 0.0
    else:
        density = sqrt(uv_area) / sqrt(mesh_area)

    return uv_area, mesh_area, density


def _apply(faces, uv_layer, origin, factor):
    # calculate origin
    if origin == 'CENTER':
        origin = Vector((0.0, 0.0))
        num = 0
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin = origin + uv
                num = num + 1
        origin = origin / num
    elif origin == 'LEFT_TOP':
        origin = Vector((100000.0, -100000.0))
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = min(origin.x, uv.x)
                origin.y = max(origin.y, uv.y)
    elif origin == 'LEFT_CENTER':
        origin = Vector((100000.0, 0.0))
        num = 0
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = min(origin.x, uv.x)
                origin.y = origin.y + uv.y
                num = num + 1
        origin.y = origin.y / num
    elif origin == 'LEFT_BOTTOM':
        origin = Vector((100000.0, 100000.0))
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = min(origin.x, uv.x)
                origin.y = min(origin.y, uv.y)
    elif origin == 'CENTER_TOP':
        origin = Vector((0.0, -100000.0))
        num = 0
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = origin.x + uv.x
                origin.y = max(origin.y, uv.y)
                num = num + 1
        origin.x = origin.x / num
    elif origin == 'CENTER_BOTTOM':
        origin = Vector((0.0, 100000.0))
        num = 0
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = origin.x + uv.x
                origin.y = min(origin.y, uv.y)
                num = num + 1
        origin.x = origin.x / num
    elif origin == 'RIGHT_TOP':
        origin = Vector((-100000.0, -100000.0))
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = max(origin.x, uv.x)
                origin.y = max(origin.y, uv.y)
    elif origin == 'RIGHT_CENTER':
        origin = Vector((-100000.0, 0.0))
        num = 0
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = max(origin.x, uv.x)
                origin.y = origin.y + uv.y
                num = num + 1
        origin.y = origin.y / num
    elif origin == 'RIGHT_BOTTOM':
        origin = Vector((-100000.0, 100000.0))
        for f in faces:
            for l in f.loops:
                uv = l[uv_layer].uv
                origin.x = max(origin.x, uv.x)
                origin.y = min(origin.y, uv.y)

    # update UV coordinate
    for f in faces:
        for l in f.loops:
            uv = l[uv_layer].uv
            diff = uv - origin
            l[uv_layer].uv = origin + diff * factor


def _get_target_textures(_, __):
    objs = common.get_uv_editable_objects(bpy.context)
    images = []
    for obj in objs:
        images.extend(common.find_images(obj))

    items = []
    items.append(("[Average]", "[Average]", "Average of all textures"))
    items.append(("[Max]", "[Max]", "Max of all textures"))
    items.append(("[Min]", "[Min]", "Min of all textures"))
    items.extend([(img.name, img.name, "") for img in images])
    return items


@PropertyClassRegistry()
class _Properties:
    idname = "world_scale_uv"

    @classmethod
    def init_props(cls, scene):
        scene.muv_world_scale_uv_enabled = BoolProperty(
            name="World Scale UV Enabled",
            description="World Scale UV is enabled",
            default=False
        )
        scene.muv_world_scale_uv_src_mesh_area = FloatProperty(
            name="Mesh Area",
            description="Source Mesh Area",
            default=0.0,
            min=0.0
        )
        scene.muv_world_scale_uv_src_uv_area = FloatProperty(
            name="UV Area",
            description="Source UV Area (Average if calculation method is UV "
                        "Island or Face)",
            default=0.0,
            min=0.0
        )
        scene.muv_world_scale_uv_src_density = FloatProperty(
            name="Density",
            description="Source Texel Density",
            default=0.0,
            min=0.0
        )
        scene.muv_world_scale_uv_tgt_density = FloatProperty(
            name="Density",
            description="Target Texel Density",
            default=0.0,
            min=0.0
        )
        scene.muv_world_scale_uv_tgt_scaling_factor = FloatProperty(
            name="Scaling Factor",
            default=1.0,
            max=1000.0,
            min=0.00001
        )
        scene.muv_world_scale_uv_tgt_texture_size = IntVectorProperty(
            name="Texture Size",
            size=2,
            min=1,
            soft_max=10240,
            default=(1024, 1024),
        )
        scene.muv_world_scale_uv_mode = EnumProperty(
            name="Mode",
            description="Density calculation mode",
            items=[
                ('PROPORTIONAL_TO_MESH', "Proportional to Mesh",
                 "Apply density proportionaled by mesh size"),
                ('SCALING_DENSITY', "Scaling Density",
                 "Apply scaled density from source"),
                ('SAME_DENSITY', "Same Density",
                 "Apply same density of source"),
                ('MANUAL', "Manual", "Specify density and size by manual"),
            ],
            default='MANUAL'
        )
        scene.muv_world_scale_uv_origin = EnumProperty(
            name="Origin",
            description="Aspect Origin",
            items=[
                ('CENTER', "Center", "Center"),
                ('LEFT_TOP', "Left Top", "Left Bottom"),
                ('LEFT_CENTER', "Left Center", "Left Center"),
                ('LEFT_BOTTOM', "Left Bottom", "Left Bottom"),
                ('CENTER_TOP', "Center Top", "Center Top"),
                ('CENTER_BOTTOM', "Center Bottom", "Center Bottom"),
                ('RIGHT_TOP', "Right Top", "Right Top"),
                ('RIGHT_CENTER', "Right Center", "Right Center"),
                ('RIGHT_BOTTOM', "Right Bottom", "Right Bottom")

            ],
            default='CENTER',
        )
        scene.muv_world_scale_uv_measure_tgt_texture = EnumProperty(
            name="Texture",
            description="Texture to be measured",
            items=_get_target_textures
        )
        scene.muv_world_scale_uv_apply_tgt_texture = EnumProperty(
            name="Texture",
            description="Texture to be applied",
            items=_get_target_textures
        )
        scene.muv_world_scale_uv_tgt_area_calc_method = EnumProperty(
            name="Area Calculation Method",
            description="How to calculate target area",
            items=[
                ('MESH', "Mesh", "Calculate area by whole faces in mesh"),
                ('UV ISLAND', "UV Island", "Calculate area each UV islands"),
                ('FACE', "Face", "Calculate area each face")
            ],
            default='MESH'
        )
        scene.muv_world_scale_uv_measure_only_selected = BoolProperty(
            name="Only Selected",
            description="Measure with only selected faces",
            default=True,
        )
        scene.muv_world_scale_uv_apply_only_selected = BoolProperty(
            name="Only Selected",
            description="Apply to only selected faces",
            default=True,
        )

    @classmethod
    def del_props(cls, scene):
        del scene.muv_world_scale_uv_enabled
        del scene.muv_world_scale_uv_src_mesh_area
        del scene.muv_world_scale_uv_src_uv_area
        del scene.muv_world_scale_uv_src_density
        del scene.muv_world_scale_uv_tgt_density
        del scene.muv_world_scale_uv_tgt_scaling_factor
        del scene.muv_world_scale_uv_mode
        del scene.muv_world_scale_uv_origin
        del scene.muv_world_scale_uv_measure_tgt_texture
        del scene.muv_world_scale_uv_apply_tgt_texture
        del scene.muv_world_scale_uv_tgt_area_calc_method
        del scene.muv_world_scale_uv_measure_only_selected
        del scene.muv_world_scale_uv_apply_only_selected


@BlClassRegistry()
@compat.make_annotations
class MUV_OT_WorldScaleUV_Measure(bpy.types.Operator):
    """
    Operation class: Measure face size
    """

    bl_idname = "uv.muv_world_scale_uv_measure"
    bl_label = "Measure World Scale UV"
    bl_description = "Measure face size for scale calculation"
    bl_options = {'REGISTER', 'UNDO'}

    tgt_texture = EnumProperty(
        name="Texture",
        description="Texture to be applied",
        items=_get_target_textures
    )
    only_selected = BoolProperty(
        name="Only Selected",
        description="Measure with only selected faces",
        default=True,
    )

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context_for_measure(context)

    @staticmethod
    def setup_argument(ops, scene):
        try:
            ops.tgt_texture = scene.muv_world_scale_uv_measure_tgt_texture
        except TypeError:
            # Workaround for the error raised when the items of EnumProperty
            # are deleted.
            ops.tgt_texture = "[Average]"
        ops.only_selected = scene.muv_world_scale_uv_measure_only_selected

    def execute(self, context):
        sc = context.scene
        objs = common.get_uv_editable_objects(context)
        # poll() method ensures that only one object is selected.
        obj = objs[0]

        if self.tgt_texture == "[Average]":
            uv_areas, mesh_areas, densities = _measure_wsuv_info(
                obj, calc_method='MESH', tex_selection_method='AVERAGE',
                only_selected=self.only_selected)
        elif self.tgt_texture == "[Max]":
            uv_areas, mesh_areas, densities = _measure_wsuv_info(
                obj, calc_method='MESH', tex_selection_method='MAX',
                only_selected=self.only_selected)
        elif self.tgt_texture == "[Min]":
            uv_areas, mesh_areas, densities = _measure_wsuv_info(
                obj, calc_method='MESH', tex_selection_method='MIN',
                only_selected=self.only_selected)
        else:
            texture = bpy.data.images[self.tgt_texture]
            uv_areas, mesh_areas, densities = _measure_wsuv_info(
                obj, calc_method='MESH', tex_selection_method='USER_SPECIFIED',
                only_selected=self.only_selected, tex_size=texture.size)
        if not uv_areas:
            self.report({'WARNING'},
                        "Object must have more than one UV map and texture")
            return {'CANCELLED'}

        sc.muv_world_scale_uv_src_uv_area = uv_areas[0]
        sc.muv_world_scale_uv_src_mesh_area = mesh_areas[0]
        sc.muv_world_scale_uv_src_density = densities[0]

        self.report({'INFO'},
                    "UV Area: {0}, Mesh Area: {1}, Texel Density: {2}"
                    .format(uv_areas[0], mesh_areas[0], densities[0]))

        return {'FINISHED'}


@BlClassRegistry()
@compat.make_annotations
class MUV_OT_WorldScaleUV_ApplyManual(bpy.types.Operator):
    """
    Operation class: Apply scaled UV (Manual)
    """

    bl_idname = "uv.muv_world_scale_uv_apply_manual"
    bl_label = "Apply World Scale UV (Manual)"
    bl_description = "Apply scaled UV based on user specification"
    bl_options = {'REGISTER', 'UNDO'}

    tgt_density = FloatProperty(
        name="Density",
        description="Target Texel Density",
        default=1.0,
        min=0.0
    )
    tgt_texture_size = IntVectorProperty(
        name="Texture Size",
        size=2,
        min=1,
        soft_max=10240,
        default=(1024, 1024),
    )
    origin = EnumProperty(
        name="Origin",
        description="Aspect Origin",
        items=[
            ('CENTER', "Center", "Center"),
            ('LEFT_TOP', "Left Top", "Left Bottom"),
            ('LEFT_CENTER', "Left Center", "Left Center"),
            ('LEFT_BOTTOM', "Left Bottom", "Left Bottom"),
            ('CENTER_TOP', "Center Top", "Center Top"),
            ('CENTER_BOTTOM', "Center Bottom", "Center Bottom"),
            ('RIGHT_TOP', "Right Top", "Right Top"),
            ('RIGHT_CENTER', "Right Center", "Right Center"),
            ('RIGHT_BOTTOM', "Right Bottom", "Right Bottom")

        ],
        default='CENTER'
    )
    show_dialog = BoolProperty(
        name="Show Diaglog Menu",
        description="Show dialog menu if true",
        default=True,
        options={'HIDDEN', 'SKIP_SAVE'}
    )
    tgt_area_calc_method = EnumProperty(
        name="Area Calculation Method",
        description="How to calculate target area",
        items=[
            ('MESH', "Mesh", "Calculate area by whole faces in mesh"),
            ('UV ISLAND', "UV Island", "Calculate area each UV islands"),
            ('FACE', "Face", "Calculate area each face")
        ],
        default='MESH'
    )
    only_selected = BoolProperty(
        name="Only Selected",
        description="Apply to only selected faces",
        default=True,
    )

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context_for_apply(context)

    @staticmethod
    def setup_argument(ops, scene):
        ops.tgt_density = scene.muv_world_scale_uv_tgt_density
        ops.tgt_texture_size = scene.muv_world_scale_uv_tgt_texture_size
        ops.origin = scene.muv_world_scale_uv_origin
        ops.show_dialog = False
        ops.tgt_area_calc_method = \
            scene.muv_world_scale_uv_tgt_area_calc_method
        ops.only_selected = scene.muv_world_scale_uv_apply_only_selected

    def __apply_manual(self, context):
        objs = common.get_uv_editable_objects(context)

        for obj in objs:
            bm = bmesh.from_edit_mesh(obj.data)
            if common.check_version(2, 73, 0) >= 0:
                bm.verts.ensure_lookup_table()
                bm.edges.ensure_lookup_table()
                bm.faces.ensure_lookup_table()

            if not bm.loops.layers.uv:
                self.report({'WARNING'},
                            "Object {} must have more than one UV map"
                            .format(obj.name))
                return {'CANCELLED'}
            uv_layer = bm.loops.layers.uv.verify()
            tex_layer = common.find_texture_layer(bm)
            faces_list = common.get_faces_list(
                bm, self.tgt_area_calc_method, self.only_selected)

            tex_size = self.tgt_texture_size

            factors = []
            for faces in faces_list:
                uv_area, _, density = _measure_wsuv_info_from_faces(
                    obj, bm, faces, uv_layer, tex_layer,
                    tex_selection_method='USER_SPECIFIED', tex_size=tex_size)

                if not uv_area:
                    self.report({'WARNING'},
                                "Object {} must have more than one UV map"
                                .format(obj.name))
                    return {'CANCELLED'}

                tgt_density = self.tgt_density
                factor = tgt_density / density

                _apply(faces, uv_layer, self.origin, factor)
                factors.append(factor)

            bmesh.update_edit_mesh(obj.data)
            self.report({'INFO'},
                        "Scaling factor of object {}: {}"
                        .format(obj.name, factors))

        return {'FINISHED'}

    def draw(self, _):
        layout = self.layout

        layout.label(text="Target:")
        layout.prop(self, "only_selected")
        layout.prop(self, "tgt_texture_size")
        layout.prop(self, "tgt_density")
        layout.prop(self, "origin")
        layout.prop(self, "tgt_area_calc_method")

        layout.separator()

    def invoke(self, context, _):
        if self.show_dialog:
            wm = context.window_manager
            return wm.invoke_props_dialog(self)

        return self.execute(context)

    def execute(self, context):
        return self.__apply_manual(context)


@BlClassRegistry()
@compat.make_annotations
class MUV_OT_WorldScaleUV_ApplyScalingDensity(bpy.types.Operator):
    """
    Operation class: Apply scaled UV (Scaling Density)
    """

    bl_idname = "uv.muv_world_scale_uv_apply_scaling_density"
    bl_label = "Apply World Scale UV (Scaling Density)"
    bl_description = "Apply scaled UV with scaling density"
    bl_options = {'REGISTER', 'UNDO'}

    tgt_scaling_factor = FloatProperty(
        name="Scaling Factor",
        default=1.0,
        max=1000.0,
        min=0.00001
    )
    origin = EnumProperty(
        name="Origin",
        description="Aspect Origin",
        items=[
            ('CENTER', "Center", "Center"),
            ('LEFT_TOP', "Left Top", "Left Bottom"),
            ('LEFT_CENTER', "Left Center", "Left Center"),
            ('LEFT_BOTTOM', "Left Bottom", "Left Bottom"),
            ('CENTER_TOP', "Center Top", "Center Top"),
            ('CENTER_BOTTOM', "Center Bottom", "Center Bottom"),
            ('RIGHT_TOP', "Right Top", "Right Top"),
            ('RIGHT_CENTER', "Right Center", "Right Center"),
            ('RIGHT_BOTTOM', "Right Bottom", "Right Bottom")

        ],
        default='CENTER'
    )
    src_density = FloatProperty(
        name="Density",
        description="Source Texel Density",
        default=0.0,
        min=0.0,
        options={'HIDDEN'}
    )
    same_density = BoolProperty(
        name="Same Density",
        description="Apply same density",
        default=False,
        options={'HIDDEN'}
    )
    show_dialog = BoolProperty(
        name="Show Diaglog Menu",
        description="Show dialog menu if true",
        default=True,
        options={'HIDDEN', 'SKIP_SAVE'}
    )
    tgt_texture = EnumProperty(
        name="Texture",
        description="Texture to be applied",
        items=_get_target_textures
    )
    tgt_area_calc_method = EnumProperty(
        name="Area Calculation Method",
        description="How to calculate target area",
        items=[
            ('MESH', "Mesh", "Calculate area by whole faces in mesh"),
            ('UV ISLAND', "UV Island", "Calculate area each UV islands"),
            ('FACE', "Face", "Calculate area each face")
        ],
        default='MESH'
    )
    only_selected = BoolProperty(
        name="Only Selected",
        description="Apply to only selected faces",
        default=True,
    )

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context_for_apply(context)

    @staticmethod
    def setup_argument(ops, scene):
        ops.tgt_scaling_factor = \
            scene.muv_world_scale_uv_tgt_scaling_factor
        ops.origin = scene.muv_world_scale_uv_origin
        ops.src_density = scene.muv_world_scale_uv_src_density
        ops.same_density = False
        ops.show_dialog = False
        try:
            ops.tgt_texture = scene.muv_world_scale_uv_apply_tgt_texture
        except TypeError:
            # Workaround for the error raised when the items of EnumProperty
            # are deleted.
            ops.tgt_texture = "[Average]"
        ops.tgt_area_calc_method = \
            scene.muv_world_scale_uv_tgt_area_calc_method
        ops.only_selected = scene.muv_world_scale_uv_apply_only_selected

    def __apply_scaling_density(self, context):
        objs = common.get_uv_editable_objects(context)

        for obj in objs:
            bm = bmesh.from_edit_mesh(obj.data)
            if common.check_version(2, 73, 0) >= 0:
                bm.verts.ensure_lookup_table()
                bm.edges.ensure_lookup_table()
                bm.faces.ensure_lookup_table()

            if not bm.loops.layers.uv:
                self.report({'WARNING'},
                            "Object {} must have more than one UV map"
                            .format(obj.name))
                return {'CANCELLED'}
            uv_layer = bm.loops.layers.uv.verify()
            tex_layer = common.find_texture_layer(bm)
            faces_list = common.get_faces_list(
                bm, self.tgt_area_calc_method, self.only_selected)

            factors = []
            for faces in faces_list:
                if self.tgt_texture == "[Average]":
                    uv_area, _, density = _measure_wsuv_info_from_faces(
                        obj, bm, faces, uv_layer, tex_layer,
                        tex_selection_method='AVERAGE')
                elif self.tgt_texture == "[Max]":
                    uv_area, _, density = _measure_wsuv_info_from_faces(
                        obj, bm, faces, uv_layer, tex_layer,
                        tex_selection_method='MAX')
                elif self.tgt_texture == "[Min]":
                    uv_area, _, density = _measure_wsuv_info_from_faces(
                        obj, bm, faces, uv_layer, tex_layer,
                        tex_selection_method='MIN')
                else:
                    tgt_texture = bpy.data.images[self.tgt_texture]
                    uv_area, _, density = _measure_wsuv_info_from_faces(
                        obj, bm, faces, uv_layer, tex_layer,
                        tex_selection_method='USER_SPECIFIED',
                        tex_size=tgt_texture.size)

                if not uv_area:
                    self.report({'WARNING'},
                                "Object {} must have more than one UV map and "
                                "texture".format(obj.name))
                    return {'CANCELLED'}

                tgt_density = self.src_density * self.tgt_scaling_factor
                factor = tgt_density / density

                _apply(faces, uv_layer, self.origin, factor)
                factors.append(factor)

            bmesh.update_edit_mesh(obj.data)
            self.report({'INFO'},
                        "Scaling factor of object {}: {}"
                        .format(obj.name, factors))

        return {'FINISHED'}

    def draw(self, _):
        layout = self.layout

        layout.label(text="Source:")
        col = layout.column()
        col.prop(self, "src_density")
        col.enabled = False

        layout.separator()

        layout.label(text="Target:")
        if not self.same_density:
            layout.prop(self, "tgt_scaling_factor")
        layout.prop(self, "only_selected")
        layout.prop(self, "tgt_texture")
        layout.prop(self, "origin")
        layout.prop(self, "tgt_area_calc_method")

        layout.separator()

    def invoke(self, context, _):
        sc = context.scene

        if self.show_dialog:
            wm = context.window_manager

            if self.same_density:
                self.tgt_scaling_factor = 1.0
            else:
                self.tgt_scaling_factor = \
                    sc.muv_world_scale_uv_tgt_scaling_factor
                self.src_density = sc.muv_world_scale_uv_src_density

            return wm.invoke_props_dialog(self)

        return self.execute(context)

    def execute(self, context):
        if self.same_density:
            self.tgt_scaling_factor = 1.0

        return self.__apply_scaling_density(context)


@BlClassRegistry()
@compat.make_annotations
class MUV_OT_WorldScaleUV_ApplyProportionalToMesh(bpy.types.Operator):
    """
    Operation class: Apply scaled UV (Proportional to mesh)
    """

    bl_idname = "uv.muv_world_scale_uv_apply_proportional_to_mesh"
    bl_label = "Apply World Scale UV (Proportional to mesh)"
    bl_description = "Apply scaled UV proportionaled to mesh"
    bl_options = {'REGISTER', 'UNDO'}

    origin = EnumProperty(
        name="Origin",
        description="Aspect Origin",
        items=[
            ('CENTER', "Center", "Center"),
            ('LEFT_TOP', "Left Top", "Left Bottom"),
            ('LEFT_CENTER', "Left Center", "Left Center"),
            ('LEFT_BOTTOM', "Left Bottom", "Left Bottom"),
            ('CENTER_TOP', "Center Top", "Center Top"),
            ('CENTER_BOTTOM', "Center Bottom", "Center Bottom"),
            ('RIGHT_TOP', "Right Top", "Right Top"),
            ('RIGHT_CENTER', "Right Center", "Right Center"),
            ('RIGHT_BOTTOM', "Right Bottom", "Right Bottom")

        ],
        default='CENTER'
    )
    src_density = FloatProperty(
        name="Source Density",
        description="Source Texel Density",
        default=0.0,
        min=0.0,
        options={'HIDDEN'}
    )
    src_uv_area = FloatProperty(
        name="Source UV Area",
        description="Source UV Area",
        default=0.0,
        min=0.0,
        options={'HIDDEN'}
    )
    src_mesh_area = FloatProperty(
        name="Source Mesh Area",
        description="Source Mesh Area",
        default=0.0,
        min=0.0,
        options={'HIDDEN'}
    )
    show_dialog = BoolProperty(
        name="Show Diaglog Menu",
        description="Show dialog menu if true",
        default=True,
        options={'HIDDEN', 'SKIP_SAVE'}
    )
    tgt_texture = EnumProperty(
        name="Texture",
        description="Texture to be applied",
        items=_get_target_textures
    )
    tgt_area_calc_method = EnumProperty(
        name="Area Calculation Method",
        description="How to calculate target area",
        items=[
            ('MESH', "Mesh", "Calculate area by whole faces in mesh"),
            ('UV ISLAND', "UV Island", "Calculate area each UV islands"),
            ('FACE', "Face", "Calculate area each face")
        ],
        default='MESH'
    )
    only_selected = BoolProperty(
        name="Only Selected",
        description="Apply to only selected faces",
        default=True,
    )

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context_for_apply(context)

    @staticmethod
    def setup_argument(ops, scene):
        ops.origin = scene.muv_world_scale_uv_origin
        ops.src_density = scene.muv_world_scale_uv_src_density
        ops.src_uv_area = scene.muv_world_scale_uv_src_uv_area
        ops.src_mesh_area = scene.muv_world_scale_uv_src_mesh_area
        ops.show_dialog = False
        try:
            ops.tgt_texture = scene.muv_world_scale_uv_apply_tgt_texture
        except TypeError:
            # Workaround for the error raised when the items of EnumProperty
            # are deleted.
            ops.tgt_texture = "[Average]"
        ops.tgt_area_calc_method = \
            scene.muv_world_scale_uv_tgt_area_calc_method
        ops.only_selected = scene.muv_world_scale_uv_apply_only_selected

    def __apply_proportional_to_mesh(self, context):
        objs = common.get_uv_editable_objects(context)

        for obj in objs:
            bm = bmesh.from_edit_mesh(obj.data)
            if common.check_version(2, 73, 0) >= 0:
                bm.verts.ensure_lookup_table()
                bm.edges.ensure_lookup_table()
                bm.faces.ensure_lookup_table()

            if not bm.loops.layers.uv:
                self.report({'WARNING'},
                            "Object {} must have more than one UV map"
                            .format(obj.name))
                return {'CANCELLED'}
            uv_layer = bm.loops.layers.uv.verify()
            tex_layer = common.find_texture_layer(bm)
            faces_list = common.get_faces_list(
                bm, self.tgt_area_calc_method, self.only_selected)

            factors = []
            for faces in faces_list:
                if self.tgt_texture == "[Average]":
                    uv_area, mesh_area, density = \
                        _measure_wsuv_info_from_faces(
                            obj, bm, faces, uv_layer, tex_layer,
                            tex_selection_method='AVERAGE')
                elif self.tgt_texture == "[Max]":
                    uv_area, mesh_area, density = \
                        _measure_wsuv_info_from_faces(
                            obj, bm, faces, uv_layer, tex_layer,
                            tex_selection_method='MAX')
                elif self.tgt_texture == "[Min]":
                    uv_area, mesh_area, density = \
                        _measure_wsuv_info_from_faces(
                            obj, bm, faces, uv_layer, tex_layer,
                            tex_selection_method='MIN')
                else:
                    tgt_texture = bpy.data.images[self.tgt_texture]
                    uv_area, mesh_area, density = \
                        _measure_wsuv_info_from_faces(
                            obj, bm, faces, uv_layer, tex_layer,
                            tex_selection_method='USER_SPECIFIED',
                            tex_size=tgt_texture.size)
                if not uv_area:
                    self.report({'WARNING'},
                                "Object {} must have more than one UV map and "
                                "texture".format(obj.name))
                    return {'CANCELLED'}

                tgt_density = self.src_density * sqrt(mesh_area) / sqrt(
                    self.src_mesh_area)
                factor = tgt_density / density

                _apply(faces, uv_layer, self.origin, factor)
                factors.append(factor)

            bmesh.update_edit_mesh(obj.data)
            self.report({'INFO'},
                        "Scaling factor of object {}: {}"
                        .format(obj.name, factors))

        return {'FINISHED'}

    def draw(self, _):
        layout = self.layout

        layout.label(text="Source:")
        col = layout.column(align=True)
        col.prop(self, "src_density")
        col.prop(self, "src_uv_area")
        col.prop(self, "src_mesh_area")
        col.enabled = False

        layout.separator()

        layout.label(text="Target:")
        layout.prop(self, "only_selected")
        layout.prop(self, "origin")
        layout.prop(self, "tgt_area_calc_method")
        layout.prop(self, "tgt_texture")

        layout.separator()

    def invoke(self, context, _):
        if self.show_dialog:
            wm = context.window_manager
            sc = context.scene

            self.src_density = sc.muv_world_scale_uv_src_density
            self.src_mesh_area = sc.muv_world_scale_uv_src_mesh_area

            return wm.invoke_props_dialog(self)

        return self.execute(context)

    def execute(self, context):
        return self.__apply_proportional_to_mesh(context)