annotate SceneGraph/BlenderScript/create_mesh.py @ 545:085c9284125a draft

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