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
|
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/
# or write to the Free Software Foundation, Inc., 51 Franklin Street,
# Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Copy2 Vertices, Edges or Faces",
"author": "Eleanor Howick (elfnor.com)",
"version": (0, 1, 1),
"blender": (2, 71, 0),
"location": "3D View > Object > Copy 2",
"description": "Copy one object to the selected vertices, edges or faces of another object",
"warning": "",
"category": "Object"
}
import bpy
from bpy.types import Operator
from bpy.props import (
BoolProperty,
EnumProperty,
FloatProperty,
)
from mathutils import (
Vector,
Matrix,
)
class Copy2(Operator):
bl_idname = "mesh.copy2"
bl_label = "Copy 2"
bl_description = ("Copy Vertices, Edges or Faces to the Selected object\n"
"Needs an existing Active Mesh Object")
bl_options = {"REGISTER", "UNDO"}
obj_list = None
def obj_list_cb(self, context):
return Copy2.obj_list
def sec_axes_list_cb(self, context):
if self.priaxes == 'X':
sec_list = [('Y', "Y", "Secondary axis Y"),
('Z', "Z", "Secondary axis Z")]
if self.priaxes == 'Y':
sec_list = [('X', "X", "Secondary axis X"),
('Z', "Z", "Secondary axis Z")]
if self.priaxes == 'Z':
sec_list = [('X', "X", "Secondary axis X"),
('Y', "Y", "Secondary axis Y")]
return sec_list
copytype: EnumProperty(
items=(('V', "Vertex",
"Paste the Copied Geometry to Vertices of the Active Object", 'VERTEXSEL', 0),
('E', "Edge",
"Paste the Copied Geometry to Edges of the Active Object", 'EDGESEL', 1),
('F', "Face",
"Paste the Copied Geometry to Faces of the Active Object", 'FACESEL', 2)),
)
copyfromobject: EnumProperty(
name="Copy from",
description="Copy an Object from the list",
items=obj_list_cb
)
priaxes: EnumProperty(
description="Primary axes used for Copied Object orientation",
items=(('X', "X", "Along X"),
('Y', "Y", "Along Y"),
('Z', "Z", "Along Z")),
)
edgescale: BoolProperty(
name="Scale to fill edge",
default=False
)
secaxes: EnumProperty(
name="Secondary Axis",
description="Secondary axis used for Copied Object orientation",
items=sec_axes_list_cb
)
scale: FloatProperty(
name="Scale",
default=1.0,
min=0.0,
)
@classmethod
def poll(cls, context):
obj = context.active_object
return obj and obj.type == "MESH"
def draw(self, context):
layout = self.layout
layout.prop(self, "copyfromobject")
layout.label("to:")
layout.prop(self, "copytype", expand=True)
layout.label("Primary axis:")
layout.prop(self, "priaxes", expand=True)
layout.label("Secondary axis:")
layout.prop(self, "secaxes", expand=True)
if self.copytype == "E":
layout.prop(self, "edgescale")
if self.edgescale:
layout.prop(self, "scale")
return
def execute(self, context):
copytoobject = context.active_object.name
axes = self.priaxes + self.secaxes
# check if there is a problem with the strings related to some chars
copy_to_object = bpy.data.objects[copytoobject] if \
copytoobject in bpy.data.objects else None
copy_from_object = bpy.data.objects[self.copyfromobject] if \
self.copyfromobject in bpy.data.objects else None
if copy_to_object is None or copy_from_object is None:
self.report({"WARNING"},
"There was a problem with retrieving Object data. Operation Cancelled")
return {"CANCELLED"}
try:
copy_to_from(
context.scene,
copy_to_object,
copy_from_object,
self.copytype,
axes,
self.edgescale,
self.scale
)
except Exception as e:
self.report({"WARNING"},
"Copy2 could not be completed (Check the Console for more info)")
print("\n[Add Advanced Objects]\nOperator: mesh.copy2\n{}\n".format(e))
return {"CANCELLED"}
return {"FINISHED"}
def invoke(self, context, event):
Copy2.obj_list = [(obj.name, obj.name, obj.name) for obj in bpy.data.objects]
return {"FINISHED"}
def copy_to_from(scene, to_obj, from_obj, copymode, axes, edgescale, scale):
if copymode == 'V':
vertex_copy(scene, to_obj, from_obj, axes)
if copymode == 'E':
# don't pass edgescalling to object types that cannot be scaled
if from_obj.type in ["CAMERA", "LIGHT", "EMPTY", "ARMATURE", "SPEAKER", "META"]:
edgescale = False
edge_copy(scene, to_obj, from_obj, axes, edgescale, scale)
if copymode == 'F':
face_copy(scene, to_obj, from_obj, axes)
axes_dict = {'XY': (1, 2, 0),
'XZ': (2, 1, 0),
'YX': (0, 2, 1),
'YZ': (2, 0, 1),
'ZX': (0, 1, 2),
'ZY': (1, 0, 2)}
def copyto(scene, source_obj, pos, xdir, zdir, axes, scale=None):
"""
copy the source_obj to pos, so its primary axis points in zdir and its
secondary axis points in xdir
"""
copy_obj = source_obj.copy()
scene.objects.link(copy_obj)
xdir = xdir.normalized()
zdir = zdir.normalized()
# rotation first
z_axis = zdir
x_axis = xdir
y_axis = z_axis.cross(x_axis)
# use axes_dict to assign the axis as chosen in panel
A, B, C = axes_dict[axes]
rot_mat = Matrix()
rot_mat[A].xyz = x_axis
rot_mat[B].xyz = y_axis
rot_mat[C].xyz = z_axis
rot_mat.transpose()
# rotate object
copy_obj.matrix_world = rot_mat
# move object into position
copy_obj.location = pos
# scale object
if scale is not None:
copy_obj.scale = scale
return copy_obj
def vertex_copy(scene, obj, source_obj, axes):
# vertex select mode
sel_verts = []
copy_list = []
for v in obj.data.vertices:
if v.select is True:
sel_verts.append(v)
# make a set for each vertex. The set contains all the connected vertices
# use sets so the list is unique
vert_con = [set() for i in range(len(obj.data.vertices))]
for e in obj.data.edges:
vert_con[e.vertices[0]].add(e.vertices[1])
vert_con[e.vertices[1]].add(e.vertices[0])
for v in sel_verts:
pos = v.co * obj.matrix_world.transposed()
xco = obj.data.vertices[list(vert_con[v.index])[0]].co * obj.matrix_world.transposed()
zdir = (v.co + v.normal) * obj.matrix_world.transposed() - pos
zdir = zdir.normalized()
edir = pos - xco
# edir is nor perpendicular to z dir
# want xdir to be projection of edir onto plane through pos with direction zdir
xdir = edir - edir.dot(zdir) * zdir
xdir = -xdir.normalized()
copy = copyto(scene, source_obj, pos, xdir, zdir, axes)
copy_list.append(copy)
# select all copied objects
for copy in copy_list:
copy.select = True
obj.select = False
def edge_copy(scene, obj, source_obj, axes, es, scale):
# edge select mode
sel_edges = []
copy_list = []
for e in obj.data.edges:
if e.select is True:
sel_edges.append(e)
for e in sel_edges:
# pos is average of two edge vertexs
v0 = obj.data.vertices[e.vertices[0]].co * obj.matrix_world.transposed()
v1 = obj.data.vertices[e.vertices[1]].co * obj.matrix_world.transposed()
pos = (v0 + v1) / 2
# xdir is along edge
xdir = v0 - v1
xlen = xdir.magnitude
xdir = xdir.normalized()
# project each edge vertex normal onto plane normal to xdir
vn0 = (obj.data.vertices[e.vertices[0]].co * obj.matrix_world.transposed() +
obj.data.vertices[e.vertices[0]].normal) - v0
vn1 = (obj.data.vertices[e.vertices[1]].co * obj.matrix_world.transposed() +
obj.data.vertices[e.vertices[1]].normal) - v1
vn0p = vn0 - vn0.dot(xdir) * xdir
vn1p = vn1 - vn1.dot(xdir) * xdir
# the mean of the two projected normals is the zdir
zdir = vn0p + vn1p
zdir = zdir.normalized()
escale = None
if es:
escale = Vector([1.0, 1.0, 1.0])
i = list('XYZ').index(axes[1])
escale[i] = scale * xlen / source_obj.dimensions[i]
copy = copyto(scene, source_obj, pos, xdir, zdir, axes, scale=escale)
copy_list.append(copy)
# select all copied objects
for copy in copy_list:
copy.select = True
obj.select = False
def face_copy(scene, obj, source_obj, axes):
# face select mode
sel_faces = []
copy_list = []
for f in obj.data.polygons:
if f.select is True:
sel_faces.append(f)
for f in sel_faces:
fco = f.center * obj.matrix_world.transposed()
# get first vertex corner of transformed object
vco = obj.data.vertices[f.vertices[0]].co * obj.matrix_world.transposed()
# get face normal of transformed object
fn = (f.center + f.normal) * obj.matrix_world.transposed() - fco
fn = fn.normalized()
copy = copyto(scene, source_obj, fco, vco - fco, fn, axes)
copy_list.append(copy)
# select all copied objects
for copy in copy_list:
copy.select = True
obj.select = False
def register():
bpy.utils.register_class(Copy2)
def unregister():
bpy.utils.unregister_class(Copy2)
if __name__ == "__main__":
register()
|