Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert prints to operator messages for blender 2.5+ #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 51 additions & 35 deletions blender-2.56/iqm_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@

MAXVCACHE = 32

def IQMerror(operator, message):
operator.report({'ERROR'}, message)
return {'CANCELLED'}

def IQMdebug(operator, message):
operator.report({'DEBUG'}, message)
return {'CANCELLED'}

def IQMwarning(operator, message):
operator.report({'WARNING'}, message)
return None

def IQMinfo(operator, message):
operator.report({'INFO'}, message)
return None

class Vertex:
def __init__(self, index, coord, normal, uv, weights):
self.index = index
Expand Down Expand Up @@ -113,7 +129,8 @@ def __eq__(self, v):


class Mesh:
def __init__(self, name, material, verts):
def __init__(self, operator, name, material, verts):
self.operator = operator
self.name = name
self.material = material
self.verts = [ None for v in verts ]
Expand Down Expand Up @@ -211,10 +228,10 @@ def optimize(self):
besttri = i
bestscore = score

print('%s: %d verts optimized to %d/%d loads for %d entry LRU cache' % (self.name, len(self.verts), vertloads, len(vertschedule), MAXVCACHE))
#print('%s: %d verts scheduled to %d' % (self.name, len(self.verts), len(vertschedule)))
IQMdebug(self.operator, '%s: %d verts optimized to %d/%d loads for %d entry LRU cache' % (self.name, len(self.verts), vertloads, len(vertschedule), MAXVCACHE))
#IQMdebug(self.operator, '%s: %d verts scheduled to %d' % (self.name, len(self.verts), len(vertschedule)))
self.verts = vertschedule
# print('%s: %d tris scheduled to %d' % (self.name, len(self.tris), len(trischedule)))
# IQMdebug(self.operator, '%s: %d tris scheduled to %d' % (self.name, len(self.tris), len(trischedule)))
self.tris = trischedule

def calcNeighbors(self):
Expand Down Expand Up @@ -413,13 +430,14 @@ def boundsData(self, bones, meshes):
invbase.append(bone.matrix.copy().invert())
data = b''
for i, frame in enumerate(self.frames):
print('Calculating bounding box for %s:%d' % (self.name, i))
IQMdebug(self.operator, 'Calculating bounding box for %s:%d' % (self.name, i))
data += self.frameBoundsData(bones, meshes, frame, invbase)
return data


class IQMFile:
def __init__(self):
def __init__(self, operator):
self.operator = operator
self.textoffsets = {}
self.textdata = b''
self.meshes = []
Expand Down Expand Up @@ -479,7 +497,7 @@ def calcFrameSize(self):
for joint in self.joints:
if self.anims:
self.posedata.append(joint.poseData(self))
print('Exporting %d frames of size %d' % (self.numframes, self.framesize))
IQMdebug(self.operator, 'Exporting %d frames of size %d' % (self.numframes, self.framesize))

def writeVerts(self, file, offset):
if self.numverts <= 0:
Expand Down Expand Up @@ -626,7 +644,7 @@ def findArmature(context):
return armature


def derigifyBones(context, armature, scale):
def derigifyBones(operator, context, armature, scale):
data = armature.data

orgbones = {}
Expand Down Expand Up @@ -683,11 +701,11 @@ def derigifyBones(context, armature, scale):
bonematrix[3][2] *= scale
bones[bone.name] = Bone(bname, bone.name, index, bname in defparent and bones.get(defbones[defparent[bname]].name), bonematrix)
worklist.extend(defchildren[bname])
print('De-rigified %d bones' % len(worklist))
IQMdebug(operator, 'De-rigified %d bones' % len(worklist))
return bones


def collectBones(context, armature, scale):
def collectBones(operator, context, armature, scale):
data = armature.data
bones = {}
worldmatrix = armature.matrix_world
Expand All @@ -702,16 +720,16 @@ def collectBones(context, armature, scale):
for child in bone.children:
if child not in worklist:
worklist.append(child)
print('Collected %d bones' % len(worklist))
IQMdebug(operator, 'Collected %d bones' % len(worklist))
return bones


def collectAnim(context, armature, scale, bones, action, startframe = None, endframe = None):
def collectAnim(operator, context, armature, scale, bones, action, startframe = None, endframe = None):
if not startframe or not endframe:
startframe, endframe = action.frame_range
startframe = int(startframe)
endframe = int(endframe)
print('Exporting action "%s" frames %d-%d' % (action.name, startframe, endframe))
IQMdebug(operator, 'Exporting action "%s" frames %d-%d' % (action.name, startframe, endframe))
scene = context.scene
worldmatrix = armature.matrix_world
armature.animation_data.action = action
Expand Down Expand Up @@ -743,7 +761,7 @@ def collectAnim(context, armature, scale, bones, action, startframe = None, endf
return outdata


def collectAnims(context, armature, scale, bones, animspecs):
def collectAnims(operator, context, armature, scale, bones, animspecs):
actions = bpy.data.actions
animspecs = [ spec.strip() for spec in animspecs.split(',') ]
anims = []
Expand All @@ -754,7 +772,7 @@ def collectAnims(context, armature, scale, bones, animspecs):
animspec = [ arg.strip() for arg in animspec.split(':') ]
animname = animspec[0]
if animname not in actions:
print('Action "%s" not found in current armature' % animname)
IQMwarning(operator, 'Action "%s" not found in current armature' % animname)
continue
try:
startframe = int(animspec[1])
Expand All @@ -772,14 +790,14 @@ def collectAnims(context, armature, scale, bones, animspecs):
flags = int(animspec[4])
except:
flags = 0
framedata = collectAnim(context, armature, scale, bones, actions[animname], startframe, endframe)
framedata = collectAnim(operator, context, armature, scale, bones, actions[animname], startframe, endframe)
anims.append(Animation(animname, framedata, fps, flags))
armature.animation_data.action = oldaction
scene.frame_set(oldframe)
return anims


def collectMeshes(context, bones, scale, matfun, useskel = True, filetype = 'IQM'):
def collectMeshes(operator, context, bones, scale, matfun, useskel = True, filetype = 'IQM'):
vertwarn = []
objs = context.selected_objects #context.scene.objects
meshes = []
Expand Down Expand Up @@ -807,7 +825,7 @@ def collectMeshes(context, bones, scale, matfun, useskel = True, filetype = 'IQM
mesh = materials[obj.name, matindex, material]
except:
matprefix = (data.materials and data.materials[matindex].name) or ''
mesh = Mesh(obj.name, matfun(matprefix, material), data.vertices)
mesh = Mesh(operator, obj.name, matfun(matprefix, material), data.vertices)
meshes.append(mesh)
materials[obj.name, matindex, material] = mesh

Expand Down Expand Up @@ -839,7 +857,7 @@ def collectMeshes(context, bones, scale, matfun, useskel = True, filetype = 'IQM
except:
if (groups[g.group].name, mesh.name) not in vertwarn:
vertwarn.append((groups[g.group].name, mesh.name))
print('Vertex depends on non-existent bone: %s in mesh: %s' % (groups[g.group].name, mesh.name))
IQMwarning(operator, 'Vertex depends on non-existent bone: %s in mesh: %s' % (groups[g.group].name, mesh.name))

if not face.use_smooth:
vertindex = len(verts)
Expand Down Expand Up @@ -877,7 +895,7 @@ def collectMeshes(context, bones, scale, matfun, useskel = True, filetype = 'IQM
if filetype == 'IQM':
mesh.calcTangents()
mesh.calcNeighbors()
print('%s %s: generated %d triangles' % (mesh.name, mesh.material, len(mesh.tris)))
IQMdebug(operator, '%s %s: generated %d triangles' % (mesh.name, mesh.material, len(mesh.tris)))

return meshes

Expand Down Expand Up @@ -933,39 +951,37 @@ def exportIQE(file, meshes, bones, anims):
file.write('\n')


def exportIQM(context, filename, usemesh = True, useskel = True, usebbox = True, scale = 1.0, animspecs = None, matfun = (lambda prefix, image: image), derigify = False):
def exportIQM(operator, context, filename, usemesh = True, useskel = True, usebbox = True, scale = 1.0, animspecs = None, matfun = (lambda prefix, image: image), derigify = False):
armature = findArmature(context)
if useskel and not armature:
print('No armature selected')
return
return IQMerror('No armature selected')

if filename.lower().endswith('.iqm'):
filetype = 'IQM'
elif filename.lower().endswith('.iqe'):
filetype = 'IQE'
else:
print('Unknown file type: %s' % filename)
return
return IQMerror('Unknown file type: %s' % filename)

if useskel:
if derigify:
bones = derigifyBones(context, armature, scale)
bones = derigifyBones(operator, context, armature, scale)
else:
bones = collectBones(context, armature, scale)
bones = collectBones(operator, context, armature, scale)
else:
bones = {}
bonelist = sorted(bones.values(), key = lambda bone: bone.index)
if usemesh:
meshes = collectMeshes(context, bones, scale, matfun, useskel, filetype)
meshes = collectMeshes(operator, context, bones, scale, matfun, useskel, filetype)
else:
meshes = []
if useskel and animspecs:
anims = collectAnims(context, armature, scale, bonelist, animspecs)
anims = collectAnims(operator, context, armature, scale, bonelist, animspecs)
else:
anims = []

if filetype == 'IQM':
iqm = IQMFile()
iqm = IQMFile(operator)
iqm.addMeshes(meshes)
iqm.addJoints(bonelist)
iqm.addAnims(anims)
Expand All @@ -978,16 +994,16 @@ def exportIQM(context, filename, usemesh = True, useskel = True, usebbox = True,
else:
file = open(filename, 'w')
except:
print ('Failed writing to %s' % (filename))
return
return IQMerror(operator, 'Failed writing to %s' % (filename))
if filetype == 'IQM':
iqm.export(file, usebbox)
elif filetype == 'IQE':
exportIQE(file, meshes, bonelist, anims)
file.close()
print('Saved %s file to %s' % (filetype, filename))
IQMinfo(operator, 'Saved %s file to %s' % (filetype, filename))
else:
print('No %s file was generated' % (filetype))
return IQMerror(operator, 'No %s file was generated' % (filetype))
return {'FINISHED'}


class ExportIQM(bpy.types.Operator, ExportHelper):
Expand All @@ -1009,7 +1025,7 @@ def execute(self, context):
matfun = lambda prefix, image: prefix + os.path.splitext(image)[0]
else:
matfun = lambda prefix, image: image
exportIQM(context, self.properties.filepath, self.properties.usemesh, self.properties.useskel, self.properties.usebbox, self.properties.usescale, self.properties.animspec, matfun, self.properties.derigify)
exportIQM(operator, context, self.properties.filepath, self.properties.usemesh, self.properties.useskel, self.properties.usebbox, self.properties.usescale, self.properties.animspec, matfun, self.properties.derigify)
return {'FINISHED'}

def check(self, context):
Expand Down
Loading