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

skin.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 77792c1326b01b5f9acef5420c1de5d8df16f720 (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
#!BPY

"""
Name: 'Skin Two Vert-loops / Loft Multiple'
Blender: 234
Group: 'Mesh'
Submenu: 'Loft-loop - shortest edge method' A1
Submenu: 'Loft-loop - even method' A2
Submenu: 'Loft-segment - shortest edge' B1
Submenu: 'Loft-segment - even method' B2
Tooltip: 'Select 2 or more vert loops, then run this script'
"""

# $Id$
#
# -------------------------------------------------------------------------- 
# Skin Selected edges 1.0 By Campbell Barton (AKA Ideasman)
# -------------------------------------------------------------------------- 
# ***** 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 2 
# 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, write to the Free Software Foundation, 
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
# 
# ***** END GPL LICENCE BLOCK ***** 
# -------------------------------------------------------------------------- 



# Made by Ideasman/Campbell 2004/04/25 - ideasman@linuxmail.org

import Blender
from Blender import *
import math
from math import *
arg = __script__['arg']


#================#
# Math functions #
#================#

# Measure 2 points
def measure(v1, v2):
  return Mathutils.Vector([v1[0]-v2[0], v1[1] - v2[1], v1[2] - v2[2]]).length
  
# Clamp
def clamp(max, number):
	while number >= max:
		number = number - max
	return number

#=============================================================#
# List func that takes the last item and adds it to the front #
#=============================================================#
def listRotate(ls):
	return [ls[-1]] + ls[:-1]

#=================================================================#
# Recieve a list of locs: [x,y,z] and return the average location #
#=================================================================#
def averageLocation(locList):
	avLoc = [0,0,0]
	
	# Loop through x/y/z
	for coordIdx in [0,1,2]:
		
		# Add all the values from 1 of the 3 coords at the avLoc.
		for loc in locList:
			avLoc[coordIdx] += loc[coordIdx]
		
		avLoc[coordIdx] = avLoc[coordIdx] / len(locList)	
	return avLoc



#=============================#
# Blender functions/shortcuts #
#=============================#
def error(str):
	Draw.PupMenu('ERROR%t|'+str)

# Returns a new face that has the same properties as the origional face
# With no verts though
def copyFace(face):
  newFace = NMesh.Face()
  # Copy some generic properties
  newFace.mode = face.mode
  if face.image != None:
    newFace.image = face.image
  newFace.flag = face.flag
  newFace.mat = face.mat
  newFace.smooth = face.smooth
  return newFace

#=============================================#
# Find a selected vert that 2 faces share.    #
#=============================================#
def selVertBetween2Faces(face1, face2):
	for v1 in face1.v:
		if v1.sel:
			for v2 in face2.v:
				if v1 == v2:
					return v1
	
	
#=======================================================#
# Measure the total distance between all the edges in   #
# 2 vertex loops                                        #
#=======================================================#
def measureVloop(mesh, v1loop, v2loop, surplusFaces):
	totalDist = 0
	
	# Rotate the vertloops to cycle through each pair.
	# of faces to compate the distance between the 2 poins
	for ii in range(len(v1loop)):
		if ii not in surplusFaces:
			V1 = selVertBetween2Faces(mesh.faces[v1loop[0]], mesh.faces[v1loop[1]])
			V2 = selVertBetween2Faces(mesh.faces[v2loop[0]], mesh.faces[v2loop[1]])
			
			P1 = (V1[0],V1[1],V1[2])
			P2 = (V2[0],V2[1],V2[2])
	
			totalDist += measure(P1,P2)
			v1loop = listRotate(v1loop)
			v2loop = listRotate(v2loop)
	
	#selVertBetween2Faces(mesh.faces[v2loop[0]], mesh.faces[v2loop[1]])
	return totalDist

# Remove the shortest edge from a vert loop
def removeSmallestFace(mesh, vloop):
	bestDistSoFar = None
	bestFIdxSoFar = None
	for fIdx in vloop:
		vSelLs = []
		for v in mesh.faces[fIdx].v:
			if v.sel:
				vSelLs.append(v)
		
		dist = measure(vSelLs[0].co, vSelLs[1].co)
		
		if bestDistSoFar == None:
			bestDistSoFar = dist
			bestFIdxSoFar = fIdx 
		elif dist < bestDistSoFar:
			bestDistSoFar = dist
			bestFIdxSoFar = fIdx
	
	# Return the smallest face index of the vloop that was sent
	return bestFIdxSoFar


#=============================================#
# Take 2 vert loops and skin them             #
#=============================================#
def skinVertLoops(mesh, v1loop, v2loop):
	
	
	#=============================================#
	# Handle uneven vert loops, this is tricky    #
	#=============================================#
	# Reorder so v1loop is always the biggest
	if len(v1loop) < len(v2loop):
		v1loop, v2loop = v2loop, v1loop
	
	# Work out if the vert loops are equel or not, if not remove the extra faces from the larger
	surplusFaces = []
	tempv1loop = eval(str(v1loop)) # strip faces off this one, use it to keep track of which we have taken faces from.
	if len(v1loop) > len(v2loop):
		
		# Even face method.
		if arg[1] == '2':
			remIdx = 0
			faceStepping = len(	v1loop) / len(v2loop)
			while len(v1loop) - len(surplusFaces) > len(v2loop):
				remIdx += faceStepping
				surplusFaces.append(tempv1loop[ clamp(len(tempv1loop),remIdx) ]) 
				tempv1loop.remove(surplusFaces[-1])
		
		# Shortest face
		elif arg[1] == '1':
			while len(v1loop) - len(surplusFaces) > len(v2loop):
				surplusFaces.append(removeSmallestFace(mesh, tempv1loop)) 
				tempv1loop.remove(surplusFaces[-1])
			
	
	tempv1loop = None
	
	v2loop = optimizeLoopOrdedShortEdge(mesh, v1loop, v2loop, surplusFaces)
	
	# make Faces from 
	lenVloop = len(v1loop)
	lenSupFaces = len(surplusFaces)
	fIdx = 0
	offset = 0
	while fIdx < lenVloop:
		
		face = copyFace( mesh.faces[v1loop[clamp(lenVloop, fIdx+1)]] )
		
		if v1loop[fIdx] in surplusFaces:
			# Draw a try, this face does not catch with an edge.
			# So we must draw a tri and wedge it in.
			
			# Copy old faces properties
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v1loop[clamp(lenVloop, fIdx)]],\
			mesh.faces[v1loop[clamp(lenVloop, fIdx+1)]]) )
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v1loop[clamp(lenVloop, fIdx+1)]],\
			mesh.faces[v1loop[clamp(lenVloop, fIdx+2)]]) )
			
			#face.v.append( selVertBetween2Faces(\
			#mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, (fIdx - offset +1 ))]],\
			#mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, (fIdx - offset + 2))]]) )
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, (fIdx - offset))]],\
			mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, fIdx - offset + 1)]]) )
			
			mesh.faces.append(face)			
			
			# We need offset to work out how much smaller v2loop is at this current index.
			offset+=1		
			

		else:	
			# Draw a normal quad between the 2 edges/faces
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v1loop[clamp(lenVloop, fIdx)]],\
			mesh.faces[v1loop[clamp(lenVloop, fIdx+1)]]) )
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v1loop[clamp(lenVloop, fIdx+1)]],\
			mesh.faces[v1loop[clamp(lenVloop, fIdx+2)]]) )
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, (fIdx - offset +1 ))]],\
			mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, (fIdx - offset + 2))]]) )
			
			face.v.append( selVertBetween2Faces(\
			mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, (fIdx - offset))]],\
			mesh.faces[v2loop[clamp(lenVloop - lenSupFaces, fIdx - offset + 1)]]) )
			
			mesh.faces.append(face)
			
		fIdx +=1
		
	return mesh



#=======================================================#
# Takes a face and returns the number of selected verts #
#=======================================================#
def faceVSel(face):
	vSel = 0
	for v in face.v:
		if v.sel:
			vSel +=1
	return vSel




#================================================================#
# This function takes a face and returns its selected vert loop  #
# it returns a list of face indicies
#================================================================#
def vertLoop(mesh, startFaceIdx, fIgLs): # fIgLs is a list of faces to ignore.
	# Here we store the faces indicies that
	# are a part of the first vertex loop
	vertLoopLs = [startFaceIdx]

	restart = 0
	while restart == 0:
		# this keeps the face loop going until its told to stop,
		# If the face loop does not find an adjacent face then the vert loop has been compleated
		restart = 1 
		
		# Get my selected verts for the active face/edge.
		selVerts = []
		for v in mesh.faces[vertLoopLs[-1]].v:
			selVerts.append(v)
		
		fIdx = 0
		while fIdx < len(mesh.faces) and restart:
			# Not already added to the vert list
			if fIdx not in fIgLs + vertLoopLs:
				# Has 2 verts selected
				if faceVSel(mesh.faces[fIdx]) > 1:
					# Now we need to find if any of the selected verts
					# are shared with our active face. (are we next to ActiveFace)
					for v in mesh.faces[fIdx].v:
						if v in selVerts:
							vertLoopLs.append(fIdx)
							restart = 0 # restart the face loop.
							break
					
			fIdx +=1
			
	return vertLoopLs




#================================================================#
# Now we work out the optimum order to 'skin' the 2 vert loops   #
# by measuring the total distance of all edges created,          #
# test this for every possible series of joins                   # 
# and find the shortest, Once this is done the                   #
# shortest dist can be skinned.                                  #
# returns only the 2nd-reordered vert loop                       #
#================================================================#
def optimizeLoopOrded(mesh, v1loop, v2loop):
	bestSoFar = None
	
	# Measure the dist, ii is just a counter
	for ii in range(len(v1loop)):
		
		# Loop twice , Once for the forward test, and another for the revearsed
		for iii in [0, 0]:
			dist = measureVloop(mesh, v1loop, v2loop)
			# Initialize the Best distance recorded
			if bestSoFar == None:
				bestSoFar = dist
				bestv2Loop = eval(str(v2loop))
				
			elif dist < bestSoFar: # Update the info if a better vloop rotation is found.
				bestSoFar = dist
				bestv2Loop = eval(str(v2loop))
			
			# We might have got the vert loop backwards, try the other way
			v2loop.reverse()
		v2loop = listRotate(v2loop)
	return bestv2Loop
	
	
	
#================================================================#
# Now we work out the optimum order to 'skin' the 2 vert loops   #
# by measuring the total distance of all edges created,          #
# test this for every possible series of joins                   # 
# and find the shortest, Once this is done the                   #
# shortest dist can be skinned.                                  #
# returns only the 2nd-reordered vert loop                       #
#================================================================#
def optimizeLoopOrdedShortEdge(mesh, v1loop, v2loop, surplusFaces):
	bestSoFar = None
	
	# Measure the dist, ii is just a counter
	for ii in range(len(v2loop)):
		
		# Loop twice , Once for the forward test, and another for the revearsed
		for iii in [0, 0]:
			dist = measureVloop(mesh, v1loop, v2loop, surplusFaces)
			print 'dist', dist 
			# Initialize the Best distance recorded
			if bestSoFar == None:
				bestSoFar = dist
				bestv2Loop = eval(str(v2loop))
				
			elif dist < bestSoFar: # Update the info if a better vloop rotation is found.
				bestSoFar = dist
				bestv2Loop = eval(str(v2loop))
			
			# We might have got the vert loop backwards, try the other way
			v2loop.reverse()
		v2loop = listRotate(v2loop)
	print 'best so far ', bestSoFar
	return bestv2Loop	
	
	
	
	


#==============================#
#  Find our     vert loop list #
#==============================#
# Find a face with 2 verts selected,
#this will be the first face in out vert loop
def findVertLoop(mesh, fIgLs): # fIgLs is a list of faces to ignore.
	
	startFaceIdx = None
	
	fIdx = 0
	while fIdx < len(mesh.faces):	
		if fIdx not in fIgLs:
			# Do we have an edge?
			if faceVSel(mesh.faces[fIdx]) > 1:
				# THIS IS THE STARTING FACE.
				startFaceIdx = fIdx
				break
		fIdx+=1
	
	# Here we access the function that generates the real vert loop
	if startFaceIdx != None:
		return vertLoop(mesh, startFaceIdx, fIgLs)
	else:
		# We are out'a vert loops, return a None,
		return None

#===================================#
# Get the average loc of a vertloop #
# This is used when working out the #
# order to loft an object           #
#===================================#
def vLoopAverageLoc(mesh, vertLoop):
	locList = [] # List of vert locations
		
	fIdx = 0
	while fIdx < len(mesh.faces):	
		if fIdx in vertLoop:
			for v in mesh.faces[fIdx].v:
				if v.sel:
					locList.append(v.co)
		fIdx+=1
	
	return averageLocation(locList)



#=================================================#
# Vert loop group functions

def getAllVertLoops(mesh):
	# Make a chain of vert loops.
	fIgLs = [] # List of faces to ignore 
	allVLoops = [findVertLoop(mesh, fIgLs)]
	while allVLoops[-1] != None:
		
		# In future ignore all faces in this vert loop
		fIgLs += allVLoops[-1]		
		
		# Add the new vert loop to the list
		allVLoops.append( findVertLoop(mesh, fIgLs) )
	
	return allVLoops[:-1] # Remove the last Value- None.
	
	
def reorderCircularVLoops(mesh, allVLoops):
	# Now get a location for each vert loop.
	allVertLoopLocs = []
	for vLoop in allVLoops:
		allVertLoopLocs.append( vLoopAverageLoc(mesh, vLoop) )

	# We need to find the longest distance between 2 vert loops so we can 
	reorderedVLoopLocs = []

	# Start with this one, then find the next closest.
	# in doing this make a new list called reorderedVloop
	currentVLoop = 0
	reorderedVloopIdx = [currentVLoop]
	newOrderVLoops = [allVLoops[0]] # This is a re-ordered allVLoops
	while len(reorderedVloopIdx) != len(allVLoops):
		bestSoFar = None
		bestVIdxSoFar = None
		for vLoopIdx in range(len(allVLoops)):
			if vLoopIdx not in reorderedVloopIdx + [currentVLoop]:
				if bestSoFar == None:
					bestSoFar = measure( allVertLoopLocs[vLoopIdx], allVertLoopLocs[currentVLoop] )
					bestVIdxSoFar = vLoopIdx
				else:
					newDist = measure( allVertLoopLocs[vLoopIdx], allVertLoopLocs[currentVLoop] )
					if newDist < bestSoFar:
						bestSoFar = newDist
						bestVIdxSoFar = vLoopIdx
		
		reorderedVloopIdx.append(bestVIdxSoFar)
		reorderedVLoopLocs.append(allVertLoopLocs[bestVIdxSoFar])
		newOrderVLoops.append( allVLoops[bestVIdxSoFar] ) 
		
		# Start looking for the next best fit
		currentVLoop = bestVIdxSoFar
	
	# This is not the locicle place to put this but its convieneint.
	# Here we find the 2 vert loops that are most far apart
	# We use this to work out which 2 vert loops not to skin when making an open loft.
	vLoopIdx = 0
	# Longest measured so far - 0 dummy.
	bestSoFar = 0
	while vLoopIdx < len(reorderedVLoopLocs):
		
		
		# Skin back to the start if needs be, becuase this is a crcular loft
		toSkin2 = vLoopIdx + 1
		if toSkin2 == len(reorderedVLoopLocs):
			toSkin2 = 0
			
		
		newDist  = measure( reorderedVLoopLocs[vLoopIdx], reorderedVLoopLocs[toSkin2] )
		
		if newDist >= bestSoFar:
			bestSoFar = newDist
			vLoopIdxNotToSkin = vLoopIdx + 1	
				
		vLoopIdx +=1 
	
	return newOrderVLoops, vLoopIdxNotToSkin


is_editmode = Window.EditMode()
if is_editmode: Window.EditMode(0)

# Get a mesh and raise errors if we cant
mesh = None
if len(Object.GetSelected()) > 0:
  if Object.GetSelected()[0].getType() == 'Mesh':
    mesh = Object.GetSelected()[0].getData()
  else:
    error('please select a mesh')
else:
  error('no mesh object selected')


if mesh != None:
  allVLoops = getAllVertLoops(mesh)
  
  # Re order the vert loops
  allVLoops, vLoopIdxNotToSkin = reorderCircularVLoops(mesh, allVLoops)	
  
  vloopIdx = 0
  while vloopIdx < len(allVLoops):
    #print range(len(allVLoops) )
    #print vloopIdx
    #print allVLoops[vloopIdx]
    
    # Skin back to the start if needs be, becuase this is a crcular loft
    toSkin2 = vloopIdx + 1
    if toSkin2 == len(allVLoops):
      toSkin2 = 0
    
    # Circular loft or not?
    if arg[0] == 'B': # B for open
      if vloopIdx != vLoopIdxNotToSkin:
        mesh = skinVertLoops(mesh, allVLoops[vloopIdx], allVLoops[toSkin2])
    elif arg[0] == 'A': # A for closed
      mesh = skinVertLoops(mesh, allVLoops[vloopIdx], allVLoops[toSkin2])
    
    vloopIdx +=1	
  
  mesh.update()

if is_editmode: Window.EditMode(1)