296
|
1 #!BPY
|
|
2 """Registration info for Blender menus:
|
|
3 Name: 'Libps3 (from xml)'
|
|
4 Blender: 240
|
|
5 Group: 'Import'
|
|
6 Tooltip: 'Import to Blender for libps3'
|
|
7 """
|
|
8
|
|
9 ######################################################
|
|
10 # Importing modules
|
|
11 ######################################################
|
|
12 import sys
|
|
13 from xml.sax import ContentHandler
|
|
14 from xml.sax import make_parser
|
|
15 import math
|
|
16 import os
|
|
17 import Blender
|
|
18 import struct
|
|
19 import base64
|
|
20 import inspect
|
|
21 from Blender import *
|
|
22 from Blender import sys as bsys, Mathutils, Draw, BGL
|
|
23 from Blender.Mathutils import Vector
|
|
24 from Blender.sys import *
|
|
25 import bpy
|
|
26
|
|
27 class SimpleHandler(ContentHandler):
|
|
28
|
|
29 def __init__(self):
|
|
30 self.meList = []
|
|
31 self.objNames = []
|
|
32 self.objNum = 0
|
|
33 self.vi = 0
|
|
34 self.name = ""
|
|
35 self.coords = []
|
|
36
|
|
37 def startElement(self, name, attrs):
|
|
38 if name == 'surface':
|
|
39 self.mesh = mesh_obj()
|
|
40 mobj = self.mesh
|
|
41 mobj.objName = attrs['name']
|
|
42 mobj.objSize = int(attrs['size'])
|
|
43 mobj.objPrim = attrs['prim']
|
|
44 mobj.objParent = attrs['parent']
|
|
45 self.meList.append(mobj)
|
|
46 self.objNames.append(mobj.objName)
|
|
47 self.objNum += 1
|
|
48 self.vi = 0
|
|
49 elif name == 'coordinate':
|
|
50 self.name = name
|
|
51 elif name == 'image':
|
|
52 self.name = name
|
|
53 #_imgName = attrs['name']
|
|
54 #_imgFile = open(_imgName+".base", 'w')
|
|
55 #_imgData = ""
|
|
56 else:
|
|
57 self.name = ""
|
|
58
|
|
59 #def endElement(self, name):
|
|
60 # global _name
|
|
61
|
|
62 def characters(self, char):
|
|
63 if self.name == 'coordinate':
|
|
64 data = char.strip("\t\n\r")
|
|
65 data = data.split(" ")
|
|
66 sData = []
|
|
67 if len(data) == 3:
|
|
68 for i in range(3):
|
|
69 sData.append(float(data.pop(0))) # list [x, y, z] (type=float)
|
|
70 self.coords.append(sData)
|
|
71
|
|
72 if len(self.coords) == 3:
|
|
73 self.mesh.me.verts.extend(self.coords) # if three point prepared: add vertex ([[x,y,z],[x,y,z],[x,y,z]])
|
|
74 self.mesh.me.faces.extend([self.vi, self.vi+1, self.vi+2]) # make face between 3 vertex
|
|
75 self.vi += 3
|
|
76 self.coords = []
|
|
77
|
|
78 def create(handler):
|
|
79 sc = bpy.data.scenes.active # get scene
|
|
80 for i in range(handler.objNum):
|
|
81 sc.objects.new(handler.meList[i].me, handler.objNames[i]) # add mesh objects to scene
|
|
82 handler.meList[i].me.remDoubles(0.0) # remove double vertexs
|
|
83 Window.RedrawAll()
|
|
84
|
|
85 def file_open(fileName):
|
|
86 handler = SimpleHandler()
|
|
87 parser = make_parser()
|
|
88 f = open(fileName, 'rb')
|
|
89 parser.setContentHandler(handler)
|
|
90 parser.parse(f)
|
|
91 create(handler)
|
|
92
|
|
93 class mesh_obj:
|
|
94 def __init__(self):
|
|
95 self.me = Mesh.New('')
|
|
96 self.objName = ""
|
|
97 self.objSize = 0
|
|
98 self.objPrim = ""
|
|
99 self.objParent = ""
|
|
100
|
|
101
|
|
102 Window.FileSelector(file_open, "Choose xml file")
|