[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24282] trunk/blender: quick port of smart project to 2.5x, no operator options yet

Campbell Barton ideasman42 at gmail.com
Tue Nov 3 18:51:22 CET 2009


Revision: 24282
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24282
Author:   campbellbarton
Date:     2009-11-03 18:51:22 +0100 (Tue, 03 Nov 2009)

Log Message:
-----------
quick port of smart project to 2.5x, no operator options yet

Modified Paths:
--------------
    trunk/blender/release/scripts/io/uvcalc_smart_project.py
    trunk/blender/source/blender/makesrna/intern/rna_mesh.c

Modified: trunk/blender/release/scripts/io/uvcalc_smart_project.py
===================================================================
--- trunk/blender/release/scripts/io/uvcalc_smart_project.py	2009-11-03 17:47:44 UTC (rev 24281)
+++ trunk/blender/release/scripts/io/uvcalc_smart_project.py	2009-11-03 17:51:22 UTC (rev 24282)
@@ -1,26 +1,5 @@
-#!BPY
-
-""" Registration info for Blender menus: <- these words are ignored
-Name: 'Unwrap (smart projections)'
-Blender: 240
-Group: 'UVCalculation'
-Tooltip: 'UV Unwrap mesh faces for all select mesh objects'
-"""
-
-
-__author__ = "Campbell Barton"
-__url__ = ("blender", "blenderartists.org")
-__version__ = "1.1 12/18/05"
-
-__bpydoc__ = """\
-This script projection unwraps the selected faces of a mesh.
-
-it operates on all selected mesh objects, and can be used unwrap
-selected faces, or all faces.
-"""
-
 # -------------------------------------------------------------------------- 
-# Smart Projection UV Projection Unwrapper v1.1 by Campbell Barton (AKA Ideasman) 
+# Smart Projection UV Projection Unwrapper v1.2 by Campbell Barton (AKA Ideasman) 
 # -------------------------------------------------------------------------- 
 # ***** BEGIN GPL LICENSE BLOCK ***** 
 # 
@@ -42,10 +21,12 @@
 # -------------------------------------------------------------------------- 
 
 
-from Blender import Object, Draw, Window, sys, Mesh, Geometry
-from Blender.Mathutils import Matrix, Vector, RotationMatrix
+#from Blender import Object, Draw, Window, sys, Mesh, Geometry
+from Mathutils import Matrix, Vector, RotationMatrix
+import time
+import Geometry
 import bpy
-from math import cos
+from math import cos, degrees, radians
 
 DEG_TO_RAD = 0.017453292519943295 # pi/180.0
 SMALL_NUM = 0.000000001
@@ -182,7 +163,7 @@
 	# Sort by length
 	
 		
-	length_sorted_edges = [(Vector(key[0]), Vector(key[1]), value) for key, value in edges.iteritems() if value != 0]
+	length_sorted_edges = [(Vector(key[0]), Vector(key[1]), value) for key, value in edges.items() if value != 0]
 	
 	try:	length_sorted_edges.sort(key = lambda A: -A[2]) # largest first
 	except:	length_sorted_edges.sort(lambda A, B: cmp(B[2], A[2]))
@@ -192,7 +173,7 @@
 	#	e.pop(2)
 	
 	# return edges and unique points
-	return length_sorted_edges, [v.__copy__().resize3D() for v in unique_points.itervalues()]
+	return length_sorted_edges, [v.__copy__().resize3D() for v in unique_points.values()]
 	
 # ========================= NOT WORKING????
 # Find if a points inside an edge loop, un-orderd.
@@ -294,15 +275,15 @@
 	
 # Takes a list of faces that make up a UV island and rotate
 # until they optimally fit inside a square.
-ROTMAT_2D_POS_90D = RotationMatrix( 90, 2)
-ROTMAT_2D_POS_45D = RotationMatrix( 45, 2)
+ROTMAT_2D_POS_90D = RotationMatrix( radians(90.0), 2)
+ROTMAT_2D_POS_45D = RotationMatrix( radians(45.0), 2)
 
 RotMatStepRotation = []
 rot_angle = 22.5 #45.0/2
 while rot_angle > 0.1:
 	RotMatStepRotation.append([\
-	 RotationMatrix( rot_angle, 2),\
-	 RotationMatrix( -rot_angle, 2)])
+	 RotationMatrix( radians(rot_angle), 2),\
+	 RotationMatrix( radians(-rot_angle), 2)])
 	
 	rot_angle = rot_angle/2.0
 	
@@ -381,7 +362,7 @@
 	i = 0 # count the serialized uv/vectors
 	for f in faces:
 		#f.uv = [uv for uv in uvVecs[i:len(f)+i] ]
-		for j, k in enumerate(xrange(i, len(f.v)+i)):
+		for j, k in enumerate(range(i, len(f.v)+i)):
 			f.uv[j][:] = uvVecs[k]
 		i += len(f.v)
 
@@ -428,16 +409,13 @@
 	# no.. chance that to most simple edge loop first.
 	decoratedIslandListAreaSort =decoratedIslandList[:]
 	
-	try:	decoratedIslandListAreaSort.sort(key = lambda A: A[3])
-	except:	decoratedIslandListAreaSort.sort(lambda A, B: cmp(A[3], B[3]))
+	decoratedIslandListAreaSort.sort(key = lambda A: A[3])
 	
-	
 	# sort by efficiency, Least Efficient first.
 	decoratedIslandListEfficSort = decoratedIslandList[:]
 	# decoratedIslandListEfficSort.sort(lambda A, B: cmp(B[2], A[2]))
 
-	try:	decoratedIslandListEfficSort.sort(key = lambda A: -A[2])
-	except:	decoratedIslandListEfficSort.sort(lambda A, B: cmp(B[2], A[2]))
+	decoratedIslandListEfficSort.sort(key = lambda A: -A[2])
 
 	# ================================================== THESE CAN BE TWEAKED.
 	# This is a quality value for the number of tests.
@@ -557,7 +535,7 @@
 							elif Intersect == 0: # No intersection?? Place it.
 								# Progress
 								removedCount +=1
-								Window.DrawProgressBar(0.0, 'Merged: %i islands, Ctrl to finish early.' % removedCount)
+#XXX								Window.DrawProgressBar(0.0, 'Merged: %i islands, Ctrl to finish early.' % removedCount)
 								
 								# Move faces into new island and offset
 								targetIsland[0].extend(sourceIsland[0])
@@ -581,7 +559,7 @@
 								
 								# Sort by edge length, reverse so biggest are first.
 								
-								try: 	targetIsland[6].sort(key = lambda A: A[2])
+								try:	 targetIsland[6].sort(key = lambda A: A[2])
 								except:	targetIsland[6].sort(lambda B,A: cmp(A[2], B[2] ))
 								
 								
@@ -625,16 +603,15 @@
 	
 	# Get seams so we dont cross over seams
 	edge_seams = {} # shoudl be a set
-	SEAM = Mesh.EdgeFlags.SEAM
 	for ed in me.edges:
-		if ed.flag & SEAM:
+		if ed.seam:
 			edge_seams[ed.key] = None # dummy var- use sets!			
 	# Done finding seams
 	
 	
 	islandList = []
 	
-	Window.DrawProgressBar(0.0, 'Splitting %d projection groups into UV islands:' % len(faceGroups))
+#XXX	Window.DrawProgressBar(0.0, 'Splitting %d projection groups into UV islands:' % len(faceGroups))
 	#print '\tSplitting %d projection groups into UV islands:' % len(faceGroups),
 	# Find grouped faces
 	
@@ -652,7 +629,7 @@
 		
 		for i, f in enumerate(faces):
 			for ed_key in f.edge_keys:
-				if edge_seams.has_key(ed_key): # DELIMIT SEAMS! ;)
+				if ed_key in edge_seams: # DELIMIT SEAMS! ;)
 					edge_users[ed_key] = [] # so as not to raise an error
 				else:
 					try:		edge_users[ed_key].append(i)
@@ -677,7 +654,7 @@
 			ok = True
 			while ok:
 				ok= False
-				for i in xrange(len(faces)):
+				for i in range(len(faces)):
 					if face_modes[i] == 1: # search
 						for ed_key in faces[i].edge_keys:
 							for ii in edge_users[ed_key]:
@@ -691,7 +668,7 @@
 			islandList.append(newIsland)
 			
 			ok = False
-			for i in xrange(len(faces)):
+			for i in range(len(faces)):
 				if face_modes[i] == 0:
 					newIsland = []
 					newIsland.append(faces[i])
@@ -700,7 +677,7 @@
 					break
 			# if not ok will stop looping
 	
-	Window.DrawProgressBar(0.1, 'Optimizing Rotation for %i UV Islands' % len(islandList))
+#XXX	Window.DrawProgressBar(0.1, 'Optimizing Rotation for %i UV Islands' % len(islandList))
 	
 	for island in islandList:
 		optiRotateUvIsland(island)
@@ -710,7 +687,7 @@
 
 def packIslands(islandList):
 	if USER_FILL_HOLES:
-		Window.DrawProgressBar(0.1, 'Merging Islands (Ctrl: skip merge)...')
+#XXX		Window.DrawProgressBar(0.1, 'Merging Islands (Ctrl: skip merge)...')
 		mergeUvIslands(islandList) # Modify in place
 		
 	
@@ -760,18 +737,18 @@
 	# with the islands.
 	
 	#print '\tPacking UV Islands...'
-	Window.DrawProgressBar(0.7, 'Packing %i UV Islands...' % len(packBoxes) )
+#XXX	Window.DrawProgressBar(0.7, 'Packing %i UV Islands...' % len(packBoxes) )
 	
-	time1 = sys.time()
+	time1 = time.time()
 	packWidth, packHeight = Geometry.BoxPack2D(packBoxes)
 	
-	# print 'Box Packing Time:', sys.time() - time1
+	# print 'Box Packing Time:', time.time() - time1
 	
 	#if len(pa	ckedLs) != len(islandList):
 	#	raise "Error packed boxes differes from original length"
 	
 	#print '\tWriting Packed Data to faces'
-	Window.DrawProgressBar(0.8, 'Writing Packed Data to faces')
+#XXX	Window.DrawProgressBar(0.8, 'Writing Packed Data to faces')
 	
 	# Sort by ID, so there in sync again
 	islandIdx = len(islandList)
@@ -812,50 +789,73 @@
 	return Matrix([a1[0], a1[1], a1[2]], [a2[0], a2[1], a2[2]], [a3[0], a3[1], a3[2]])
 
 
+# Utility funcs for 2.5, make into a module??
+def ord_ind(i1,i2):
+	if i1<i2: return i1,i2
+	return i2,i1
+	
+def edge_key(ed):
+	v1,v2 = tuple(ed.verts)
+	return ord_ind(v1, v2)
 
+def face_edge_keys(f):
+	verts  = tuple(f.verts)
+	if len(verts)==3:
+		return ord_ind(verts[0], verts[1]),  ord_ind(verts[1], verts[2]),  ord_ind(verts[2], verts[0])
+	
+	return ord_ind(verts[0], verts[1]),  ord_ind(verts[1], verts[2]),  ord_ind(verts[2], verts[3]),  ord_ind(verts[3], verts[0])
+
+
 class thickface(object):
 	__slost__= 'v', 'uv', 'no', 'area', 'edge_keys'
-	def __init__(self, face):
-		self.v = face.v
-		self.uv = face.uv
-		self.no = face.no
+	def __init__(self, face, uvface, mesh_verts):
+		self.v = [mesh_verts[i] for i in face.verts]
+		if len(self.v)==4:
+			self.uv = uvface.uv1, uvface.uv2, uvface.uv3, uvface.uv4
+		else:
+			self.uv = uvface.uv1, uvface.uv2, uvface.uv3
+			
+		self.no = face.normal
 		self.area = face.area
-		self.edge_keys = face.edge_keys
+		self.edge_keys = face_edge_keys(face)
 
 global ob
 ob = None
-def main():
+def main(context):
 	global USER_FILL_HOLES
 	global USER_FILL_HOLES_QUALITY
 	global USER_STRETCH_ASPECT
 	global USER_ISLAND_MARGIN
 	
-	objects= bpy.data.scenes.active.objects
+#XXX objects= bpy.data.scenes.active.objects
+	objects = context.selected_editable_objects
 	
+	
 	# we can will tag them later.
-	obList =  [ob for ob in objects.context if ob.type == 'Mesh']
+	obList =  [ob for ob in objects if ob.type == 'MESH']
 	
 	# Face select object may not be selected.
-	ob = objects.active
-	if ob and ob.sel == 0 and ob.type == 'Mesh':
+#XXX	ob = objects.active
+	ob= objects[0]
+
+	if ob and ob.selected == 0 and ob.type == 'MESH':
 		# Add to the list
 		obList =[ob]
 	del objects
 	
 	if not obList:
-		Draw.PupMenu('error, no selected mesh objects')
-		return
+		raise('error, no selected mesh objects')
 	
 	# Create the variables.
-	USER_PROJECTION_LIMIT = Draw.Create(66)
-	USER_ONLY_SELECTED_FACES = Draw.Create(1)
-	USER_SHARE_SPACE = Draw.Create(1) # Only for hole filling.
-	USER_STRETCH_ASPECT = Draw.Create(1) # Only for hole filling.
-	USER_ISLAND_MARGIN = Draw.Create(0.0) # Only for hole filling.
-	USER_FILL_HOLES = Draw.Create(0)
-	USER_FILL_HOLES_QUALITY = Draw.Create(50) # Only for hole filling.
-	USER_VIEW_INIT = Draw.Create(0) # Only for hole filling.
-	USER_AREA_WEIGHT = Draw.Create(1) # Only for hole filling.
+	USER_PROJECTION_LIMIT = (66)
+	USER_ONLY_SELECTED_FACES = (1)
+	USER_SHARE_SPACE = (1) # Only for hole filling.
+	USER_STRETCH_ASPECT = (1) # Only for hole filling.
+	USER_ISLAND_MARGIN = (0.0) # Only for hole filling.
+	USER_FILL_HOLES = (0)

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list