- Campbell Barton updated his obj exporter and skin (bridge/skin/loft) scripts.  Thanks.
This commit is contained in:
Willian Padovani Germano 2005-06-13 17:21:30 +00:00
parent e5b527c629
commit de567cd0cc
2 changed files with 91 additions and 100 deletions

@ -2,7 +2,7 @@
"""
Name: 'Wavefront (.obj)...'
Blender: 237
Blender: 232
Group: 'Export'
Tooltip: 'Save a Wavefront OBJ File'
"""
@ -56,37 +56,20 @@ NULL_IMG = '(null)' # from docs at http://astronomy.swin.edu.au/~pbourke/geomfor
def save_mtl(filename):
file = open(filename, "w")
file.write('# Blender MTL File: %s\n' % (Get('filename')))
for mat in Material.Get():
file.write('newmtl %s\n' % (mat.getName())) # Define a new material
# Hardness, convert blenders 1-511 to MTL's
file.write('Ns %s\n' % ((mat.getHardness()-1) * 1.9607843137254901 ) )
col = mat.getRGBCol()
# Diffuse
file.write('Kd %s %s %s\n' % (col[0], col[1], col[2]))
col = mat.getMirCol()
# Ambient, uses mirror colour,
file.write('Ka %s %s %s\n' % (col[0], col[1], col[2]))
col = mat.getSpecCol()
# Specular
file.write('Ks %s %s %s\n' % (col[0],col[1], col[2]))
# Alpha (dissolve)
file.write('d %s\n' % (mat.getAlpha()))
file.write('Ns %s\n' % ((mat.getHardness()-1) * 1.9607843137254901 ) ) # Hardness, convert blenders 1-511 to MTL's
file.write('Kd %.6f %.6f %.6f\n' % tuple(mat.getRGBCol())) # Diffuse
file.write('Ka %.6f %.6f %.6f\n' % tuple(mat.getMirCol())) # Ambient, uses mirror colour,
file.write('Ks %.6f %.6f %.6f\n' % tuple(mat.getSpecCol())) # Specular
file.write('d %.6f\n' % mat.getAlpha()) # Alpha (obj uses 'd' for dissolve)
# illum, 0 to disable lightng, 2 is normal.
if mat.getMode() & Material.Modes['SHADELESS']:
file.write('illum 0\n') # ignore lighting
file.write('illum 0\n\n') # ignore lighting
else:
file.write('illum 2\n') # light normaly
# End OF Mat
file.write('\n') # new line
file.write('illum 2\n\n') # light normaly
file.close()
def save_obj(filename):
@ -126,24 +109,20 @@ def save_obj(filename):
# Vert
for v in m.verts:
file.write('v %s %s %s\n' % (v.co.x, v.co.y, v.co.z))
file.write('v %.6f %.6f %.6f\n' % tuple(v.co))
# UV
for f in m.faces:
''' # Export edges?
if len(f.v) < 3:
continue
'''
for uvIdx in range(len(f.v)):
if f.uv:
file.write('vt %s %s 0.0\n' % (f.uv[uvIdx][0], f.uv[uvIdx][1]))
file.write('vt %.6f %.6f 0.0\n' % f.uv[uvIdx])
else:
file.write('vt 0.0 0.0 0.0\n')
# NORMAL
for f1 in m.faces:
for v in f1.v:
file.write('vn %s %s %s\n' % (v.no.x, v.no.y, v.no.z))
for f in m.faces:
for v in f.v:
file.write('vn %.6f %.6f %.6f\n' % tuple(v.no))
uvIdx = 0
for f in m.faces:
@ -172,7 +151,7 @@ def save_obj(filename):
file.write('f ')
for v in f.v:
file.write( '%s/%s/%s ' % (m.verts.index(v) + totverts+1, uvIdx+totuvco+1, uvIdx+totuvco+1))
file.write( '%s/%s/%s ' % (v.index + totverts+1, uvIdx+totuvco+1, uvIdx+totuvco+1))
uvIdx+=1
file.write('\n')
@ -181,6 +160,6 @@ def save_obj(filename):
totverts += len(m.verts)
totuvco += uvIdx
file.close()
print "obj export time: ", sys.time() - time1
print "obj export time: %.2f" % (sys.time() - time1)
Window.FileSelector(save_obj, 'Export Wavefront OBJ', newFName('obj'))

@ -1,19 +1,15 @@
#!BPY
"""
Name: 'Skin Two Vert-loops / Loft Multiple'
Name: 'Bridge/Skin/Loft'
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'
"""
__author__ = "Campbell Barton"
__url__ = ["blender", "elysiun"]
__version__ = "1.0 2004/04/25"
__author__ = "Campbell Barton AKA Ideasman"
__url__ = ["http://members.iinet.net.au/~cpbarton/ideasman/", "blender", "elysiun"]
__version__ = "1.1 2005/06/13"
__bpydoc__ = """\
With this script vertex loops can be skinned: faces are created to connect the
@ -23,7 +19,11 @@ Usage:
In mesh Edit mode select the vertices of the loops (closed paths / curves of
vertices: circles, for example) that should be skinned, then run this script.
A pop-up will provide further options, if the results of a method are not adequate try one of the others.
A pop-up will provide further options.
Notes:
If the results of a method chosen from the pop-up are not adequate, undo and try one of the others.
"""
@ -59,7 +59,23 @@ import Blender
from Blender import *
import math
from math import *
arg = __script__['arg']
choice = Draw.PupMenu(\
'Loft-loop - shortest edge method|\
Loft-loop - even method|\
Loft-segment - shortest edge|\
Loft-segment - even method')
if choice == 1:
arg='A1'
elif choice == 2:
arg='A2'
elif choice == 3:
arg='B1'
elif choice == 4:
arg='B2'
#================#
@ -80,7 +96,7 @@ def clamp(max, number):
# List func that takes the last item and adds it to the front #
#=============================================================#
def listRotate(ls):
return [ls[-1]] + ls[:-1]
ls.append(ls.pop(0))
#=================================================================#
# Recieve a list of locs: [x,y,z] and return the average location #
@ -134,22 +150,26 @@ def selVertBetween2Faces(face1, face2):
# Measure the total distance between all the edges in #
# 2 vertex loops #
#=======================================================#
def measureVloop(mesh, v1loop, v2loop, surplusFaces):
def measureVloop(mesh, v1loop, v2loop, surplusFaces, bestSoFar):
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]])
# Clamp
v2clampii = ii
while v2clampii >= len(v2loop):
v2clampii -= len(v2loop)
print v2clampii
P1 = (V1[0],V1[1],V1[2])
P2 = (V2[0],V2[1],V2[2])
V1 = selVertBetween2Faces(mesh.faces[v1loop[ii-1]], mesh.faces[v1loop[ii]])
V2 = selVertBetween2Faces(mesh.faces[v2loop[v2clampii-1]], mesh.faces[v2loop[v2clampii]])
totalDist += measure(P1,P2)
v1loop = listRotate(v1loop)
v2loop = listRotate(v2loop)
totalDist += measure(V1, V2)
# Bail out early if not an improvement on previously measured.
if bestSoFar != None and totalDist > bestSoFar:
return totalDist
#selVertBetween2Faces(mesh.faces[v2loop[0]], mesh.faces[v2loop[1]])
return totalDist
@ -192,7 +212,7 @@ def skinVertLoops(mesh, v1loop, v2loop):
# 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.
tempv1loop = 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.
@ -348,24 +368,19 @@ def optimizeLoopOrded(mesh, v1loop, v2loop):
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)
for iii in [None, None]:
dist = measureVloop(mesh, v1loop, v2loop, bestSoFar)
# Initialize the Best distance recorded
if bestSoFar == None:
if bestSoFar == None or dist < bestSoFar:
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))
bestv2Loop = v2loop[:]
# We might have got the vert loop backwards, try the other way
v2loop.reverse()
v2loop = listRotate(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, #
@ -381,29 +396,23 @@ def optimizeLoopOrdedShortEdge(mesh, v1loop, v2loop, surplusFaces):
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)
for iii in [None, None]:
dist = measureVloop(mesh, v1loop, v2loop, surplusFaces, bestSoFar)
print 'dist', dist
# Initialize the Best distance recorded
if bestSoFar == None:
if bestSoFar == None or dist < bestSoFar:
bestSoFar = dist
bestv2Loop = eval(str(v2loop))
bestv2Loop = 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)
#v2loop = listRotate(v2loop)
listRotate(v2loop)
print 'best so far ', bestSoFar
return bestv2Loop
#==============================#
# Find our vert loop list #
#==============================#
@ -511,13 +520,11 @@ def reorderCircularVLoops(mesh, allVLoops):
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:
@ -534,7 +541,9 @@ if is_editmode: Window.EditMode(0)
# Get a mesh and raise errors if we cant
mesh = None
if len(Object.GetSelected()) > 0:
if choice == -1:
pass
elif len(Object.GetSelected()) > 0:
if Object.GetSelected()[0].getType() == 'Mesh':
mesh = Object.GetSelected()[0].getData()
else:
@ -542,8 +551,9 @@ if len(Object.GetSelected()) > 0:
else:
error('no mesh object selected')
time1 = sys.time()
if mesh != None:
Window.WaitCursor(1)
allVLoops = getAllVertLoops(mesh)
# Re order the vert loops
@ -569,6 +579,8 @@ if mesh != None:
vloopIdx +=1
mesh.update()
mesh.update(1,(mesh.edges != []),0)
if is_editmode: Window.EditMode(1)
Window.WaitCursor(0)
print "skinning time: %.2f" % (sys.time() - time1)