|
|
@@ -1,178 +0,0 @@
|
|
1
|
|
-# ##### BEGIN GPL LICENSE BLOCK #####
|
|
2
|
|
-#
|
|
3
|
|
-# This program is free software; you can redistribute it and/or
|
|
4
|
|
-# modify it under the terms of the GNU General Public License
|
|
5
|
|
-# as published by the Free Software Foundation; either version 2
|
|
6
|
|
-# of the License, or (at your option) any later version.
|
|
7
|
|
-#
|
|
8
|
|
-# This program is distributed in the hope that it will be useful,
|
|
9
|
|
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
|
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
|
-# GNU General Public License for more details.
|
|
12
|
|
-#
|
|
13
|
|
-# You should have received a copy of the GNU General Public License
|
|
14
|
|
-# along with this program; if not, write to the Free Software Foundation,
|
|
15
|
|
-# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
16
|
|
-#
|
|
17
|
|
-# ##### END GPL LICENSE BLOCK #####
|
|
18
|
|
-
|
|
19
|
|
-# --------------------------------- UV to MESH ------------------------------- #
|
|
20
|
|
-# -------------------------------- version 0.1.1 ----------------------------- #
|
|
21
|
|
-# #
|
|
22
|
|
-# Create a new Mesh based on active UV #
|
|
23
|
|
-# #
|
|
24
|
|
-# (c) Alessandro Zomparelli #
|
|
25
|
|
-# (2017) #
|
|
26
|
|
-# #
|
|
27
|
|
-# http://www.co-de-it.com/ #
|
|
28
|
|
-# #
|
|
29
|
|
-# ############################################################################ #
|
|
30
|
|
-
|
|
31
|
|
-import bpy
|
|
32
|
|
-import math
|
|
33
|
|
-from bpy.types import Operator
|
|
34
|
|
-from bpy.props import BoolProperty
|
|
35
|
|
-from mathutils import Vector
|
|
36
|
|
-from .utils import *
|
|
37
|
|
-
|
|
38
|
|
-
|
|
39
|
|
-class uv_to_mesh(Operator):
|
|
40
|
|
- bl_idname = "object.uv_to_mesh"
|
|
41
|
|
- bl_label = "UV to Mesh"
|
|
42
|
|
- bl_description = ("Create a new Mesh based on active UV")
|
|
43
|
|
- bl_options = {'REGISTER', 'UNDO'}
|
|
44
|
|
-
|
|
45
|
|
- apply_modifiers : BoolProperty(
|
|
46
|
|
- name="Apply Modifiers",
|
|
47
|
|
- default=True,
|
|
48
|
|
- description="Apply object's modifiers"
|
|
49
|
|
- )
|
|
50
|
|
- vertex_groups : BoolProperty(
|
|
51
|
|
- name="Keep Vertex Groups",
|
|
52
|
|
- default=True,
|
|
53
|
|
- description="Transfer all the Vertex Groups"
|
|
54
|
|
- )
|
|
55
|
|
- materials : BoolProperty(
|
|
56
|
|
- name="Keep Materials",
|
|
57
|
|
- default=True,
|
|
58
|
|
- description="Transfer all the Materials"
|
|
59
|
|
- )
|
|
60
|
|
- auto_scale : BoolProperty(
|
|
61
|
|
- name="Resize",
|
|
62
|
|
- default=True,
|
|
63
|
|
- description="Scale the new object in order to preserve the average surface area"
|
|
64
|
|
- )
|
|
65
|
|
-
|
|
66
|
|
- def execute(self, context):
|
|
67
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
68
|
|
- ob0 = bpy.context.object
|
|
69
|
|
- for o in bpy.context.view_layer.objects: o.select_set(False)
|
|
70
|
|
- ob0.select_set(True)
|
|
71
|
|
-
|
|
72
|
|
- #if self.apply_modifiers:
|
|
73
|
|
- # bpy.ops.object.duplicate_move()
|
|
74
|
|
- # bpy.ops.object.convert(target='MESH')
|
|
75
|
|
-
|
|
76
|
|
-# me0 = ob0.to_mesh(bpy.context.depsgraph, apply_modifiers=self.apply_modifiers)
|
|
77
|
|
- #if self.apply_modifiers: me0 = simple_to_mesh(ob0)
|
|
78
|
|
- #else: me0 = ob0.data.copy()
|
|
79
|
|
- name0 = ob0.name
|
|
80
|
|
- ob0 = convert_object_to_mesh(ob0, apply_modifiers=self.apply_modifiers, preserve_status=False)
|
|
81
|
|
- me0 = ob0.data
|
|
82
|
|
- area = 0
|
|
83
|
|
-
|
|
84
|
|
- verts = []
|
|
85
|
|
- faces = []
|
|
86
|
|
- face_materials = []
|
|
87
|
|
- for face in me0.polygons:
|
|
88
|
|
- area += face.area
|
|
89
|
|
- uv_face = []
|
|
90
|
|
- store = False
|
|
91
|
|
- try:
|
|
92
|
|
- for loop in face.loop_indices:
|
|
93
|
|
- uv = me0.uv_layers.active.data[loop].uv
|
|
94
|
|
- if uv.x != 0 and uv.y != 0:
|
|
95
|
|
- store = True
|
|
96
|
|
- new_vert = Vector((uv.x, uv.y, 0))
|
|
97
|
|
- verts.append(new_vert)
|
|
98
|
|
- uv_face.append(loop)
|
|
99
|
|
- if store:
|
|
100
|
|
- faces.append(uv_face)
|
|
101
|
|
- face_materials.append(face.material_index)
|
|
102
|
|
- except:
|
|
103
|
|
- self.report({'ERROR'}, "Missing UV Map")
|
|
104
|
|
-
|
|
105
|
|
- return {'CANCELLED'}
|
|
106
|
|
-
|
|
107
|
|
- name = name0 + '_UV'
|
|
108
|
|
- # Create mesh and object
|
|
109
|
|
- me = bpy.data.meshes.new(name + 'Mesh')
|
|
110
|
|
- ob = bpy.data.objects.new(name, me)
|
|
111
|
|
-
|
|
112
|
|
- # Link object to scene and make active
|
|
113
|
|
- scn = bpy.context.scene
|
|
114
|
|
- bpy.context.collection.objects.link(ob)
|
|
115
|
|
- bpy.context.view_layer.objects.active = ob
|
|
116
|
|
- ob.select_set(True)
|
|
117
|
|
-
|
|
118
|
|
- # Create mesh from given verts, faces.
|
|
119
|
|
- me.from_pydata(verts, [], faces)
|
|
120
|
|
- # Update mesh with new data
|
|
121
|
|
- me.update()
|
|
122
|
|
- if self.auto_scale:
|
|
123
|
|
- new_area = 0
|
|
124
|
|
- for p in me.polygons:
|
|
125
|
|
- new_area += p.area
|
|
126
|
|
- if new_area == 0:
|
|
127
|
|
- self.report({'ERROR'}, "Impossible to generate mesh from UV")
|
|
128
|
|
- bpy.data.objects.remove(ob0)
|
|
129
|
|
-
|
|
130
|
|
- return {'CANCELLED'}
|
|
131
|
|
-
|
|
132
|
|
- # VERTEX GROUPS
|
|
133
|
|
- if self.vertex_groups:
|
|
134
|
|
- for group in ob0.vertex_groups:
|
|
135
|
|
- index = group.index
|
|
136
|
|
- ob.vertex_groups.new(name=group.name)
|
|
137
|
|
- for p in me0.polygons:
|
|
138
|
|
- for vert, loop in zip(p.vertices, p.loop_indices):
|
|
139
|
|
- try:
|
|
140
|
|
- ob.vertex_groups[index].add([loop], group.weight(vert), 'REPLACE')
|
|
141
|
|
- except:
|
|
142
|
|
- pass
|
|
143
|
|
-
|
|
144
|
|
- ob0.select_set(False)
|
|
145
|
|
- if self.auto_scale:
|
|
146
|
|
- scaleFactor = math.pow(area / new_area, 1 / 2)
|
|
147
|
|
- ob.scale = Vector((scaleFactor, scaleFactor, scaleFactor))
|
|
148
|
|
-
|
|
149
|
|
- bpy.ops.object.mode_set(mode='EDIT', toggle=False)
|
|
150
|
|
- bpy.ops.mesh.remove_doubles(threshold=1e-06)
|
|
151
|
|
- bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
|
|
152
|
|
- bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
|
|
153
|
|
-
|
|
154
|
|
- # MATERIALS
|
|
155
|
|
- if self.materials:
|
|
156
|
|
- try:
|
|
157
|
|
- # assign old material
|
|
158
|
|
- uv_materials = [slot.material for slot in ob0.material_slots]
|
|
159
|
|
- for i in range(len(uv_materials)):
|
|
160
|
|
- bpy.ops.object.material_slot_add()
|
|
161
|
|
- bpy.context.object.material_slots[i].material = uv_materials[i]
|
|
162
|
|
- for i in range(len(ob.data.polygons)):
|
|
163
|
|
- ob.data.polygons[i].material_index = face_materials[i]
|
|
164
|
|
- except:
|
|
165
|
|
- pass
|
|
166
|
|
- '''
|
|
167
|
|
- if self.apply_modifiers:
|
|
168
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
169
|
|
- ob.select_set(False)
|
|
170
|
|
- ob0.select_set(True)
|
|
171
|
|
- bpy.ops.object.delete(use_global=False)
|
|
172
|
|
- ob.select_set(True)
|
|
173
|
|
- bpy.context.view_layer.objects.active = ob
|
|
174
|
|
- '''
|
|
175
|
|
-
|
|
176
|
|
- bpy.data.objects.remove(ob0)
|
|
177
|
|
- bpy.data.meshes.remove(me0)
|
|
178
|
|
- return {'FINISHED'}
|