Last active
August 29, 2015 14:06
-
-
Save liyonghelpme/a96917f7c08ef0fbb1e1 to your computer and use it in GitHub Desktop.
增加导入骨骼功能 骨骼位置的旋转矩阵计算需要引入parent的旋转才可以正确计算位置
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!BPY | |
""" Registration info for Blender menus: | |
Name: 'OGRE (.mesh.xml)...' | |
Blender: 236 | |
Group: 'Import' | |
Tip: 'Import an Ogre-Mesh (.mesh.xml) file.' | |
""" | |
''' | |
__author__ = "Daniel Wickert" | |
__version__ = "0.4 05/11/05" | |
__bpydoc__ = """\ | |
This script imports Ogre-Mesh models into Blender. | |
Supported:<br> | |
* multiple submeshes (triangle list) | |
* uvs | |
* materials (textures only) | |
* vertex colours | |
Missing:<br> | |
* submeshes provided as triangle strips and triangle fans | |
* materials (diffuse, ambient, specular, alpha mode, etc.) | |
* skeletons | |
* animations | |
Known issues:<br> | |
* blender only supports a single uv set, always the first is taken | |
and only the first texture unit in a material, even if it is not for | |
the first uv set. | |
* code is a bit hacky in parts. | |
""" | |
''' | |
# Copyright (c) 2005 Daniel Wickert | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a | |
# copy of this software and associated documentation files (the "Software"), | |
# to deal in the Software without restriction, including without limitation | |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
# and/or sell copies of the Software, and to permit persons to whom the | |
# Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
# IN THE SOFTWARE. | |
# HISTORY: | |
# 0.1 04/02/05 | |
# ------------ | |
# * initial implementation. single submesh as triangle list, no uvs | |
# | |
# 0.2 04/03/05 | |
# ------------ | |
# * uv support added | |
# * texture loading added | |
# | |
# 0.3 04/09/05 | |
# ------------ | |
# * multiple submeshes added | |
# * scaling added | |
# * material parsing (only textures yet) | |
# * mesh -> mesh.xml conversion if IMPORT_OGREXMLCONVERTER defined | |
# * vertex colours | |
# | |
# 0.3.1 04/11/05 | |
# -------------- | |
# * stdout output streamlined | |
# * missing materials and textures are now handled gracefully | |
# | |
# 0.3.1a 04/11/05 | |
# -------------- | |
# * Mesh is assigned to a correctly named object and added | |
# to the current scene | |
# | |
# 0.4. 05/11/05 | |
# -------------- | |
# * shared vertices support | |
# * multiple texture coordinates in meshes are handled correctly no, | |
# but only the first coordinate set is used, since Blender doesn't | |
# allow multiple uvs. | |
# * only the first texture_unit's texture is used now. | |
# | |
# 0.4.1 06/02/05 | |
# -------------- | |
# * fixed bug: pathes invalid under linux | |
# * fixed bug: shared vertices were duplicated with each submesh | |
# | |
# 0.5.0 06/06/05 | |
# -------------- | |
# * consistent logging scheme with adjustable log level | |
# * render materials | |
# | |
# 0.5.1 13/07/05 | |
# -------------- | |
# * fixed bug: meshes without normals and texture coords cannot be imported | |
# | |
# 0.5.2 12/02/06 | |
# -------------- | |
# * fixed bug: multiple blender materials created from one ogre material | |
# | |
# TODO: implement a consistent naming scheme: | |
# bxxx for blender classes; oxxx for ogre(well, sorta) classes | |
bl_info = { | |
"name": "ogre mesh import", | |
"author": "Bartek Skorupa, Greg Zaal", | |
"version": (0, 1, 6), | |
"blender": (2, 70, 0), | |
"location": "File > Import > ogre (.mesh)", | |
"description": "import ogre mesh file", | |
"warning": "", | |
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/" | |
"Scripts/Nodes/Nodes_Efficiency_Tools", | |
"category": "Import-Export", | |
} | |
__version__ = "1.2.3" | |
import logging | |
handle = logging.FileHandler("/Users/liyong/blender.log") | |
logger = logging.getLogger() | |
logger.addHandler(handle) | |
logger.setLevel(logging.INFO) | |
import bpy | |
import glob | |
import os | |
import re | |
import xml.sax | |
import xml.sax.handler | |
import bmesh | |
import mathutils | |
from mathutils import Vector | |
from mathutils import Matrix | |
# determines the verbosity of loggin. | |
# 0 - no logging (fatal errors are still printed) | |
# 1 - standard logging | |
# 2 - verbose logging | |
# 3 - debug level. really boring (stuff like vertex data and verbatim lines) | |
IMPORT_LOG_LEVEL = 1 | |
IMPORT_SCALE_FACTOR = 1 | |
#IMPORT_OGREXMLCONVERTER = "d:\stuff\Torchlight_modding\orge_tools\OgreXmlConverter.exe" | |
IMPORT_OGREXMLCONVERTER = "/Users/liyong/Downloads/OgreCommandLineToolsMac_1.8.0/OgreXMLConverter" | |
#IMPORT_OGREXMLCONVERTER = "F:\\Projekte\\rastullah\checkout\\rl\\branches\python\dependencies\ogre\Tools\Common\\bin\\release\\OgreXmlConverter.exe" | |
def log(msg): | |
logger.info(msg) | |
#if IMPORT_LOG_LEVEL >= 1: print(msg) | |
def vlog(msg): | |
if IMPORT_LOG_LEVEL >= 2: print(msg) | |
def dlog(msg): | |
if IMPORT_LOG_LEVEL >= 3: print(msg) | |
class Mesh: | |
def __init__(self): | |
self.submeshes = [] | |
self.vertices = [] | |
self.vertexcolours = [] | |
self.normals = [] | |
self.uvs = [] | |
class Submesh: | |
def __init__(self): | |
self.vertices = [] | |
self.vertexcolours = [] | |
self.normals = [] | |
self.faces = [] | |
self.uvs = [] | |
self.indextype = "" | |
self.materialname = "" | |
self.sharedvertices = 0 | |
self.vertexGroup = {} | |
class Material: | |
def __init__(self, name): | |
self.name = name | |
self.texname = "" | |
self.diffuse = (1.0, 1.0, 1.0, 1.0) | |
self.ambient = (1.0, 1.0, 1.0, 1.0) | |
self.specular = (0.0, 0.0, 0.0, 0.0) | |
self.blenderimage = 0 | |
self.loading_failed = 0 | |
def getTexture(self): | |
if self.blenderimage == 0 and not self.loading_failed: | |
try: | |
#f = file(self.texname, 'r') | |
#f.close() | |
#self.blenderimage = Blender.Image.Load(self.texname) | |
if not os.path.exists(self.texname): | |
self.texname = self.texname.replace('.png', '.dds') | |
self.blenderimage = bpy.data.images.load(self.texname) | |
except IOError as err: | |
errmsg = "Couldn't open %s #%s" \ | |
% (self.texname, err) | |
log(errmsg) | |
self.loading_failed = 1; | |
return self.blenderimage | |
class OgreMeshSaxHandler(xml.sax.handler.ContentHandler): | |
global IMPORT_SCALE_FACTOR | |
def __init__(self): | |
self.mesh = 0 | |
self.submesh = 0 | |
self.ignore_input = 0 | |
self.load_next_texture_coords = 0 | |
self.skeleton = 0 | |
#self.boneassignments = 0 | |
def startDocument(self): | |
self.mesh = Mesh() | |
def startElement(self, name, attrs): | |
if name == 'boneassignments': | |
#self.boneassignments = {} | |
pass | |
if name == 'vertexboneassignment': | |
vind = int(attrs.get("vertexindex", "")) | |
bind = int(attrs.get("boneindex", "")) | |
weight = float(attrs.get("weight", "")) | |
if bind not in self.submesh.vertexGroup: | |
self.submesh.vertexGroup[bind] = [] | |
self.submesh.vertexGroup[bind].append({"vertexIndex":vind, | |
"boneIndex" :bind, "weight":weight }) | |
if name == 'skeletonlink': | |
self.skeleton = attrs.get("name", '') | |
if name == 'sharedgeometry': | |
self.submesh = self.mesh | |
if name == 'submesh': | |
self.submesh = Submesh() | |
self.submesh.materialname = attrs.get('material', "") | |
self.submesh.indextype = attrs.get('operationtype', "") | |
if attrs.get('usesharedvertices') == 'true': | |
self.submesh.sharedvertices = 1 | |
if name == 'vertex': | |
self.load_next_texture_coords = 1 | |
if name == 'face' and self.submesh: | |
face = ( | |
int(attrs.get('v1',"")), | |
int(attrs.get('v2',"")), | |
int(attrs.get('v3',"")) | |
) | |
self.submesh.faces.append(face) | |
if name == 'position': | |
vertex = ( | |
#Ogre x/y/z --> Blender x/-z/y | |
float(attrs.get('x', "")) * IMPORT_SCALE_FACTOR, | |
-float(attrs.get('z', "")) * IMPORT_SCALE_FACTOR, | |
float(attrs.get('y', "")) * IMPORT_SCALE_FACTOR | |
) | |
self.submesh.vertices.append(vertex) | |
if name == 'normal': | |
normal = ( | |
#Ogre x/y/z --> Blender x/-z/y | |
float(attrs.get('x', "")), | |
-float(attrs.get('z', "")), | |
float(attrs.get('y', "")) | |
) | |
self.submesh.normals.append(normal) | |
if name == 'texcoord' and self.load_next_texture_coords: | |
uv = ( | |
float(attrs.get('u', "")), | |
# flip vertical value, Blender's 0/0 is lower left | |
# whereas Ogre's 0/0 is upper left | |
1.0 - float(attrs.get('v', "")) | |
) | |
self.submesh.uvs.append(uv) | |
self.load_next_texture_coords = 0 | |
if name == 'colour_diffuse': | |
self.submesh.vertexcolours.append(attrs.get('value', "").split()) | |
def endElement(self, name): | |
if name == 'submesh': | |
self.mesh.submeshes.append(self.submesh) | |
self.submesh = 0 | |
def setMaterial(ob, mat): | |
me = ob.data | |
me.materials.append(mat) | |
from xml.dom import minidom | |
def OpenFile(filename): | |
if filename.find('.xml') == -1: | |
convert_meshfile(filename) | |
filename += '.xml' | |
xml_file = open(filename, 'r') | |
xml_doc = minidom.parse(xml_file) | |
xml_file.close() | |
return xml_doc | |
def CreateSkeleton(skeleton): | |
global dirname | |
if skeleton != 0: | |
#scene = bpy.context.scene | |
filename = os.path.join(dirname, skeleton) | |
xml_doc = OpenFile(filename) | |
CreateBindSkeleton(xml_doc) | |
#CreateRestPoseAction(xml_doc) | |
#OGREBoneIDsDic(xml_doc) | |
def OGREBonesDic(xmldoc): | |
OGRE_Bones = {} | |
OGRE_Bone_List = [] | |
OGRE_Bone_ID = {} | |
for bones in xmldoc.getElementsByTagName('bones'): | |
for bone in bones.childNodes: | |
OGRE_Bone = {} | |
if bone.localName == 'bone': | |
BoneName = str(bone.getAttributeNode('name').value) | |
BoneId = int(bone.getAttributeNode('id').value) | |
OGRE_Bone['name'] = BoneName | |
OGRE_Bone['id'] = BoneId | |
for b in bone.childNodes: | |
if b.localName == 'position': | |
x = float(b.getAttributeNode('x').value) | |
y = -float(b.getAttributeNode('z').value) | |
z = float(b.getAttributeNode('y').value) | |
OGRE_Bone['position'] = [x,y,z] | |
if b.localName == 'rotation': | |
angle = float(b.getAttributeNode('angle').value) | |
axis = b.childNodes[1] | |
axisx = float(axis.getAttributeNode('x').value) | |
axisy = float(axis.getAttributeNode('y').value) | |
axisz = float(axis.getAttributeNode('z').value) | |
#convert ogre coordinate to blender coordinate | |
OGRE_Bone['rotation'] = [axisx, -axisz, axisy] | |
OGRE_Bone['angle'] = angle | |
OGRE_Bones[BoneName] = OGRE_Bone | |
OGRE_Bone_List.append(OGRE_Bone) | |
OGRE_Bone_ID[OGRE_Bone["id"]] = OGRE_Bone | |
for bonehierarchy in xmldoc.getElementsByTagName('bonehierarchy'): | |
for boneparent in bonehierarchy.childNodes: | |
if boneparent.localName == 'boneparent': | |
Bone = str(boneparent.getAttributeNode('bone').value) | |
Parent = str(boneparent.getAttributeNode('parent').value) | |
OGRE_Bones[Bone]['parent'] = Parent | |
#return OGRE_Bone_List | |
return OGRE_Bones, OGRE_Bone_List, OGRE_Bone_ID | |
#骨骼的孩子节点 | |
def ChildList(BonesData): | |
for bone in BonesData.keys(): | |
childlist = [] | |
for key in BonesData.keys(): | |
if BonesData[key].get('parent'): | |
parent = BonesData[key]['parent'] | |
if parent == bone: | |
if key.find('tag') != -1: | |
childlist.append(BonesData[key]) | |
else: | |
childlist.insert(0, BonesData[key]) | |
BonesData[bone]['children'] = childlist | |
#获取所有骨骼的孩子列表缓存起来 | |
#没有孩子节点 或者 有超过1个的孩子节点 的骨骼增加一个Helper骨骼 设置父亲为bone | |
def HelperBones(BonesData): | |
ChildList(BonesData) | |
count = 0 | |
for bone in BonesData.keys(): | |
if (len(BonesData[bone]['children']) == 0) or (len(BonesData[bone]['children']) > 1): | |
HelperBone = {} | |
HelperBone['position'] = [0.2,0.0,0.0] | |
HelperBone['parent'] = bone | |
HelperBone['rotation'] = [1.0,0.0,0.0,0.0] | |
HelperBone['flag'] = 'helper' | |
BonesData['Helper'+str(count)] = HelperBone | |
count+=1 | |
#### 在 0 0 0 位置的骨骼 增加辅助的 0 骨骼到 parent上面 | |
def ZeroBones(BonesData): | |
for bone in BonesData.keys(): | |
pos = BonesData[bone]['position'] | |
if (math.sqrt(pos[0]**2+pos[1]**2+pos[2]**2)) == 0: | |
ZeroBone = {} | |
ZeroBone['position'] = [0.2,0.0,0.0] | |
ZeroBone['rotation'] = [1.0,0.0,0.0,0.0] | |
if (BonesData[bone].get('parent')): | |
ZeroBone['parent'] = BonesData[bone]['parent'] | |
ZeroBone['flag'] = 'zerobone' | |
BonesData['Zero'+bone] = ZeroBone | |
if (BonesData[bone].get('parent')): | |
BonesData[BonesData[bone]['parent']]['children'].append('Zero'+bone) | |
#计算骨骼长度 | |
def CalcBoneLength(vec): | |
return math.sqrt(vec[0]**2+vec[1]**2+vec[2]**2) | |
#计算每个joint的旋转矩阵 世界坐标中的旋转矩阵 | |
def CreateEmptys(BonesDic): | |
bpy.ops.mesh.primitive_cube_add() | |
modelCube = bpy.data.objects["Cube"] | |
for k in modelCube.data.vertices.values(): | |
k.co *= 0.1 | |
modelCube.location = (4, 0, 0) | |
for bone in BonesDic.keys(): | |
#bpy.ops.mesh.primitive_cube_add() | |
bpy.ops.object.select_all(action="DESELECT") | |
modelCube.select = True | |
bpy.ops.object.duplicate() | |
obj = bpy.data.objects["Cube.001"] | |
#obj = bpy.data.objects.new(bone, None) | |
obj.name = bone | |
#obj.scale = (0.1, 0.1, 0.1) | |
#bpy.context.scene.objects.link(obj) | |
log("create Cube: "+obj.name) | |
for bone in BonesDic.keys(): | |
if BonesDic[bone].get('parent'): | |
Parent = bpy.data.objects[BonesDic[bone].get('parent')] | |
obj = bpy.data.objects[bone] | |
obj.parent = Parent | |
for bone in BonesDic.keys(): | |
obj = bpy.data.objects[bone] | |
rot = BonesDic[bone]['rotation'] | |
angle = BonesDic[bone]['angle'] | |
loc = BonesDic[bone]['position'] | |
euler = mathutils.Matrix.Rotation(angle, 3, rot).to_euler() | |
obj.location = loc | |
obj.rotation_euler = euler | |
bpy.context.scene.update() | |
for bone in BonesDic.keys(): | |
obj = bpy.data.objects[bone] | |
#rotmatAS = obj.matrix_world.to_quaternion() | |
translation = obj.matrix_world.to_translation() | |
log("obj matrix update "+obj.name+" "+str(translation)) | |
#BonesDic[bone]['rotmatAS'] = rotmatAS.to_matrix() | |
BonesDic[bone]["translation"] = translation | |
for bone in BonesDic.keys(): | |
obj = bpy.data.objects[bone] | |
bpy.context.scene.objects.unlink(obj) | |
del obj | |
def SetRestMatrix(BonesDic): | |
#for key in BonesDic.keys(): | |
# bone = | |
pass | |
def SortData(bone, BonesData): | |
waitToVisit = [bone] | |
while len(waitToVisit) > 0: | |
b = waitToVisit.pop(0) | |
BonesData.append(b) | |
for c in b['children']: | |
waitToVisit.append(c) | |
def FindRoot(BonesList): | |
for c in BonesList: | |
if c['name'] == 'root': | |
return c | |
def addPos(a, b): | |
c = [0, 0, 0] | |
c[0] = a[0]+b[0] | |
c[1] = a[1]+b[1] | |
c[2] = a[2]+b[2] | |
return c | |
import mathutils | |
#当前选择对象的 pose属性 | |
def CreateActions(ActionsDic, armaturename, BonesDic): | |
armature = bpy.data.armatures[armaturename] | |
pose = bpy.context.object.pose | |
BoneID2Name = {} | |
#create blender armature submesh multiple mesh use shared vertex or not share | |
#convert ogre joint to bones | |
#建立绑定位置的 骨骼位置信息 pose mode下面可以调整微调 | |
def CreateBindSkeleton(xmldoc): | |
BonesDic, BonesList, BoneID = OGREBonesDic(xmldoc) | |
global BoneID2Name | |
BoneID2Name = BoneID | |
#BonesData = BonesDic | |
BonesData = [] | |
ChildList(BonesDic) | |
CreateEmptys(BonesDic) | |
#return | |
root = FindRoot(BonesList) | |
#root = BonesList[0] | |
if root == None: | |
log("not find root bone "+str(BonesList)) | |
return | |
#sort from root | |
SortData(root, BonesData) | |
if len(BonesData) != len(BonesList): | |
log("sort bone not equal "+ str(len(BonesList))) | |
log("data is "+str(len(BonesData))) | |
log("lost Number is "+str(len(BonesList) - len(BonesData))) | |
setA = set([k['name'] for k in BonesList]) | |
setB = set([k['name'] for k in BonesData]) | |
difSet = setA-setB | |
for k in difSet: | |
log('lost '+k) | |
log(BonesDic[k]) | |
return | |
#HelperBones(BonesDic) | |
#ZeroBones(BonesDic) | |
#CreateEmptys(BonesDic) | |
#SetBonesASPositions(BonesDic) | |
#BonesData = BonesDic | |
#scn = bpy.context.scene | |
#obj = bpy.data.objects.new("obj", objmesh) | |
#obj.location = bpy.context.scene.cursor_location | |
#bpy.context.scene.objects.link(obj) | |
arm = bpy.ops.object.add(type="ARMATURE", enter_editmode=True) | |
ob = bpy.context.object | |
ob.location = bpy.context.scene.cursor_location | |
ob.show_x_ray = True | |
amt = ob.data | |
amt.show_axes = True | |
#joint to bone root-> some bone | |
#root is an empty Bone joint first Bone is parent | |
bpy.ops.armature.select_all(action="DESELECT") | |
log("initial Bone") | |
for k in BonesData: | |
log("joint pos "+k['name']+" : "+str(k['translation'])) | |
#posit | |
#root joint self | |
if k.get('parent'): | |
#bpy.context.scene.active = bone | |
#root ---> Bip01 | |
#Bip01 ----> Pelvis | |
#root ---> tag_xxx | |
#parent joint rotation | |
parent = BonesDic[k['parent']] | |
#Bip01 ---> Pelvis | |
#extrude from parent bone tail | |
if parent.get('parent'): | |
parentBone = amt.edit_bones[k['parent']] | |
bpy.ops.armature.select_all(action="DESELECT") | |
parentBone.select_tail = True | |
bpy.ops.armature.extrude() | |
amt.edit_bones[-1].name = k['name'] | |
selBone = amt.edit_bones[-1] | |
#rot = Matrix.Rotation(parent['angle'], 3, parent['rotation']) | |
#rot = parent["rotmatAS"] | |
tp = k["translation"]-selBone.head | |
#tp = rot*Vector(k['position']) | |
bpy.ops.transform.translate(value=tp) | |
#selBone.matrix = rot | |
#new bone | |
#bone.head = parentBone.tail | |
#(trans, rotParent, scale) = parentBone.matrix.decompose() | |
#rot = Matrix.Rotation(parent['angle'], 3, parent['rotation']) | |
#bone.tail = Vector(k['position'])+parentBone.tail | |
#addPos(parentBone.tail, k['position']) | |
#bone.use_connect = False | |
#bone.parent = parentBone | |
#bpy.ops.transform.rotate(value=parent['angle'], axis=parent['rotation']) | |
#root--->Bip01 | |
else: | |
bone = amt.edit_bones.new(k['name']) | |
bone.head = parent["translation"] | |
#parent['position'] | |
#rot = Matrix.Rotation(parent['angle'], 3, parent['rotation']) | |
#rot = parent["rotmatAS"] | |
bone.tail = k["translation"] | |
#bone.tail = rot*Vector(k['position'])+bone.head | |
#bone.matrix = rot | |
#bone.tail = rot*Vector(k['position'])+bone.head | |
#bone.tail = addPos(bone.head, k['position']) | |
bone.use_connect = False | |
if parent.get('firstBone') != None: | |
bone.parent = amt.edit_bones[parent['firstBone']] | |
else: | |
parent['firstBone'] = bone.name | |
bpy.ops.armature.select_all(action="DESELECT") | |
''' | |
#first bone from joint root join parent is who? | |
if k['parent']['firstBone'] == None: | |
bone.head = k['parent']['position'] | |
bone.tail = addPos(k['parent']['position'], k['position']) | |
bone.use_connect = False | |
k['parent']['firstBone'] = bone | |
if k['parent'].get('parent'): | |
bone.parent = amt.edit_bones[k['parent']] | |
#other bone from joint | |
else: | |
bone.head = k['parent']['position'] | |
bone.tail = addPos(k['parent']['position'], k['position']) | |
''' | |
#parent = amt.edit_bones[k['parent']] | |
#bone.parent = parent | |
#bone.head = parent.tail | |
#bone.use_connect = False | |
#(trans, rot, scale) = parent.matrix.decompose() | |
#bone.tail = rot*Vector((0.3, 0, 0))+bone.head | |
else: | |
pass | |
#bone.head = k['position'] | |
#rot = mathutils.Matrix.Translation((0, 0, 0)) | |
#rot = Matrix.Rotation(k['rotation'][3], 3, k['rotation'][:3]) | |
#rot = Matrix.Rotation(k['rotation'][3], 3, k['rotation'][:3]) | |
#bone.head = k['position'] | |
log("create bone finish "+str(len(BonesList))) | |
#从每个submesh 里面抽取出vertexGroup的数据 | |
#建立一个 vertexGroup | |
#将对应定点放到group里面 | |
#group 按照数字名称来分组 | |
#def CreateVertexGroup(name, mesh): | |
#分析完一个子mesh的时候 子物体就添加一个顶点group | |
def CreateBlenderMesh(name, mesh, materials): | |
#bmesh = Blender.NMesh.GetRaw() | |
#bmesh = bpy.data.meshes.new("test") | |
# dict matname:blender material | |
bmaterials = {} | |
bmat_idx = -1 | |
log("Mesh with %d shared vertices." % len(mesh.vertices)) | |
vertex_count = len(mesh.vertices) | |
shareVert = mesh.vertices | |
shareNormal = mesh.normals | |
shareUV = mesh.uvs | |
submesh_count = len(mesh.submeshes) | |
vertex_offset = 0 | |
faces = [] | |
#bpy.context.active_object = None | |
bpy.ops.object.mode_set(mode="OBJECT") | |
bpy.ops.object.select_all(action="DESELECT") | |
for j in range(0, submesh_count): | |
submesh = mesh.submeshes[j] | |
vert = submesh.vertices | |
faces = submesh.faces | |
uvs = submesh.uvs | |
if submesh.sharedvertices == 1: | |
vert = shareVert | |
uvs = shareUV | |
objmesh = bpy.data.meshes.new("mesh"+str(j)) | |
obj = bpy.data.objects.new("obj"+str(j), objmesh) | |
obj.location = bpy.context.scene.cursor_location | |
bpy.context.scene.objects.link(obj) | |
objmesh.from_pydata(vert, [], faces) | |
objmesh.update(calc_edges=True) | |
obj.select = True | |
bpy.context.scene.objects.active = obj | |
bpy.ops.object.mode_set(mode="EDIT") | |
#if bpy.context.mode == 'EDIT_MESH': | |
#for f in bm.faces: | |
obj = bpy.context.active_object | |
me = obj.data | |
bm = bmesh.from_edit_mesh(me) | |
uv_layer = bm.loops.layers.uv.verify() | |
bm.faces.layers.tex.verify() | |
#设置loop 循环上面的每个定点的uv 坐标获得定点的编号来索引到对应的uv坐标 | |
for f in bm.faces: | |
for l in f.loops: | |
luv = l[uv_layer] | |
ind = l.vert.index | |
luv.uv = Vector(uvs[ind]) | |
bmesh.update_edit_mesh(me) | |
bpy.ops.object.mode_set(mode="OBJECT") | |
#检测骨骼编号 和 对应的骨骼名称 | |
log("skeleton ID") | |
allID = set(BoneID2Name.keys()) | |
useID = set(submesh.vertexGroup.keys()) | |
diffID = useID-allID | |
for k in diffID: | |
log("lostID "+str(k)) | |
log("all Lost ID "+str(len(allID))) | |
for k in allID: | |
log("allID "+str(k)+" "+str(BoneID2Name[k])) | |
if len(diffID) > 0: | |
return | |
#初始化顶点组 | |
#以骨骼快速分析骨骼名称得到实际骨骼名称 | |
#OGREBonesDic | |
#尝试将一个 joint 上面的定点权重/2 平分到相邻的两个骨骼上面即可 | |
#也就是一个joint vertexGroup 对应两个 bone的 vertexGroup | |
#分别是 tail 和 别的骨骼的head | |
#如果这个有多个child 则 平分权重到 多个child+parent上面即可 | |
for k in submesh.vertexGroup: | |
parentNode = BoneID2Name[k] | |
allGroups = [parentNode]+parentNode["children"] | |
num = float(len(allGroups)) | |
#blenderGroup = [] | |
#将定点 放到每一个 相邻骨骼的 组中 并且权重平分 | |
for g in allGroups: | |
if obj.vertex_groups.get(g["name"]) == None: | |
group = obj.vertex_groups.new(g["name"]) | |
else: | |
group = obj.vertex_groups[g["name"]] | |
for v in submesh.vertexGroup[k]: | |
group.add([v["vertexIndex"]], v["weight"]/num, 'ADD') | |
#blenderGroup.append(group) | |
#name = realName["name"] | |
#realName["parent"] | |
#骨骼是使用 tail joint命名的因此需要 调整为parent 的名称来为 定点group命名 | |
#group = obj.vertex_groups.new(name) | |
#for v in submesh.vertexGroup[k]: | |
# group.add([v["vertexIndex"]], v["weight"], 'REPLACE') | |
#bmesh.from_pydata(vert, [], faces) | |
#break | |
if materials.get(submesh.materialname): | |
omat = materials[submesh.materialname] | |
bmat = 0 | |
if not bmaterials.get(submesh.materialname): | |
bmat = create_blender_material(omat) | |
bmaterials[submesh.materialname] = bmat | |
else: | |
bmat = bmaterials[submesh.materialname] | |
setMaterial(obj, bmat) | |
#设置模型材质 | |
bpy.ops.object.join() | |
return None | |
def convert_meshfile(filename): | |
log("convert file "+filename) | |
if IMPORT_OGREXMLCONVERTER != '': | |
commandline = IMPORT_OGREXMLCONVERTER +' -log /Users/liyong/err.log '+ ' "' + filename + '"' + ' "'+ filename + '.xml' + '"' | |
log("executing %s" % commandline) | |
log(os.path.abspath('.')) | |
status = os.system(commandline) | |
log('status '+str(status)) | |
log("done.") | |
#set ambient diffuse specular emissive color | |
#def collect_materials(dirname): | |
# matname_pattern = re.compile('^\s*material\s+(.*?)\s*$') | |
def collect_materials(dirname): | |
# preparing some patterns | |
# to collect the material name | |
matname_pattern = re.compile('^\s*material\s+(.*?)\s*$') | |
# to collect the texture name | |
texname_pattern = re.compile('^\s*texture\s+(.*?)\s*$') | |
# to collect the diffuse colour | |
diffuse_alpha_pattern = re.compile(\ | |
'^\s*diffuse\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+).*$') | |
diffuse_pattern = re.compile(\ | |
'^\s*diffuse\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+).*$') | |
# to collect the specular colour | |
specular_pattern = re.compile(\ | |
'^\s*specular\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+).*$') | |
ambient_pattern = re.compile( | |
'^\s*ambient\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+).*$') | |
# the dictionary where to put the materials | |
materials = {} | |
# for all lines in all material files.. | |
material_files = glob.glob(dirname + '/*.material') | |
material = 0 | |
for filename in material_files: | |
f = open(filename, 'r') | |
line_number = 0 | |
for line in f: | |
try: | |
line_number = line_number + 1 | |
dlog("line to be matched: %s" % line) | |
#m = matname_pattern.match(line) | |
mat_name = matname_pattern.findall(line) | |
if len(mat_name) > 0: | |
material = Material(mat_name[0]) | |
materials[material.name] = material | |
vlog("parsing material %s" % mat_name[0]) | |
#m = texname_pattern.match(line) | |
tex_name = texname_pattern.findall(line) | |
# load only the first texture unit's texture | |
# TODO change to use the first one using the first uv set | |
if len(tex_name) > 0 and not material.texname: | |
#if m and not material.texname: | |
material.texname = dirname + '/' + tex_name[0] | |
m = diffuse_alpha_pattern.match(line) | |
if not m: | |
m = diffuse_pattern.match(line) | |
if m: | |
vlog(" parsing diffuse..") | |
groups = m.groups() | |
r = float(groups[0]) | |
g = float(groups[1]) | |
b = float(groups[2]) | |
#TODO: alpha still untested | |
if len(groups) > 3: | |
a = float(groups[3]) | |
else: | |
a = 1.0 | |
material.diffuse = (r, g, b, a) | |
vlog(" diffuse: %s" % str(material.diffuse)) | |
m = specular_pattern.match(line) | |
if m: | |
vlog(" parsing specular..") | |
groups = m.groups() | |
r = float(groups[0]) | |
g = float(groups[1]) | |
b = float(groups[2]) | |
material.specular = (r, g, b, 1.0) | |
vlog(" specular: %s" % str(material.specular)) | |
except Exception as e: | |
log(" error parsing material %s in %s on line % d: " % \ | |
(material.name, filename, line_number)) | |
log(" exception: %s" % str(e)) | |
return materials | |
def create_blender_material(omat): | |
mat = bpy.data.materials.new(omat.name) | |
mat.diffuse_color = omat.diffuse[:3] | |
mat.diffuse_shader = 'LAMBERT' | |
mat.diffuse_intensity = 1.0 | |
mat.specular_color = omat.specular[:3] | |
mat.specular_shader = 'COOKTORR' | |
mat.specular_intensity = 1.0 | |
mat.alpha = omat.diffuse[3] | |
mat.ambient = 1 | |
img = omat.getTexture() | |
if img: | |
cTex = bpy.data.textures.new(omat.texname, type="IMAGE") | |
cTex.image = img | |
mtex = mat.texture_slots.add() | |
mtex.texture = cTex | |
mtex.texture_coords = "UV" | |
mtex.use_map_color_diffuse = True | |
#mtex.use_map_color_emission = True | |
#mtex.emission_color_factor = 0.5 | |
#mtex.use_map_density = True | |
mtex.mapping = "FLAT" | |
return mat | |
''' | |
bmat = Blender.Material.New(omat.name) | |
bmat.rgbCol = (omat.diffuse[0], omat.diffuse[1], omat.diffuse[2]) | |
bmat.specCol = (omat.specular[0], omat.specular[1], omat.specular[2]) | |
bmat.alpha = omat.diffuse[3] | |
img = omat.getTexture() | |
if img: | |
tex = Blender.Texture.New(omat.texname) | |
tex.setType('Image') | |
tex.setImage(omat.getTexture()) | |
bmat.setTexture(0, tex, Blender.Texture.TexCo.UV,\ | |
Blender.Texture.MapTo.COL) | |
return bmat | |
''' | |
#return None | |
dirname = None | |
basename = None | |
def fileselection_callback(filename): | |
log("Reading mesh file %s..." % filename) | |
filename = os.path.expanduser(filename) | |
#if fileName: | |
# (shortName, ext) = os.path.splitext(filename) | |
# is this a mesh file instead of an xml file? | |
if (filename.lower().find('.xml') == -1): | |
# No. Use the xml converter to fix this | |
log("No mesh.xml file. Trying to convert it from binary mesh format.") | |
convert_meshfile(filename) | |
filename += '.xml' | |
global dirname | |
global basename | |
dirname = os.path.dirname(filename) | |
basename = os.path.basename(filename) | |
#dirname = sys.dirname(filename) | |
#basename = Blender.sys.basename(filename) | |
# parse material files and make up a dictionary: {mat_name:material, ..} | |
materials = collect_materials(dirname) | |
#create materials | |
# prepare the SAX parser and parse the file using our own content handler | |
parser = xml.sax.make_parser() | |
handler = OgreMeshSaxHandler() | |
parser.setContentHandler(handler) | |
parser.parse(open(filename)) | |
# create the mesh from the parsed data and link it to a fresh object | |
#scene = Blender.Scene.GetCurrent() | |
meshname = basename[0:basename.lower().find('.mesh.xml')] | |
amt = CreateSkeleton(handler.skeleton) | |
obj = CreateBlenderMesh(meshname, handler.mesh, materials) | |
#scene.link(object) | |
#object.select = True | |
log("import completed.") | |
#Blender.Redraw() | |
def clearScene(): | |
#global toggle | |
scn = bpy.context.scene | |
log("clearScene ") | |
for ob in scn.objects: | |
if ob.type in ["MESH", "CURVE", "TEXT"]: | |
scn.objects.active = ob | |
bpy.ops.object.mode_set(mode='OBJECT') | |
scn.objects.unlink(ob) | |
del ob | |
return scn | |
#Blender.Window.FileSelector(fileselection_callback, "Import OGRE", "*.xml") | |
from bpy.props import * | |
class IMPORT_OT_OGRE(bpy.types.Operator): | |
bl_idname = "import_scene.ogre_mesh" | |
bl_description = 'Import from mesh file format (.mesh)' | |
bl_label = "Import mesh" +' v.'+ __version__ | |
bl_space_type = "PROPERTIES" | |
bl_region_type = "WINDOW" | |
bl_options = {'UNDO'} | |
filepath = StringProperty( | |
subtype='FILE_PATH', | |
) | |
def draw(self, context): | |
layout0 = self.layout | |
#layout0.enabled = False | |
#col = layout0.column_flow(2,align=True) | |
layout = layout0.box() | |
col = layout.column() | |
#col.prop(self, 'KnotType') waits for more knottypes | |
#col.label(text="import Parameters") | |
#col.prop(self, 'replace') | |
#col.prop(self, 'new_scene') | |
#row = layout.row(align=True) | |
#row.prop(self, 'curves') | |
#row.prop(self, 'circleResolution') | |
#row = layout.row(align=True) | |
#row.prop(self, 'merge') | |
#if self.merge: | |
# row.prop(self, 'mergeLimit') | |
#row = layout.row(align=True) | |
#row.label('na') | |
#row.prop(self, 'draw_one') | |
#row.prop(self, 'thic_on') | |
#col = layout.column() | |
#col.prop(self, 'codec') | |
#row = layout.row(align=True) | |
#ow.prop(self, 'debug') | |
#if self.debug: | |
# row.prop(self, 'verbose') | |
def execute(self, context): | |
''' | |
global toggle, theMergeLimit, theCodec, theCircleRes | |
O_Merge = T_Merge if self.merge else 0 | |
#O_Replace = T_Replace if self.replace else 0 | |
O_NewScene = T_NewScene if self.new_scene else 0 | |
O_Curves = T_Curves if self.curves else 0 | |
O_ThicON = T_ThicON if self.thic_on else 0 | |
O_DrawOne = T_DrawOne if self.draw_one else 0 | |
O_Debug = T_Debug if self.debug else 0 | |
O_Verbose = T_Verbose if self.verbose else 0 | |
toggle = O_Merge | O_DrawOne | O_NewScene | O_Curves | O_ThicON | O_Debug | O_Verbose | |
theMergeLimit = self.mergeLimit*1e-4 | |
theCircleRes = self.circleResolution | |
theCodec = self.codec | |
''' | |
#bpy.ops.wm.read_homefile() | |
clearScene() | |
#readAndBuildDxfFile(self.filepath) | |
fileselection_callback(self.filepath) | |
return {'FINISHED'} | |
def invoke(self, context, event): | |
wm = context.window_manager | |
wm.fileselect_add(self) | |
return {'RUNNING_MODAL'} | |
def menu_func(self, context): | |
self.layout.operator(IMPORT_OT_OGRE.bl_idname, text="ogre (.mesh)") | |
def register(): | |
bpy.utils.register_module(__name__) | |
bpy.types.INFO_MT_file_import.append(menu_func) | |
def unregister(): | |
bpy.utils.unregister_module(__name__) | |
bpy.types.INFO_MT_file_import.remove(menu_func) | |
if __name__ == "__main__": | |
register() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment