Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!BPY
- """
- Name: 'TRK'
- Blender: 249
- Group: 'Export'
- Tip: 'Export VDrift race track. (.trk)'
- """
- ######################################################
- # VDrift Track Exporter
- # Date: 13 JAN 10
- # Ver: 0.1
- ######################################################
- import Blender
- from Blender import *
- import bpy
- ######################################################
- # Bezier Patch
- ######################################################
- class bpatch:
- def __init__(self, (fl, fr, bl, br)):
- # 4x4 array
- self.points = [[None] * 4 for i in range(4)]
- # corners
- self.points[0][0] = fl
- self.points[0][3] = fr
- self.points[3][0] = bl
- self.points[3][3] = br
- # intermediate front and back points
- temp = fr - fl
- self.points[0][1] = fl + temp * 1.0 / 3.0
- self.points[0][2] = fl + temp * 2.0 / 3.0
- temp = br - bl
- self.points[3][1] = bl + temp * 1.0 / 3.0
- self.points[3][2] = bl + temp * 2.0 / 3.0
- # intermediate left and right points
- for i in range(4):
- temp = self.points[3][i] - self.points[0][i]
- self.points[1][i] = self.points[0][i] + temp * 1.0 / 3.0
- self.points[2][i] = self.points[0][i] + temp * 2.0 / 3.0
- def attach(self, other):
- for x in range(4):
- slope = (other.points[0][x] - self.points[3][x]).normalize()
- print slope
- otherlen = (other.points[0][x] - other.points[3][x]).length
- mylen = (self.points[0][x] - self.points[3][x]).length
- meanlen = min(otherlen, mylen)
- other.points[2][x] = other.points[3][x] + slope * meanlen / 3.0
- self.points[1][x] = self.points[0][x] - slope * meanlen / 3.0
- def string(self):
- patch_str = ""
- for x in range(4):
- for y in range(4):
- p = self.points[x][y]
- #patch_str += str(p.x) + " " + str(p.y) + " " + str(p.z) + "\n"
- patch_str += str(p.y) + " " + str(p.z) + " " + str(p.x) + "\n"
- return patch_str
- ######################################################
- # main
- ######################################################
- # the edge is the key, and a list of faces that use it are the value
- def edge_faces_dict(mesh):
- edge_faces = dict([(edge.key, []) for edge in mesh.edges])
- for face in mesh.faces:
- for key in face.edge_keys:
- edge_faces[key].append(face)
- return edge_faces
- # get next face that shares an edge
- def next_face_key(face, prev_key, edge_faces):
- for key in face.edge_keys:
- if key != prev_key:
- faces = edge_faces[key]
- if len(faces) == 2:
- if faces[0] == face:
- return faces[1], key
- else:
- return faces[0], key
- return face, prev_key
- # get back left, back right, front left, front right vertices
- # key0 is back edge, key1 is front edge
- def face_verts(key0, key1, mesh):
- fl = mesh.verts[key1[0]].co
- fr = mesh.verts[key1[1]].co
- bl = mesh.verts[key0[0]].co
- br = mesh.verts[key0[1]].co
- f = fl - bl
- if (br - bl).cross(f).z < 0: #z is up-axis!!!
- bl, br = br, bl
- if (fr - fl).cross(f).z < 0: #z is up-axis!!!
- fl, fr = fr, fl
- return fl, fr, bl, br
- # closed loop track only atm ;)
- def create_track(mesh):
- patches = []
- edge_faces = edge_faces_dict(mesh)
- for key, faces in edge_faces.iteritems():
- if len(faces) == 2: break
- f0, k0 = faces[0], key # first track face(closed loop track)
- while 1:
- f1, k1 = next_face_key(f0, k0, edge_faces)
- if k1 == k0:
- print "end of track\n"
- break # end of track, oops
- verts = face_verts(k0, k1, mesh)
- patches.append(bpatch(verts))
- f0, k0 = f1, k1
- if k1 == key: break # loop closed, done
- for i in range(len(patches)):
- patches[i-1].attach(patches[i]) # closed loop track
- return patches
- # tracks are quad strip loop meshes named "track n" (n number of the track)
- def export_tracks(filepath):
- # export tracks
- trk = file(filepath, 'w')
- editmode = Window.EditMode()
- if editmode: Window.EditMode(0)
- tracks = filter(lambda o: o.getType()=="Mesh" and o.name.startswith("track") , bpy.data.scenes.active.objects)
- if len(tracks):
- trk.write(str(len(tracks)) + "\n\n") #number of tracks
- mesh = bpy.data.meshes.new('temp') # temporary mesh to hold actual (modified) mesh data
- for track in tracks:
- mesh.getFromObject(track)
- mesh.transform(track.matrixWorld)
- patches = create_track(mesh)
- if len(patches):
- trk.write(str(len(patches)) + "\n\n") #number of patches
- for patch in patches:
- trk.write(patch.string() + "\n")
- if editmode: Window.EditMode(1)
- trk.close()
- # export start positions, lap segments(start/finich line)
- txt = file(filepath + ".txt", 'w')
- txt.write("cull faces = on\n")
- txt.write("non-treaded friction coefficient = 1.0\n")
- txt.write("treaded friction coefficient = 0.9\n")
- lap = filter(lambda o: o.name.startswith("lap sequence "), bpy.data.scenes.active.objects)
- txt.write("lap sequences = " + str(len(lap)) + "\n")
- for l in lap:
- #txt.write(l.name + " = " + str(l.LocX) + "," + str(l.LocY) + "," + str(l.LocZ) +"\n")
- txt.write(l.name + " = " + str(l.LocY) + "," + str(l.LocZ) + "," + str(l.LocX) +"\n")
- pos = filter(lambda ob: ob.name.startswith("start position "), bpy.data.scenes.active.objects)
- for p in pos:
- id = p.name.rsplit(' ', 1)[1]
- q = p.mat.toQuat()
- print str(q)
- #txt.write(p.name + " = " + str(p.LocX) + "," + str(p.LocY) + "," + str(p.LocZ) + "\n")
- txt.write(p.name + " = " + str(p.LocY) + "," + str(p.LocZ) + "," + str(p.LocX) + "\n")
- txt.write("start orientation-xyz " + id + " = " + str(q[2]) + "," + str(q[0]) + "," + str(q[1]) + "\n")
- txt.write("start orientation-w " + id + " = " + str(q[3]) + "\n")
- txt.close()
- Window.FileSelector(export_tracks, "Export TRK")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement