~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/addons/io_import_scene_unreal_psk.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#
17
17
# ##### END GPL LICENSE BLOCK #####
18
18
 
19
 
bl_addon_info = {
20
 
    "name": "Import Unreal Skeleton Mesh(.psk)",
 
19
bl_info = {
 
20
    "name": "Import Unreal Skeleton Mesh (.psk)",
21
21
    "author": "Darknet",
22
 
    "version": (2,0),
23
 
    "blender": (2, 5, 3),
24
 
    "api": 31847,
25
 
    "location": "File > Import ",
26
 
    "description": "Import Unreal Engine (.psk)",
 
22
    "version": (2, 1),
 
23
    "blender": (2, 6, 3),
 
24
    "location": "File > Import > Skeleton Mesh (.psk)",
 
25
    "description": "Import Skeleleton Mesh",
27
26
    "warning": "",
28
27
    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"
29
 
        "Scripts/File_I-O/Unreal_psk_psa",
 
28
        "Scripts/Import-Export/Unreal_psk_psa",
30
29
    "tracker_url": "https://projects.blender.org/tracker/index.php?"\
31
 
        "func=detail&aid=21366&group_id=153&atid=469",
32
 
    "category": "Import/Export"}
 
30
        "func=detail&aid=21366",
 
31
    "category": "Import-Export"}
33
32
 
34
33
"""
35
34
Version': '2.0' ported by Darknet
36
35
 
37
36
Unreal Tournament PSK file to Blender mesh converter V1.0
38
 
Author: D.M. Sturgeon (camg188 at the elYsium forum), ported by Darknet
 
37
Author: 
 
38
-Darknet (Redesign and reworked)
 
39
-D.M. Sturgeon (camg188 at the elYsium forum)
 
40
-#2011-01-20 MARIUSZ SZKARADEK GLOGOW POLAND 
 
41
 
39
42
Imports a *psk file to a new mesh
40
43
 
41
44
-No UV Texutre
42
45
-No Weight
43
46
-No Armature Bones
44
47
-No Material ID
45
 
-Export Text Log From Current Location File (Bool )
46
48
"""
47
49
 
48
50
import bpy
 
51
import bmesh
49
52
import mathutils
50
 
import os
51
 
import sys
52
 
import string
53
53
import math
54
 
import re
55
54
from string import *
56
55
from struct import *
 
56
import struct
57
57
from math import *
58
 
 
59
 
#from bpy.props import *
60
 
 
61
 
import mathutils
 
58
from bpy.props import *
 
59
from bpy_extras.io_utils import unpack_list, unpack_face_list
 
60
 
 
61
Quaternion = mathutils.Quaternion
 
62
 
 
63
bpy.types.Scene.unrealbonesize = FloatProperty(
 
64
    name="Bone Length",
 
65
    description="Bone Length from head to tail distance",
 
66
    default=1,min=0.001,max=1000)
62
67
 
63
68
#output log in to txt file
64
69
DEBUGLOG = False
65
70
 
66
71
scale = 1.0
67
72
bonesize = 1.0
68
 
md5_bones=[]
69
 
 
70
 
def unpack_list(list_of_tuples):
71
 
    l = []
72
 
    for t in list_of_tuples:
73
 
        l.extend(t)
74
 
    return l
75
 
"""
76
 
class md5_bone:
77
 
    bone_index=0
78
 
    name=""
79
 
    bindpos=[]
80
 
    bindmat = mathutils.Quaternion()
81
 
    parent=""
82
 
    parent_index=0
83
 
    blenderbone=None
84
 
    roll=0
85
 
 
86
 
    def __init__(self):
87
 
        self.bone_index=0
88
 
        self.name=""
89
 
        self.bindpos=[0.0]*3
90
 
        self.bindmat=[None]*3  #is this how you initilize a 2d-array
91
 
        for i in range(3): self.bindmat[i] = [0.0]*3
92
 
        self.parent=""
93
 
        self.parent_index=0
94
 
        self.blenderbone=None
95
 
 
96
 
    def dump(self):
97
 
        print ("bone index: ", self.bone_index)
98
 
        print ("name: ", self.name)
99
 
        print ("bind position: ", self.bindpos)
100
 
        print ("bind translation matrix: ", self.bindmat)
101
 
        print ("parent: ", self.parent)
102
 
        print ("parent index: ", self.parent_index)
103
 
        print ("blenderbone: ", self.blenderbone)
104
 
"""
105
 
class md5_bone:
106
 
    bone_index=0
107
 
    name=""
108
 
    bindpos=[]
109
 
    bindmat=[]
110
 
    scale = []
111
 
    parent=""
112
 
    parent_index=0
113
 
    blenderbone=None
114
 
    roll=0
115
 
 
116
 
    def __init__(self):
117
 
        self.bone_index=0
118
 
        self.name=""
119
 
        self.bindpos=[0.0]*3
120
 
        self.scale=[0.0]*3
121
 
        self.bindmat=[None]*3  #is this how you initilize a 2d-array
122
 
        for i in range(3): self.bindmat[i] = [0.0]*3
123
 
        self.parent=""
124
 
        self.parent_index=0
125
 
        self.blenderbone=None
126
 
 
127
 
    def dump(self):
128
 
        print ("bone index: ", self.bone_index)
129
 
        print ("name: ", self.name)
130
 
        print ("bind position: ", self.bindpos)
131
 
        print ("bind translation matrix: ", self.bindmat)
132
 
        print ("parent: ", self.parent)
133
 
        print ("parent index: ", self.parent_index)
134
 
        print ("blenderbone: ", self.blenderbone)
135
 
        
 
73
skala = 1
 
74
flipyz = False   
 
75
flipuv = True
 
76
 
 
77
#pack read words        
 
78
def word(long): 
 
79
   s=''
 
80
   for j in range(0,long): 
 
81
       lit =  struct.unpack('c',plik.read(1))[0]
 
82
       #print(">",lit)
 
83
       #print(">",bytes.decode(lit))
 
84
       if ord(lit)!=0:
 
85
           #print(lit)
 
86
           s+=bytes.decode(lit)
 
87
           if len(s)>100:
 
88
               break
 
89
   return s
 
90
#pack read data   
 
91
def b(n):
 
92
    return struct.unpack(n*'b', plik.read(n))
 
93
def B(n):
 
94
    return struct.unpack(n*'B', plik.read(n))
 
95
def h(n):
 
96
    return struct.unpack(n*'h', plik.read(n*2))
 
97
def H(n):
 
98
    return struct.unpack(n*'H', plik.read(n*2))
 
99
def i(n):
 
100
    return struct.unpack(n*'i', plik.read(n*4))
 
101
def f(n):
 
102
    return struct.unpack(n*'f', plik.read(n*4))
 
103
 
 
104
#work in progress
 
105
#bmesh
 
106
#tess function prefix
 
107
def drawmesh():
 
108
    global DEBUGLOG, plik,vertexes,uvcoord,faceslist,num_faces,facemat,facesmooth,m
 
109
    global vertexes_ids,bonesdata,meshesdata,groups,num_materials,skala,flipyz,flipuv,mat_faceslist
 
110
    global vertices,faces
 
111
    print(faces[0])
 
112
    print("[CREATING MESH:]")
 
113
    me_ob = bpy.data.meshes.new('testmesh')
 
114
    #create mesh
 
115
    print("-Vertices count:",len(vertices))
 
116
    me_ob.vertices.add(len(vertices))
 
117
    print("-Faces count:",len(faces))
 
118
    me_ob.tessfaces.add(len(faces))
 
119
    print("-Creating vertices points...")
 
120
    me_ob.vertices.foreach_set("co", unpack_list(vertices))
 
121
    print("-Creating faces idx...")
 
122
    me_ob.tessfaces.foreach_set("vertices_raw",unpack_list( faces))
 
123
    for face in me_ob.tessfaces:
 
124
        print(dir(face))
 
125
        face.use_smooth = facesmooth[face.index]
 
126
        #face.material_index = facemat[face.index]#not yet working
 
127
    print("-Creating UV Texture...")
 
128
    me_ob.tessface_uv_textures.new('uvtexture')
 
129
    #uvtex = me_ob.tessface_uv_textures[0]
 
130
    for uv in me_ob.tessface_uv_textures:
 
131
        for face in me_ob.tessfaces:
 
132
            #uv.data[face.index].uv1.x = 
 
133
            
 
134
            #print(uv.data[face.index].uv1)
 
135
            #uv.data[face.index].uv1 = Vector(uvcoord[faces[face.index]][0],uvcoord[face.index][1])
 
136
            print(face.vertices_raw[0],face.vertices_raw[1],face.vertices_raw[2])
 
137
            uv.data[face.index].uv1 = mathutils.Vector((uvcoord[face.vertices_raw[0]][0],uvcoord[face.vertices_raw[0]][1]))
 
138
            uv.data[face.index].uv2 = mathutils.Vector((uvcoord[face.vertices_raw[1]][0],uvcoord[face.vertices_raw[1]][1]))
 
139
            uv.data[face.index].uv3 = mathutils.Vector((uvcoord[face.vertices_raw[2]][0],uvcoord[face.vertices_raw[2]][1]))
 
140
 
 
141
    ob = bpy.data.objects.new("TestObject",me_ob)
 
142
    #create vertex group
 
143
    for bone_id in range(len(bonesdata)):
 
144
        bonedata = bonesdata[str(bone_id)]
 
145
        namebone = bonedata[0]#.strip()[-25:]
 
146
        #print("NAME:",namebone)
 
147
        ob.vertex_groups.new(namebone)
 
148
    me_ob.update()
 
149
    bpy.context.scene.objects.link(ob) 
 
150
 
 
151
#Create Armature
 
152
def check_armature():
 
153
    global DEBUGLOG, plik,vertexes,uvcoord,faceslist,num_faces,facemat,facesmooth,m
 
154
    global vertexes_ids,bonesdata,meshesdata,groups,num_materials,skala,flipyz,flipuv,amt
 
155
    
 
156
    #create Armature
 
157
    bpy.ops.object.mode_set(mode='OBJECT')
 
158
    for i in bpy.context.scene.objects: i.select = False #deselect all objects
 
159
 
 
160
    bpy.ops.object.add(
 
161
     type='ARMATURE', 
 
162
     enter_editmode=True,
 
163
     location=(0,0,0))
 
164
    ob = bpy.context.object
 
165
    ob.show_x_ray = True
 
166
    ob.name = "ARM"
 
167
    amt = ob.data
 
168
    amt.name = 'Amt'
 
169
    amt.show_axes = True
 
170
    bpy.ops.object.mode_set(mode='EDIT')
 
171
    bpy.context.scene.update()
 
172
#Create bones
 
173
def make_bone():    
 
174
    global DEBUGLOG, plik,vertexes,uvcoord,faceslist,num_faces,facemat,facesmooth,m
 
175
    global vertexes_ids,bonesdata,meshesdata,groups,num_materials,skala,flipyz,flipuv,amt
 
176
    global bonenames
 
177
    bonenames = []
 
178
    
 
179
    print ('make bone')
 
180
    
 
181
    for bone_id in range(len(bonesdata)):
 
182
       bonedata = bonesdata[str(bone_id)]
 
183
       namebone = bonedata[0]#.strip()[-25:]
 
184
       newbone = amt.edit_bones.new(namebone)
 
185
       #those are need to show in the scene
 
186
       newbone.head = (0,0,0)
 
187
       newbone.tail = (0,0,1)
 
188
       bonenames.append(namebone)
 
189
    bpy.context.scene.update()
 
190
#Make bone parent
 
191
def make_bone_parent():    
 
192
    global DEBUGLOG, plik,vertexes,uvcoord,faceslist,num_faces,facemat,facesmooth,m
 
193
    global vertexes_ids,bonesdata,meshesdata,groups,num_materials,skala,flipyz,flipuv,amt    
 
194
    global children
 
195
    children = {}
 
196
    
 
197
    print ('make bone parent')
 
198
    for bone_id in range(len(bonesdata)):
 
199
        bonedata = bonesdata[str(bone_id)]
 
200
        namebone  = bonenames[bone_id]
 
201
        nameparent = bonenames[bonedata[1][2]]
 
202
        #print(nameparent)
 
203
        if nameparent != None:#make sure it has name
 
204
           parentbone = amt.edit_bones[nameparent]
 
205
           bonecurrnet = amt.edit_bones[namebone]
 
206
           bonecurrnet.parent = parentbone
 
207
    bpy.context.scene.update()
 
208
 
 
209
#make bone martix set    
 
210
def bones_matrix():    
 
211
    global DEBUGLOG, plik,vertexes,uvcoord,faceslist,num_faces,facemat,facesmooth,m
 
212
    global vertexes_ids,bonesdata,meshesdata,groups,num_materials,skala,flipyz,flipuv,amt
 
213
    
 
214
    for bone_id in range(len(bonesdata)):
 
215
        bonedata = bonesdata[str(bone_id)]
 
216
        namebone = bonedata[0]#.strip()[-25:]
 
217
        nameparent = bonenames[bonedata[1][2]]
 
218
        pos = bonedata[2][4:7] 
 
219
        rot = bonedata[2][0:4] 
 
220
        qx,qy,qz,qw = rot[0],rot[1],rot[2],rot[3]
 
221
        #print("Quaternion:",qx,qy,qz,qw)
 
222
        if bone_id==0:
 
223
            rot =  mathutils.Quaternion((qw,-qx,-qy,-qz))
 
224
        if bone_id!=0:
 
225
            rot =  mathutils.Quaternion((qw,qx,qy,qz))
 
226
        matrix = mathutils.Matrix()
 
227
        rot = rot.to_matrix().inverted()
 
228
        #print(rot)
 
229
        matrix[0][:3] = rot[0]
 
230
        matrix[1][:3] = rot[1]
 
231
        matrix[2][:3] = rot[2]
 
232
        matrix[3][:3] = pos
 
233
        if bone_id>0:
 
234
            bonedata.append(matrix*bonesdata[str(bonedata[1][2])][3])
 
235
        else:
 
236
            bonedata.append(matrix)
 
237
    bpy.context.scene.update()
 
238
 
 
239
#not working    
 
240
def make_bone_position():
 
241
    print ('make bone position')
 
242
    bpy.ops.object.mode_set(mode='EDIT')
 
243
    for bone_id in range(len(bonesdata)):
 
244
        bonedata = bonesdata[str(bone_id)]
 
245
        namebone = bonedata[0]#.strip()[-25:]
 
246
        bone = amt.edit_bones[namebone]
 
247
        bone.transform(bonedata[3], scale=True, roll=True)
 
248
        bvec = bone.tail- bone.head
 
249
        bvec.normalize()
 
250
        bone.tail = bone.head + 0.1 * bvec
 
251
        #bone.tail = bone.head + 1 * bvec
 
252
    bpy.context.scene.update()    
 
253
        
 
254
#not working  
 
255
def make_bone_position1():    
 
256
    print ('make bone position')
 
257
    bpy.ops.object.mode_set(mode='EDIT')
 
258
    for bone_id in range(len(bonesdata)):
 
259
        bonedata = bonesdata[str(bone_id)]
 
260
        namebone = bonedata[0]#.strip()[-25:]
 
261
        pos = bonedata[2][4:7] 
 
262
        pos1 = pos[0]*skala  
 
263
        pos2 = pos[1]*skala  
 
264
        pos3 = pos[2]*skala 
 
265
        if flipyz == False:
 
266
            pos = [pos1,pos2,pos3]
 
267
        if flipyz == True:
 
268
            pos = [pos1,-pos3,pos2]
 
269
        rot = bonedata[2][0:4] 
 
270
        qx,qy,qz,qw = rot[0],rot[1],rot[2],rot[3]
 
271
        if bone_id==0:
 
272
            rot = mathutils.Quaternion((qw,-qx,-qy,-qz))
 
273
        if bone_id!=0:
 
274
            rot = mathutils.Quaternion((qw,qx,qy,qz))
 
275
        #rot = rot.toMatrix().invert()  
 
276
        rot = rot.to_matrix().inverted()        
 
277
        bone = amt.edit_bones[namebone]
 
278
        #print("BONES:",amt.bones[0])
 
279
        
 
280
        if bone_id!=0:
 
281
            bone.head = bone.parent.head+mathutils.Vector(pos) * bone.parent.matrix
 
282
            #print(dir(rot))
 
283
            #print("rot:",rot)
 
284
            #print("matrix:",bone.parent.matrix)
 
285
            
 
286
            tempM = rot.to_4x4()*bone.parent.matrix
 
287
            #bone.matrix = tempM
 
288
            bone.transform(tempM, scale=False, roll=True)
 
289
            #bone.matrix_local = tempM
 
290
        else:
 
291
            bone.head = mathutils.Vector(pos)
 
292
            #bone.matrix = rot
 
293
            bone.transform(rot, scale=False, roll=True)
 
294
            #bone.matrix_local = rot
 
295
        bvec = bone.tail- bone.head
 
296
        bvec.normalize()
 
297
        bone.tail = bone.head + 0.1 * bvec
 
298
        #bone.tail = bone.head + 1 * bvec
 
299
    
 
300
 
136
301
#http://www.blender.org/forum/viewtopic.php?t=13340&sid=8b17d5de07b17960021bbd72cac0495f            
137
302
def fixRollZ(b):
138
303
    v = (b.tail-b.head)/b.length
145
310
    else:
146
311
        #align Z-axis
147
312
        b.roll -= math.degrees(math.atan2(v[0]*v[2]*(1 - v[1]),v[0]*v[0] + v[1]*v[2]*v[2])) 
148
 
        
149
 
def pskimport(infile):
150
 
    global DEBUGLOG
 
313
""" #did not port this yet unstable but work in some ways
 
314
def make_bone_position3():    
 
315
            for bone in md5_bones:
 
316
                #print(dir(bone))
 
317
                bpy.ops.object.mode_set(mode='EDIT')
 
318
                newbone = ob_new.data.edit_bones.new(bone.name)
 
319
                #parent the bone
 
320
                print("DRI:",dir(newbone))
 
321
                parentbone = None
 
322
                print("bone name:",bone.name)
 
323
                #note bone location is set in the real space or global not local
 
324
                bonesize = bpy.types.Scene.unrealbonesize
 
325
                if bone.name != bone.parent:
 
326
 
 
327
                    pos_x = bone.bindpos[0]
 
328
                    pos_y = bone.bindpos[1]
 
329
                    pos_z = bone.bindpos[2]
 
330
                    
 
331
                    #print( "LINKING:" , bone.parent ,"j")
 
332
                    parentbone = ob_new.data.edit_bones[bone.parent]
 
333
                    newbone.parent = parentbone
 
334
                    
 
335
                    rotmatrix = bone.bindmat.to_matrix().to_4x4().to_3x3()  # XXX, redundant matrix conversion?
 
336
                    newbone.transform(bone.bindmat.to_matrix().to_4x4(),True,True)
 
337
                    #parent_head = parentbone.matrix.to_quaternion().inverse() * parentbone.head
 
338
                    #parent_tail = parentbone.matrix.to_quaternion().inverse() * parentbone.tail
 
339
                    #location=Vector(pos_x,pos_y,pos_z)
 
340
                    #set_position = (parent_tail - parent_head) + location
 
341
                    #print("tmp head:",set_position)
 
342
 
 
343
                    #pos_x = set_position.x
 
344
                    #pos_y = set_position.y
 
345
                    #pos_z = set_position.z
 
346
                    
 
347
                 
 
348
                    newbone.head.x = parentbone.head.x + pos_x
 
349
                    newbone.head.y = parentbone.head.y + pos_y
 
350
                    newbone.head.z = parentbone.head.z + pos_z
 
351
                    #print("head:",newbone.head)
 
352
                    newbone.tail.x = parentbone.head.x + (pos_x + bonesize * rotmatrix[0][1])
 
353
                    newbone.tail.y = parentbone.head.y + (pos_y + bonesize * rotmatrix[1][1])
 
354
                    newbone.tail.z = parentbone.head.z + (pos_z + bonesize * rotmatrix[2][1])
 
355
                    #newbone.roll = fixRoll(newbone)
 
356
                else:
 
357
                    #print("rotmatrix:",dir(bone.bindmat.to_matrix().resize_4x4()))
 
358
                    #rotmatrix = bone.bindmat.to_matrix().resize_4x4().to_3x3()  # XXX, redundant matrix conversion?
 
359
                    rotmatrix = bone.bindmat.to_matrix().to_3x3()  # XXX, redundant matrix conversion?
 
360
                    #newbone.transform(bone.bindmat.to_matrix(),True,True)
 
361
                    newbone.head.x = bone.bindpos[0]
 
362
                    newbone.head.y = bone.bindpos[1]
 
363
                    newbone.head.z = bone.bindpos[2]
 
364
                    newbone.tail.x = bone.bindpos[0] + bonesize * rotmatrix[0][1]
 
365
                    newbone.tail.y = bone.bindpos[1] + bonesize * rotmatrix[1][1]
 
366
                    newbone.tail.z = bone.bindpos[2] + bonesize * rotmatrix[2][1]
 
367
                    #newbone.roll = fixRoll(newbone)
 
368
                    #print("no parent")    
 
369
""" 
 
370
    
 
371
 
 
372
#import psk file
 
373
def pskimport(filename,importmesh,importbone,bDebugLogPSK,importmultiuvtextures):
 
374
    global DEBUGLOG, plik,vertexes,uvcoord,faceslist,num_faces,facemat,facesmooth,m
 
375
    global vertexes_ids,bonesdata,meshesdata,groups,num_materials,skala,flipyz,flipuv,amt,vertices,faces
 
376
    points = []
 
377
    vertexes = []
 
378
    vertices = []
 
379
    uvcoord = []
 
380
    faceslist = []
 
381
    datafaces = []
 
382
    faces = []
 
383
    facemat = []
 
384
    facesmooth = []
 
385
    groups = []
 
386
    vertexes_ids = []
 
387
    bonesdata = {}
 
388
    meshesdata ={}
 
389
        
 
390
    DEBUGLOG = bDebugLogPSK
151
391
    print ("--------------------------------------------------")
152
392
    print ("---------SCRIPT EXECUTING PYTHON IMPORTER---------")
153
393
    print ("--------------------------------------------------")
154
 
    print ("Importing file: ", infile)
155
 
    
156
 
    md5_bones=[]
157
 
    pskfile = open(infile,'rb')
158
 
    if (DEBUGLOG):
159
 
        logpath = infile.replace(".psk", ".txt")
160
 
        print("logpath:",logpath)
161
 
        logf = open(logpath,'w')
162
 
        
163
 
    def printlog(strdata):
164
 
        if (DEBUGLOG):
165
 
            logf.write(strdata)
166
 
            
167
 
    objName = infile.split('\\')[-1].split('.')[0]
168
 
    
169
 
    me_ob = bpy.data.meshes.new(objName)
170
 
    print("objName:",objName)
171
 
    printlog(("New Mesh = " + me_ob.name + "\n"))
172
 
    #read general header
173
 
    indata = unpack('20s3i',pskfile.read(32))
174
 
    #not using the general header at this time
175
 
    #================================================================================================== 
176
 
    # vertex point
177
 
    #================================================================================================== 
178
 
    #read the PNTS0000 header
179
 
    indata = unpack('20s3i',pskfile.read(32))
180
 
    recCount = indata[3]
181
 
    printlog(( "Nbr of PNTS0000 records: " + str(recCount) + "\n"))
182
 
    counter = 0
183
 
    verts = []
184
 
    while counter < recCount:
185
 
        counter = counter + 1
186
 
        indata = unpack('3f',pskfile.read(12))
187
 
        #print(indata[0],indata[1],indata[2])
188
 
        verts.extend([(indata[0],indata[1],indata[2])])
189
 
        #Tmsh.vertices.append(NMesh.Vert(indata[0],indata[1],indata[2]))
190
 
        
191
 
    #================================================================================================== 
192
 
    # UV
193
 
    #================================================================================================== 
194
 
    #read the VTXW0000 header
195
 
    indata = unpack('20s3i',pskfile.read(32))
196
 
    recCount = indata[3]
197
 
    printlog( "Nbr of VTXW0000 records: " + str(recCount)+ "\n")
198
 
    counter = 0
199
 
    UVCoords = []
200
 
    #UVCoords record format = [index to PNTS, U coord, v coord]
201
 
    while counter < recCount:
202
 
        counter = counter + 1
203
 
        indata = unpack('hhffhh',pskfile.read(16))
204
 
        UVCoords.append([indata[0],indata[2],indata[3]])
205
 
        #print([indata[0],indata[2],indata[3]])
206
 
        #print([indata[1],indata[2],indata[3]])
207
 
        
208
 
    #================================================================================================== 
209
 
    # Face
210
 
    #================================================================================================== 
211
 
    #read the FACE0000 header
212
 
    indata = unpack('20s3i',pskfile.read(32))
213
 
    recCount = indata[3]
214
 
    printlog( "Nbr of FACE0000 records: "+ str(recCount) + "\n")
215
 
    #PSK FACE0000 fields: WdgIdx1|WdgIdx2|WdgIdx3|MatIdx|AuxMatIdx|SmthGrp
216
 
    #associate MatIdx to an image, associate SmthGrp to a material
217
 
    SGlist = []
218
 
    counter = 0
219
 
    faces = []
220
 
    faceuv = []
221
 
    while counter < recCount:
222
 
        counter = counter + 1
223
 
        indata = unpack('hhhbbi',pskfile.read(12))
224
 
        #the psk values are: nWdgIdx1|WdgIdx2|WdgIdx3|MatIdx|AuxMatIdx|SmthGrp
225
 
        #indata[0] = index of UVCoords
226
 
        #UVCoords[indata[0]]=[index to PNTS, U coord, v coord]
227
 
        #UVCoords[indata[0]][0] = index to PNTS
228
 
        PNTSA = UVCoords[indata[0]][0]
229
 
        PNTSB = UVCoords[indata[1]][0]
230
 
        PNTSC = UVCoords[indata[2]][0]
231
 
        #print(PNTSA,PNTSB,PNTSC) #face id vertex
232
 
        #faces.extend([0,1,2,0])
233
 
        faces.extend([PNTSA,PNTSB,PNTSC,0])
234
 
        uv = []
235
 
        u0 = UVCoords[indata[0]][1]
236
 
        v0 = UVCoords[indata[0]][2]
237
 
        uv.append([u0,v0])
238
 
        u1 = UVCoords[indata[1]][1]
239
 
        v1 = UVCoords[indata[1]][2]
240
 
        uv.append([u1,v1])
241
 
        u2 = UVCoords[indata[2]][1]
242
 
        v2 = UVCoords[indata[2]][2]
243
 
        uv.append([u2,v2])
244
 
        faceuv.append(uv)
245
 
        #print("UV: ",u0,v0)
246
 
        #update the uv var of the last item in the Tmsh.faces list
247
 
        # which is the face just added above
248
 
        ##Tmsh.faces[-1].uv = [(u0,v0),(u1,v1),(u2,v2)]
249
 
        #print("smooth:",indata[5])
250
 
        #collect a list of the smoothing groups
251
 
        if SGlist.count(indata[5]) == 0:
252
 
            SGlist.append(indata[5])
253
 
            print("smooth:",indata[5])
254
 
        #assign a material index to the face
255
 
        #Tmsh.faces[-1].materialIndex = SGlist.index(indata[5])
256
 
    printlog( "Using Materials to represent PSK Smoothing Groups...\n")
257
 
    #==========
258
 
    # skip something...
259
 
    #==========
260
 
    
261
 
    #================================================================================================== 
262
 
    # Material
263
 
    #================================================================================================== 
264
 
    ##
265
 
    #read the MATT0000 header
266
 
    indata = unpack('20s3i',pskfile.read(32))
267
 
    recCount = indata[3]
268
 
    printlog("Nbr of MATT0000 records: " +  str(recCount) + "\n" )
269
 
    printlog(" - Not importing any material data now. PSKs are texture wrapped! \n")
270
 
    counter = 0
271
 
    while counter < recCount:
272
 
        counter = counter + 1
273
 
        indata = unpack('64s6i',pskfile.read(88))
274
 
    ##
275
 
    
276
 
    #================================================================================================== 
277
 
    # Bones (Armature)
278
 
    #================================================================================================== 
279
 
    #read the REFSKEL0 header
280
 
    indata = unpack('20s3i',pskfile.read(32))
281
 
    recCount = indata[3]
282
 
    printlog( "Nbr of REFSKEL0 records: " + str(recCount) + "\n")
283
 
    Bns = []
284
 
    bone = []
285
 
    nobone = 0
286
 
    #================================================================================================== 
287
 
    # Bone Data 
288
 
    #==================================================================================================
289
 
    counter = 0
290
 
    print ("---PRASE--BONES---")
291
 
    while counter < recCount:
292
 
        indata = unpack('64s3i11f',pskfile.read(120))
293
 
        #print( "DATA",str(indata))
294
 
        bone.append(indata)
295
 
        
296
 
        createbone = md5_bone()
297
 
        #temp_name = indata[0][:30]
298
 
        temp_name = indata[0]
299
 
        
300
 
        temp_name = bytes.decode(temp_name)
301
 
        temp_name = temp_name.lstrip(" ")
302
 
        temp_name = temp_name.rstrip(" ")
303
 
        temp_name = temp_name.strip()
304
 
        temp_name = temp_name.strip( bytes.decode(b'\x00'))
305
 
        print ("temp_name:", temp_name, "||")
306
 
        createbone.name = temp_name
307
 
        createbone.bone_index = counter
308
 
        createbone.parent_index = indata[3]
309
 
        createbone.bindpos[0] = indata[8]
310
 
        createbone.bindpos[1] = indata[9]
311
 
        createbone.bindpos[2] = indata[10]
312
 
        createbone.scale[0] = indata[12]
313
 
        createbone.scale[1] = indata[13]
314
 
        createbone.scale[2] = indata[14]
315
 
        
316
 
        #w,x,y,z
317
 
        if (counter == 0):#main parent
318
 
            print("no parent bone")
319
 
            createbone.bindmat = mathutils.Quaternion((indata[7],indata[4],indata[5],indata[6]))
320
 
            #createbone.bindmat = mathutils.Quaternion((indata[7],-indata[4],-indata[5],-indata[6]))
321
 
        else:#parent
322
 
            print("parent bone")
323
 
            createbone.bindmat = mathutils.Quaternion((indata[7],-indata[4],-indata[5],-indata[6]))
324
 
            #createbone.bindmat = mathutils.Quaternion((indata[7],indata[4],indata[5],indata[6]))
325
 
            
326
 
        md5_bones.append(createbone)
327
 
        counter = counter + 1
328
 
        bnstr = (str(indata[0]))
329
 
        Bns.append(bnstr)
330
 
        
331
 
    for pbone in md5_bones:
332
 
        pbone.parent =  md5_bones[pbone.parent_index].name
333
 
            
334
 
    bonecount = 0
335
 
    for armbone in bone:
336
 
        temp_name = armbone[0][:30]
337
 
        #print ("BONE NAME: ",len(temp_name))
338
 
        temp_name=str((temp_name))
339
 
        #temp_name = temp_name[1]
340
 
        #print ("BONE NAME: ",temp_name)
341
 
        bonecount +=1
342
 
    print ("-------------------------")
343
 
    print ("----Creating--Armature---")
344
 
    print ("-------------------------")
345
 
    
346
 
    #================================================================================================
347
 
    #Check armature if exist if so create or update or remove all and addnew bone
348
 
    #================================================================================================
349
 
    #bpy.ops.object.mode_set(mode='OBJECT')
350
 
    meshname ="ArmObject"
351
 
    objectname = "armaturedata"
352
 
    bfound = False
353
 
    arm = None
354
 
    for obj in bpy.data.objects:
355
 
        if (obj.name == meshname):
356
 
            bfound = True
357
 
            arm = obj
358
 
            break
359
 
            
360
 
    if bfound == False:
361
 
        armdata = bpy.data.armatures.new(objectname)
362
 
        ob_new = bpy.data.objects.new(meshname, armdata)
363
 
        #ob_new = bpy.data.objects.new(meshname, 'ARMATURE')
364
 
        #ob_new.data = armdata
365
 
        bpy.context.scene.objects.link(ob_new)
366
 
        #bpy.ops.object.mode_set(mode='OBJECT')
367
 
        for i in bpy.context.scene.objects: i.select = False #deselect all objects
368
 
        ob_new.select = True
369
 
        #set current armature to edit the bone
370
 
        bpy.context.scene.objects.active = ob_new
371
 
        #set mode to able to edit the bone
372
 
        bpy.ops.object.mode_set(mode='EDIT')
373
 
        #newbone = ob_new.data.edit_bones.new('test')
374
 
        #newbone.tail.y = 1
375
 
        print("creating bone(s)")
376
 
        for bone in md5_bones:
377
 
            #print(dir(bone))
378
 
            newbone = ob_new.data.edit_bones.new(bone.name)
379
 
            #parent the bone
380
 
            parentbone = None
381
 
            print("bone name:",bone.name)
382
 
            #note bone location is set in the real space or global not local
383
 
            if bone.name != bone.parent:
384
 
            
385
 
                pos_x = bone.bindpos[0]
386
 
                pos_y = bone.bindpos[1]
387
 
                pos_z = bone.bindpos[2]
388
 
            
389
 
                #print( "LINKING:" , bone.parent ,"j")
390
 
                parentbone = ob_new.data.edit_bones[bone.parent]
391
 
                newbone.parent = parentbone
392
 
                rotmatrix = bone.bindmat.to_matrix().resize4x4().rotation_part()
393
 
                
394
 
                #parent_head = parentbone.head * parentbone.matrix.to_quat().inverse()
395
 
                #parent_tail = parentbone.tail * parentbone.matrix.to_quat().inverse()
396
 
                #location=Vector(pos_x,pos_y,pos_z)
397
 
                #set_position = (parent_tail - parent_head) + location
398
 
                #print("tmp head:",set_position)
399
 
                
400
 
                #pos_x = set_position.x
401
 
                #pos_y = set_position.y
402
 
                #pos_z = set_position.z
403
 
                
404
 
                newbone.head.x = parentbone.head.x + pos_x
405
 
                newbone.head.y = parentbone.head.y + pos_y
406
 
                newbone.head.z = parentbone.head.z + pos_z
407
 
                print("head:",newbone.head)
408
 
                newbone.tail.x = parentbone.head.x + (pos_x + bonesize * rotmatrix[1][0])
409
 
                newbone.tail.y = parentbone.head.y + (pos_y + bonesize * rotmatrix[1][1])
410
 
                newbone.tail.z = parentbone.head.z + (pos_z + bonesize * rotmatrix[1][2])
411
 
            else:
412
 
                rotmatrix = bone.bindmat.to_matrix().resize4x4().rotation_part()
413
 
                newbone.head.x = bone.bindpos[0]
414
 
                newbone.head.y = bone.bindpos[1]
415
 
                newbone.head.z = bone.bindpos[2]
416
 
                newbone.tail.x = bone.bindpos[0] + bonesize * rotmatrix[1][0]
417
 
                newbone.tail.y = bone.bindpos[1] + bonesize * rotmatrix[1][1]
418
 
                newbone.tail.z = bone.bindpos[2] + bonesize * rotmatrix[1][2]
419
 
                #print("no parent")
420
 
    
421
 
    bpy.context.scene.update()
422
 
    
423
 
    #==================================================================================================
424
 
    #END BONE DATA BUILD
425
 
    #==================================================================================================
426
 
    VtxCol = []
427
 
    for x in range(len(Bns)):
428
 
        #change the overall darkness of each material in a range between 0.1 and 0.9
429
 
        tmpVal = ((float(x)+1.0)/(len(Bns))*0.7)+0.1
430
 
        tmpVal = int(tmpVal * 256)
431
 
        tmpCol = [tmpVal,tmpVal,tmpVal,0]
432
 
        #Change the color of each material slightly
433
 
        if x % 3 == 0:
434
 
            if tmpCol[0] < 128: tmpCol[0] += 60
435
 
            else: tmpCol[0] -= 60
436
 
        if x % 3 == 1:
437
 
            if tmpCol[1] < 128: tmpCol[1] += 60
438
 
            else: tmpCol[1] -= 60
439
 
        if x % 3 == 2:
440
 
            if tmpCol[2] < 128: tmpCol[2] += 60
441
 
            else: tmpCol[2] -= 60
442
 
        #Add the material to the mesh
443
 
        VtxCol.append(tmpCol)
444
 
    
445
 
    #================================================================================================== 
446
 
    # Bone Weight
447
 
    #================================================================================================== 
448
 
    #read the RAWW0000 header
449
 
    indata = unpack('20s3i',pskfile.read(32))
450
 
    recCount = indata[3]
451
 
    printlog( "Nbr of RAWW0000 records: " + str(recCount) +"\n")
452
 
    #RAWW0000 fields: Weight|PntIdx|BoneIdx
453
 
    RWghts = []
454
 
    counter = 0
455
 
    while counter < recCount:
456
 
        counter = counter + 1
457
 
        indata = unpack('fii',pskfile.read(12))
458
 
        RWghts.append([indata[1],indata[2],indata[0]])
459
 
    #RWghts fields = PntIdx|BoneIdx|Weight
460
 
    RWghts.sort()
461
 
    printlog( "len(RWghts)=" + str(len(RWghts)) + "\n")
462
 
    #Tmsh.update()
463
 
    
464
 
    #set the Vertex Colors of the faces
465
 
    #face.v[n] = RWghts[0]
466
 
    #RWghts[1] = index of VtxCol
467
 
    """
468
 
    for x in range(len(Tmsh.faces)):
469
 
        for y in range(len(Tmsh.faces[x].v)):
470
 
            #find v in RWghts[n][0]
471
 
            findVal = Tmsh.faces[x].v[y].index
472
 
            n = 0
473
 
            while findVal != RWghts[n][0]:
474
 
                n = n + 1
475
 
            TmpCol = VtxCol[RWghts[n][1]]
476
 
            #check if a vertex has more than one influence
477
 
            if n != len(RWghts)-1:
478
 
                if RWghts[n][0] == RWghts[n+1][0]:
479
 
                    #if there is more than one influence, use the one with the greater influence
480
 
                    #for simplicity only 2 influences are checked, 2nd and 3rd influences are usually very small
481
 
                    if RWghts[n][2] < RWghts[n+1][2]:
482
 
                        TmpCol = VtxCol[RWghts[n+1][1]]
483
 
        Tmsh.faces[x].col.append(NMesh.Col(TmpCol[0],TmpCol[1],TmpCol[2],0))
484
 
    """
485
 
    if (DEBUGLOG):
486
 
        logf.close()
487
 
    #================================================================================================== 
488
 
    #Building Mesh
489
 
    #================================================================================================== 
490
 
    print("vertex:",len(verts),"faces:",len(faces))
491
 
    me_ob.vertices.add(len(verts))
492
 
    me_ob.faces.add(len(faces)//4)
493
 
 
494
 
    me_ob.vertices.foreach_set("co", unpack_list(verts))
495
 
    
496
 
    me_ob.faces.foreach_set("vertices_raw", faces)
497
 
    me_ob.faces.foreach_set("use_smooth", [False] * len(me_ob.faces))
498
 
    me_ob.update()
499
 
    
500
 
    #===================================================================================================
501
 
    #UV Setup
502
 
    #===================================================================================================
503
 
    texture = []
504
 
    texturename = "text1"
505
 
    #print(dir(bpy.data))
506
 
    if (len(faceuv) > 0):
507
 
        uvtex = me_ob.uv_textures.new() #add one uv texture
508
 
        for i, face in enumerate(me_ob.faces):
509
 
            blender_tface= uvtex.data[i] #face
510
 
            blender_tface.uv1 = faceuv[i][0] #uv = (0,0)
511
 
            blender_tface.uv2 = faceuv[i][1] #uv = (0,0)
512
 
            blender_tface.uv3 = faceuv[i][2] #uv = (0,0)
513
 
        texture.append(uvtex)
514
 
        
515
 
        #for tex in me_ob.uv_textures:
516
 
            #print("mesh tex:",dir(tex))
517
 
            #print((tex.name))
518
 
    
519
 
    #===================================================================================================
520
 
    #Material Setup
521
 
    #===================================================================================================
522
 
    materialname = "mat"
523
 
    materials = []
524
 
    
525
 
    matdata = bpy.data.materials.new(materialname)
526
 
    #color is 0 - 1 not in 0 - 255
527
 
    #matdata.mirror_color=(float(0.04),float(0.08),float(0.44))
528
 
    matdata.diffuse_color=(float(0.04),float(0.08),float(0.44))#blue color
529
 
    #print(dir(me_ob.uv_textures[0].data))
530
 
    texdata = None
531
 
    texdata = bpy.data.textures[len(bpy.data.textures)-1]
532
 
    if (texdata != None):
533
 
        #print(texdata.name)
534
 
        #print(dir(texdata))
535
 
        texdata.name = "texturelist1"
536
 
        matdata.active_texture = texdata
537
 
    materials.append(matdata)
538
 
    #matdata = bpy.data.materials.new(materialname)
539
 
    #materials.append(matdata)
540
 
    #= make sure the list isnt too big
541
 
    for material in materials:
542
 
        #add material to the mesh list of materials
543
 
        me_ob.materials.append(material)
544
 
    #===================================================================================================
545
 
    #
546
 
    #===================================================================================================
547
 
    obmesh = bpy.data.objects.new(objName,me_ob)
548
 
    #check if there is a material to set to
549
 
    if len(materials) > 0:
550
 
        obmesh.active_material = materials[0] #material setup tmp
551
 
    
552
 
    bpy.context.scene.objects.link(obmesh)
553
 
    
554
 
    bpy.context.scene.update()
555
 
    
556
 
    print ("PSK2Blender completed")
 
394
    print (" DEBUG Log:",bDebugLogPSK)
 
395
    print ("Importing file: ", filename)
 
396
    
 
397
    plik = open(filename,'rb')
 
398
    word(20),i(3)
 
399
    
 
400
    #------POINTS------
 
401
    print ('reading points')
 
402
    word(20)
 
403
    data = i(3)
 
404
    num_points = data[2]    
 
405
    for m in range(num_points):
 
406
        v=f(3)
 
407
        v1 =v[0]*skala
 
408
        v2 =v[1]*skala
 
409
        v3 =v[2]*skala
 
410
        if flipyz == False:
 
411
            points.append([v1,v2,v3])
 
412
        if flipyz == True:
 
413
            points.append([v1,-v3,v2])
 
414
        vertexes_ids.append([])
 
415
    
 
416
    #------VERTEXES----
 
417
    print ('reading vertexes')
 
418
    word(20)
 
419
    data = i(3)
 
420
    num_vertexes = data[2]
 
421
    for m in range(num_vertexes):
 
422
        data1 = H(2)
 
423
        vertexes.append(points[data1[0]])
 
424
        vertices.append(points[data1[0]])
 
425
        #vertices.extend(points[data1[0]])
 
426
        #vertices.extend([(points[data1[0]][0],points[data1[0]][1],points[data1[0]][2] )])
 
427
        #vertices.extend([(points[data1[0]][0],points[data1[0]][1],points[data1[0]][2] )])
 
428
        #print(points[data1[0]])
 
429
        if flipuv == False:
 
430
            uvcoord.append([f(1)[0],1-f(1)[0]])
 
431
        if flipuv == True:
 
432
            uvcoord.append([f(1)[0],f(1)[0]])
 
433
        vertexes_ids[data1[0]].append(m)
 
434
        b(2),h(1)
 
435
    
 
436
    
 
437
    #------FACES-------
 
438
    print ('reading faces')
 
439
    word(20)
 
440
    data = i(3)
 
441
    num_faces = data[2]
 
442
    for m in range(num_faces):
 
443
        v0=H(1)[0]
 
444
        v1=H(1)[0]
 
445
        v2=H(1)[0]
 
446
        faceslist.append([v1,v0,v2])
 
447
        faces.extend([(v1,v0,v2,0)])
 
448
        #faces.append([v1,v0,v2])
 
449
        #print((v1,v0,v2,0))
 
450
        mat_ids = B(2) 
 
451
        facemat.append(mat_ids)
 
452
        if str(mat_ids[0]) not in meshesdata:
 
453
            meshesdata[str(mat_ids[0])] = []          
 
454
        meshesdata[str(mat_ids[0])].append(m)
 
455
        facesmooth.append(i(1)[0])
 
456
        #datafaces.append([v1,v0,v2],mat_ids
 
457
    
 
458
    #------MATERIALS---
 
459
    print ('making materials')
 
460
    word(20)
 
461
    data = i(3)
 
462
    num_materials = data[2]
 
463
    for m in range(num_materials):
 
464
        name = word(64)
 
465
        print ('read materials from',name)
 
466
        matdata = bpy.data.materials.new(name)
 
467
        #try:
 
468
            #mat = Material.Get(namemodel+'-'+str(m))
 
469
            #mat = Material.Get(name)
 
470
        #except:
 
471
            #mat = Material.New(namemodel+'-'+str(m))
 
472
            #mat = Material.New(name)
 
473
        i(6)
 
474
        #make_materials(name,mat)
 
475
    
 
476
    #-------BONES------
 
477
    print ('reading bones')
 
478
    #check_armature()
 
479
    check_armature()
 
480
    word(20)
 
481
    data = i(3)
 
482
    num_bones = data[2]
 
483
    for m in range(num_bones):
 
484
        #print(str(m)) #index
 
485
        bonesdata[str(m)] = []
 
486
        bonename = word(64)
 
487
        bonename = bonename#.strip() 
 
488
        bonename = bonename.strip() 
 
489
        #print(bonename)#bone name
 
490
        bonesdata[str(m)].append(bonename)
 
491
        bonesdata[str(m)].append(i(3))
 
492
        bonesdata[str(m)].append(f(11))
 
493
    make_bone()
 
494
    make_bone_parent()
 
495
    bones_matrix()
 
496
    make_bone_position1()
 
497
 
 
498
    
 
499
     #-------SKINNING---
 
500
    print ('making skinning')
 
501
    word(20)
 
502
    data = i(3)
 
503
    num_groups = data[2]
 
504
    
 
505
    for m in range(num_groups):
 
506
        w = f(1)[0]
 
507
        v_id  = i(1)[0]         
 
508
        gr = i(1)[0]
 
509
        groups.append([w,v_id,gr]) 
 
510
    #create Mesh
 
511
    drawmesh()
 
512
 
 
513
    print ("IMPORTER PSK Blender 2.6 completed")
557
514
#End of def pskimport#########################
558
515
 
559
 
def getInputFilename(filename):
 
516
def psaimport(filename):
 
517
    global plik,bonesdata,animdata,anim_offset,animation_names
 
518
    bonesdata = {}
 
519
    animation_names = []
 
520
    animation_num_bones = []
 
521
    animation_num_keys = []
 
522
    animation_loc_keys = []
 
523
    animation_rot_keys = []
 
524
    animdata = {}
 
525
    plik = open(filename,'rb')
 
526
    print (word(20),i(3))
 
527
 
 
528
    #-------BONES------
 
529
    #check_armature_for_psa()
 
530
    print (word(20))
 
531
    data = i(3)
 
532
    #print data
 
533
    num_bones = data[2]
 
534
    for m in range(num_bones):
 
535
        bonesdata[str(m)] = []
 
536
        name = word(64)
 
537
        bonesdata[str(m)].append(name)
 
538
        bonesdata[str(m)].append(i(3))
 
539
        bonesdata[str(m)].append(f(11))
 
540
 
 
541
 
 
542
    #--------ANIMATIONS-INFO
 
543
    print (word(20))
 
544
    data = i(3)
 
545
    #print data
 
546
    for m in range(data[2]):
 
547
        name_animation = word(64)#name animation
 
548
        print("NAME:",name_animation)
 
549
        animation_names.append(name_animation)
 
550
        word(64)#name of owner of animation ?
 
551
        data = i(4)#num bones - 0 - 0 - num keys for all bones for this animation
 
552
        num_bones = data[0] 
 
553
        animation_num_bones.append(num_bones)
 
554
        f(3)
 
555
        data = i(3) 
 
556
        num_keys = data[2]
 
557
        animation_num_keys.append(num_keys)
 
558
    print (plik.tell())
 
559
 
 
560
    #--------ANIMATIONS-KEYS
 
561
    print (word(20))
 
562
    data = i(3)
 
563
    #print data
 
564
    anim_offset = {}
 
565
    seek = plik.tell()
 
566
    for m in range(len(animation_names)):
 
567
        anim_name = animation_names[m]
 
568
        anim_bones = animation_num_bones[m]
 
569
        anim_keys = animation_num_keys[m]
 
570
        anim_offset[anim_name] = []
 
571
        anim_offset[anim_name].append(seek)
 
572
        anim_offset[anim_name].append(anim_keys)
 
573
        anim_offset[anim_name].append(anim_bones)
 
574
        seek+=anim_keys*anim_bones*32
 
575
    
 
576
    
 
577
def getInputFilename(self,filename,importmesh,importbone,bDebugLogPSK,importmultiuvtextures):
560
578
    checktype = filename.split('\\')[-1].split('.')[1]
561
579
    print ("------------",filename)
562
 
    if checktype.upper() != 'PSK':
 
580
    if checktype.lower() != 'psk':
563
581
        print ("  Selected file = ",filename)
564
582
        raise (IOError, "The selected input file is not a *.psk file")
565
 
    pskimport(filename)
566
 
 
567
 
from bpy.props import *
568
 
 
 
583
        #self.report({'INFO'}, ("Selected file:"+ filename))
 
584
    else:
 
585
        pskimport(filename,importmesh,importbone,bDebugLogPSK,importmultiuvtextures)
 
586
#import panel
569
587
class IMPORT_OT_psk(bpy.types.Operator):
570
588
    '''Load a skeleton mesh psk File'''
571
589
    bl_idname = "import_scene.psk"
572
590
    bl_label = "Import PSK"
 
591
    bl_space_type = "PROPERTIES"
 
592
    bl_region_type = "WINDOW"
 
593
    bl_options = {'UNDO'}
573
594
 
574
595
    # List of operator properties, the attributes will be assigned
575
596
    # to the class instance from the operator settings before calling.
576
 
    filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen= 1024, default= "")
 
597
    filepath = StringProperty(
 
598
            subtype='FILE_PATH',
 
599
            )
 
600
    filter_glob = StringProperty(
 
601
            default="*.psk",
 
602
            options={'HIDDEN'},
 
603
            )
 
604
    importmesh = BoolProperty(
 
605
            name="Mesh",
 
606
            description="Import mesh only. (not yet build.)",
 
607
            default=True,
 
608
            )
 
609
    importbone = BoolProperty(
 
610
            name="Bones",
 
611
            description="Import bones only. Current not working yet",
 
612
            default=True,
 
613
            )
 
614
    importmultiuvtextures = BoolProperty(
 
615
            name="Single UV Texture(s)",
 
616
            description="Single or Multi uv textures",
 
617
            default=True,
 
618
            )
 
619
    bDebugLogPSK = BoolProperty(
 
620
            name="Debug Log.txt",
 
621
            description="Log the output of raw format. It will save in " \
 
622
                        "current file dir. Note this just for testing",
 
623
            default=False,
 
624
            )
 
625
    unrealbonesize = FloatProperty(
 
626
            name="Bone Length",
 
627
            description="Bone Length from head to tail distance",
 
628
            default=1,
 
629
            min=0.001,
 
630
            max=1000,
 
631
            )
577
632
 
578
633
    def execute(self, context):
579
 
        getInputFilename(self.filepath)
 
634
        bpy.types.Scene.unrealbonesize = self.unrealbonesize
 
635
        getInputFilename(self,self.filepath,self.importmesh,self.importbone,self.bDebugLogPSK,self.importmultiuvtextures)
580
636
        return {'FINISHED'}
581
637
 
582
638
    def invoke(self, context, event):
583
639
        wm = context.window_manager
584
 
        wm.add_fileselect(self)
 
640
        wm.fileselect_add(self)
585
641
        return {'RUNNING_MODAL'}  
586
 
 
 
642
#import panel psk       
 
643
class OBJECT_OT_PSKPath(bpy.types.Operator):
 
644
    bl_idname = "object.pskpath"
 
645
    bl_label = "PSK Path"
 
646
    __doc__ = ""
 
647
 
 
648
    filepath = StringProperty(
 
649
            subtype='FILE_PATH',
 
650
            )
 
651
    filter_glob = StringProperty(
 
652
            default="*.psk",
 
653
            options={'HIDDEN'},
 
654
            )
 
655
    importmesh = BoolProperty(
 
656
            name="Mesh",
 
657
            description="Import mesh only. (not yet build.)",
 
658
            default=True,
 
659
            )
 
660
    importbone = BoolProperty(
 
661
            name="Bones",
 
662
            description="Import bones only. Current not working yet",
 
663
            default=True,
 
664
            )
 
665
    importmultiuvtextures = BoolProperty(
 
666
            name="Single UV Texture(s)",
 
667
            description="Single or Multi uv textures",
 
668
            default=True,
 
669
            )
 
670
    bDebugLogPSK = BoolProperty(
 
671
            name="Debug Log.txt",
 
672
            description="Log the output of raw format. It will save in " \
 
673
                        "current file dir. Note this just for testing",
 
674
            default=False,
 
675
            )
 
676
    unrealbonesize = FloatProperty(
 
677
            name="Bone Length",
 
678
            description="Bone Length from head to tail distance",
 
679
            default=1,
 
680
            min=0.001,
 
681
            max=1000,
 
682
            )
 
683
    
 
684
    def execute(self, context):
 
685
        #context.scene.importpskpath = self.properties.filepath
 
686
        bpy.types.Scene.unrealbonesize = self.unrealbonesize
 
687
        getInputFilename(self,self.filepath,self.importmesh,self.importbone,self.bDebugLogPSK,self.importmultiuvtextures)
 
688
        return {'FINISHED'}
 
689
        
 
690
    def invoke(self, context, event):
 
691
        #bpy.context.window_manager.fileselect_add(self)
 
692
        wm = context.window_manager
 
693
        wm.fileselect_add(self)
 
694
        return {'RUNNING_MODAL'}
 
695
#import panel psa               
 
696
class OBJECT_OT_PSAPath(bpy.types.Operator):
 
697
    bl_idname = "object.psapath"
 
698
    bl_label = "PSA Path"
 
699
    __doc__ = ""
 
700
 
 
701
    filepath = StringProperty(name="PSA File Path", description="Filepath used for importing the PSA file", maxlen= 1024, default= "")
 
702
    filter_glob = StringProperty(
 
703
            default="*.psa",
 
704
            options={'HIDDEN'},
 
705
            )
 
706
    def execute(self, context):
 
707
        #context.scene.importpsapath = self.properties.filepath
 
708
        psaimport(self.filepath)
 
709
        return {'FINISHED'}
 
710
        
 
711
    def invoke(self, context, event):
 
712
        bpy.context.window_manager.fileselect_add(self)
 
713
        return {'RUNNING_MODAL'}
 
714
                
 
715
class OBJECT_OT_Path(bpy.types.Operator):
 
716
    bl_idname = "object.path"
 
717
    bl_label = "MESH BUILD TEST"
 
718
    __doc__ = ""
 
719
    # generic transform props
 
720
    view_align = BoolProperty(
 
721
            name="Align to View",
 
722
            default=False,
 
723
            )
 
724
    location = FloatVectorProperty(
 
725
            name="Location",
 
726
            subtype='TRANSLATION',
 
727
            )
 
728
    rotation = FloatVectorProperty(
 
729
            name="Rotation",
 
730
            subtype='EULER',
 
731
            )
 
732
    def execute(self, context):
 
733
        return {'FINISHED'}
 
734
        
 
735
    def invoke(self, context, event):
 
736
        me = bpy.data.meshes.new("test")
 
737
        obmade = bpy.data.objects.new("TestObject",me)
 
738
        print("Create Simple Mesh")
 
739
        bpy.data.scenes[0].objects.link(obmade)
 
740
        for i in bpy.context.scene.objects: i.select = False #deselect all objects
 
741
        obmade.select = True
 
742
        bpy.context.scene.objects.active = obmade
 
743
        
 
744
        verts = [(0,0,0),(2,0,0),(2,0,2)]
 
745
        edges = [(0,1),(1,2),(2,0)]
 
746
        faces = []
 
747
        faces.extend([(0,1,2,0)])
 
748
        #me.vertices.add(len(verts))
 
749
        #print(dir(me))
 
750
        me.vertices.add(len(verts))
 
751
        me.tessfaces.add(len(faces))
 
752
        for face in me.tessfaces:
 
753
            print(dir(face))
 
754
        
 
755
        me.vertices.foreach_set("co", unpack_list(verts))
 
756
        me.tessfaces.foreach_set("vertices_raw", unpack_list(faces))
 
757
        me.edges.add(len(edges))
 
758
        me.edges.foreach_set("vertices", unpack_list(edges))
 
759
        
 
760
        #print(len(me.tessfaces))
 
761
        me.tessface_uv_textures.new("uvtexture")
 
762
        #for uv in me.tessface_uv_textures:
 
763
            #print(len(uv.data))
 
764
            #print(dir(uv.data[0]))
 
765
            #print(dir(uv.data[0].uv1))
 
766
        return {'RUNNING_MODAL'}
 
767
 
 
768
#import menu panel tool bar
 
769
class VIEW3D_PT_unrealimport_objectmode(bpy.types.Panel):
 
770
    bl_space_type = "VIEW_3D"
 
771
    bl_region_type = "TOOLS"
 
772
    bl_label = "Import PSK/PSA"
 
773
    
 
774
    @classmethod
 
775
    def poll(cls, context):
 
776
        return context.active_object
 
777
 
 
778
    def draw(self, context):
 
779
        layout = self.layout
 
780
        rd = context.scene
 
781
 
 
782
        row2 = layout.row(align=True)
 
783
        row2.operator(OBJECT_OT_PSKPath.bl_idname)                
 
784
        row2.operator(OBJECT_OT_PSAPath.bl_idname)        
 
785
        row2.operator(OBJECT_OT_Path.bl_idname)        
 
786
#import panel
587
787
def menu_func(self, context):
588
788
    self.layout.operator(IMPORT_OT_psk.bl_idname, text="Skeleton Mesh (.psk)")
589
789
 
590
 
 
591
790
def register():
 
791
    bpy.utils.register_module(__name__)
592
792
    bpy.types.INFO_MT_file_import.append(menu_func)
593
793
    
594
794
def unregister():
 
795
    bpy.utils.unregister_module(__name__)
595
796
    bpy.types.INFO_MT_file_import.remove(menu_func)
596
797
 
597
798
if __name__ == "__main__":
600
801
#note this only read the data and will not be place in the scene    
601
802
#getInputFilename('C:\\blenderfiles\\BotA.psk') 
602
803
#getInputFilename('C:\\blenderfiles\\AA.PSK')
 
804
#getInputFilename('C:\\blender26x\\blender-2.psk')