1 # SPDX-FileCopyrightText: 2017-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # --------------------------------- UV to MESH ------------------------------- #
6 # -------------------------------- version 0.1.1 ----------------------------- #
8 # Create a new Mesh based on active UV #
10 # (c) Alessandro Zomparelli #
13 # http://www.co-de-it.com/ #
15 # ############################################################################ #
19 from bpy
.types
import Operator
20 from bpy
.props
import BoolProperty
21 from mathutils
import Vector
25 class uv_to_mesh(Operator
):
26 bl_idname
= "object.uv_to_mesh"
27 bl_label
= "UV to Mesh"
28 bl_description
= ("Create a new Mesh based on active UV")
29 bl_options
= {'REGISTER', 'UNDO'}
31 apply_modifiers
: BoolProperty(
32 name
="Apply Modifiers",
34 description
="Apply object's modifiers"
36 vertex_groups
: BoolProperty(
37 name
="Keep Vertex Groups",
39 description
="Transfer all the Vertex Groups"
41 materials
: BoolProperty(
42 name
="Keep Materials",
44 description
="Transfer all the Materials"
46 auto_scale
: BoolProperty(
49 description
="Scale the new object in order to preserve the average surface area"
52 def execute(self
, context
):
53 if context
.mode
== 'EDIT_MESH': on_selection
= True
54 else: on_selection
= False
56 bpy
.ops
.object.mode_set(mode
='OBJECT')
58 for o
in bpy
.context
.view_layer
.objects
: o
.select_set(False)
62 ob0
= convert_object_to_mesh(ob0
, apply_modifiers
=self
.apply_modifiers
, preserve_status
=False)
68 if on_selection
: polygons
= [f
for f
in me0
.polygons
if f
.select
]
69 else: polygons
= me0
.polygons
76 if len(me0
.uv_layers
) > 0:
78 for loop
in face
.loop_indices
:
79 uv
= me0
.uv_layers
.active
.data
[loop
].uv
80 if uv
.x
!= 0 and uv
.y
!= 0:
82 new_vert
= bm
.verts
.new((uv
.x
, uv
.y
, 0))
83 verts
.append(new_vert
)
85 new_face
= bm
.faces
.new(verts
)
86 new_face
.material_index
= face
.material_index
88 self
.report({'ERROR'}, "Missing UV Map")
92 # Create mesh and object
93 me
= bpy
.data
.meshes
.new(name
+ 'Mesh')
94 ob
= bpy
.data
.objects
.new(name
, me
)
96 # Link object to scene and make active
97 scn
= bpy
.context
.scene
98 bpy
.context
.collection
.objects
.link(ob
)
99 bpy
.context
.view_layer
.objects
.active
= ob
102 # Create mesh from given verts, faces.
104 # Update mesh with new data
109 for p
in me
.polygons
:
112 self
.report({'ERROR'}, "Impossible to generate mesh from UV")
113 bpy
.data
.objects
.remove(ob0
)
117 if self
.vertex_groups
:
118 for group
in ob0
.vertex_groups
:
120 ob
.vertex_groups
.new(name
=group
.name
)
122 for vert
, loop
in zip(p
.vertices
, p
.loop_indices
):
124 ob
.vertex_groups
[index
].add([loop
], group
.weight(vert
), 'REPLACE')
128 ob0
.select_set(False)
130 scaleFactor
= math
.pow(area
/ new_area
, 1 / 2)
131 ob
.scale
= Vector((scaleFactor
, scaleFactor
, scaleFactor
))
133 bpy
.ops
.object.mode_set(mode
='EDIT', toggle
=False)
134 bpy
.ops
.mesh
.remove_doubles(threshold
=1e-06)
135 bpy
.ops
.object.mode_set(mode
='OBJECT', toggle
=False)
136 bpy
.ops
.object.transform_apply(location
=False, rotation
=False, scale
=True)
140 if len(ob0
.material_slots
) > 0:
141 # assign old material
142 uv_materials
= [slot
.material
for slot
in ob0
.material_slots
]
143 for i
in range(len(uv_materials
)):
144 bpy
.ops
.object.material_slot_add()
145 bpy
.context
.object.material_slots
[i
].material
= uv_materials
[i
]
147 bpy
.data
.objects
.remove(ob0
)
148 bpy
.data
.meshes
.remove(me0
)