1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # --------------------------------- UV to MESH ------------------------------- #
4 # -------------------------------- version 0.1.1 ----------------------------- #
6 # Create a new Mesh based on active UV #
8 # (c) Alessandro Zomparelli #
11 # http://www.co-de-it.com/ #
13 # ############################################################################ #
17 from bpy
.types
import Operator
18 from bpy
.props
import BoolProperty
19 from mathutils
import Vector
23 class uv_to_mesh(Operator
):
24 bl_idname
= "object.uv_to_mesh"
25 bl_label
= "UV to Mesh"
26 bl_description
= ("Create a new Mesh based on active UV")
27 bl_options
= {'REGISTER', 'UNDO'}
29 apply_modifiers
: BoolProperty(
30 name
="Apply Modifiers",
32 description
="Apply object's modifiers"
34 vertex_groups
: BoolProperty(
35 name
="Keep Vertex Groups",
37 description
="Transfer all the Vertex Groups"
39 materials
: BoolProperty(
40 name
="Keep Materials",
42 description
="Transfer all the Materials"
44 auto_scale
: BoolProperty(
47 description
="Scale the new object in order to preserve the average surface area"
50 def execute(self
, context
):
51 if context
.mode
== 'EDIT_MESH': on_selection
= True
52 else: on_selection
= False
54 bpy
.ops
.object.mode_set(mode
='OBJECT')
56 for o
in bpy
.context
.view_layer
.objects
: o
.select_set(False)
60 ob0
= convert_object_to_mesh(ob0
, apply_modifiers
=self
.apply_modifiers
, preserve_status
=False)
66 if on_selection
: polygons
= [f
for f
in me0
.polygons
if f
.select
]
67 else: polygons
= me0
.polygons
74 if len(me0
.uv_layers
) > 0:
76 for loop
in face
.loop_indices
:
77 uv
= me0
.uv_layers
.active
.data
[loop
].uv
78 if uv
.x
!= 0 and uv
.y
!= 0:
80 new_vert
= bm
.verts
.new((uv
.x
, uv
.y
, 0))
81 verts
.append(new_vert
)
83 new_face
= bm
.faces
.new(verts
)
84 new_face
.material_index
= face
.material_index
86 self
.report({'ERROR'}, "Missing UV Map")
90 # Create mesh and object
91 me
= bpy
.data
.meshes
.new(name
+ 'Mesh')
92 ob
= bpy
.data
.objects
.new(name
, me
)
94 # Link object to scene and make active
95 scn
= bpy
.context
.scene
96 bpy
.context
.collection
.objects
.link(ob
)
97 bpy
.context
.view_layer
.objects
.active
= ob
100 # Create mesh from given verts, faces.
102 # Update mesh with new data
107 for p
in me
.polygons
:
110 self
.report({'ERROR'}, "Impossible to generate mesh from UV")
111 bpy
.data
.objects
.remove(ob0
)
115 if self
.vertex_groups
:
116 for group
in ob0
.vertex_groups
:
118 ob
.vertex_groups
.new(name
=group
.name
)
120 for vert
, loop
in zip(p
.vertices
, p
.loop_indices
):
122 ob
.vertex_groups
[index
].add([loop
], group
.weight(vert
), 'REPLACE')
126 ob0
.select_set(False)
128 scaleFactor
= math
.pow(area
/ new_area
, 1 / 2)
129 ob
.scale
= Vector((scaleFactor
, scaleFactor
, scaleFactor
))
131 bpy
.ops
.object.mode_set(mode
='EDIT', toggle
=False)
132 bpy
.ops
.mesh
.remove_doubles(threshold
=1e-06)
133 bpy
.ops
.object.mode_set(mode
='OBJECT', toggle
=False)
134 bpy
.ops
.object.transform_apply(location
=False, rotation
=False, scale
=True)
138 if len(ob0
.material_slots
) > 0:
139 # assign old material
140 uv_materials
= [slot
.material
for slot
in ob0
.material_slots
]
141 for i
in range(len(uv_materials
)):
142 bpy
.ops
.object.material_slot_add()
143 bpy
.context
.object.material_slots
[i
].material
= uv_materials
[i
]
145 bpy
.data
.objects
.remove(ob0
)
146 bpy
.data
.meshes
.remove(me0
)