diff --git a/io_import_scene_mhx.py b/io_import_scene_mhx.py index 0ac4f04f7e6b24f38ccd6fdc37df9f2e1ea83912..ebc25716a361ad5cfea70aa41941e06680bb346d 100644 --- a/io_import_scene_mhx.py +++ b/io_import_scene_mhx.py @@ -39,7 +39,7 @@ Alternatively, run the script in the script editor (Alt-P), and access from the bl_info = { 'name': 'Import: MakeHuman (.mhx)', 'author': 'Thomas Larsson', - 'version': (1, 14, 1), + 'version': (1, 14, 2), "blender": (2, 65, 0), 'location': "File > Import > MakeHuman (.mhx)", 'description': 'Import files in the MakeHuman eXchange format (.mhx)', @@ -52,7 +52,7 @@ bl_info = { MAJOR_VERSION = 1 MINOR_VERSION = 14 FROM_VERSION = 13 -SUB_VERSION = 1 +SUB_VERSION = 2 # # @@ -213,11 +213,12 @@ def readMhxFile(filePath): theMessage = "" fileName = os.path.expanduser(filePath) - (shortName, ext) = os.path.splitext(fileName) + _,ext = os.path.splitext(fileName) if ext.lower() != ".mhx": - print("Error: Not a mhx file: " + fileName) + print("Error: Not a mhx file: %s" % fileName.encode('utf-8', 'strict')) return - print( "Opening MHX file "+ fileName ) + print( "Opening MHX file %s " % fileName.encode('utf-8', 'strict') ) + print("Toggle %x" % toggle) time1 = time.clock() # ignore = False # UNUSED @@ -267,7 +268,7 @@ def readMhxFile(filePath): tokens[-1][2] = sub level -= 1 except: - print( "Tokenizer error at or before line %d" % lineNo ) + print( "Tokenizer error at or before line %d.\nThe mhx file has been corrupted.\nTry to export it again from MakeHuman." % lineNo ) print( line ) stack.pop() elif lineSplit[-1] == ';': @@ -286,7 +287,7 @@ def readMhxFile(filePath): file.close() if level != 0: - MyError("Tokenizer out of kilter %d" % level) + MyError("Tokenizer error (%d).\nThe mhx file has been corrupted.\nTry to export it again from MakeHuman." % level) scn = clearScene() print( "Parsing" ) parse(tokens) @@ -296,7 +297,7 @@ def readMhxFile(filePath): print("Doing %s" % expr) exec(expr, glbals, lcals) except: - msg = "Failed: \n"+expr + msg = "Failed: %s\n" % expr print( msg ) nErrors += 1 #MyError(msg) @@ -306,7 +307,6 @@ def readMhxFile(filePath): #bpy.ops.wm.properties_edit(data_path="object", property="MhxRig", value=theArmature["MhxRig"]) time2 = time.clock() - print("toggle = %x" % toggle) msg = "File %s loaded in %g s" % (fileName, time2-time1) if nErrors: msg += " but there where %d errors. " % (nErrors) @@ -370,8 +370,7 @@ ifResult = False def parse(tokens): global MHX249, ifResult, theScale, defaultScale, One - for (key, val, sub) in tokens: - print("Parse %s" % key) + for (key, val, sub) in tokens: data = None if key == 'MHX': checkMhxVersion(int(val[0]), int(val[1])) @@ -454,7 +453,6 @@ def parse(tokens): if ob: bpy.context.scene.objects.active = ob mat = ob.data.materials[int(val[2])] - print("matanim", ob, mat) parseAnimationData(mat, val, sub) elif key == 'ShapeKeys': try: @@ -467,10 +465,6 @@ def parse(tokens): else: data = parseDefaultType(key, val, sub) - if data and key != 'Mesh': - print( data ) - return - # # parseDefaultType(typ, args, tokens): # @@ -481,20 +475,15 @@ def parseDefaultType(typ, args, tokens): name = args[0] data = None expr = "bpy.data.%s.new('%s')" % (Plural[typ], name) - # print(expr) data = eval(expr) - # print(" ok", data) bpyType = typ.capitalize() - print(bpyType, name, data) loadedData[bpyType][name] = data if data is None: return None for (key, val, sub) in tokens: - #print("%s %s" % (key, val)) defaultKey(key, val, sub, 'data', [], globals(), locals()) - print("Done ", data) return data # @@ -597,7 +586,6 @@ def channelFromDataPath(dataPath, index): expr = "ob.%s]" % (words[0]) cwords = words[1].split('"') channel = cwords[1] - # print(expr, channel, index) return (expr, channel) def parseActionFCurve(act, ob, args, tokens): @@ -647,7 +635,6 @@ def parseKeyFramePoint(pt, args, tokens): def parseAnimationData(rna, args, tokens): if not eval(args[1]): return - print("Parse Animation data") if rna.animation_data is None: rna.animation_data_create() adata = rna.animation_data @@ -656,7 +643,6 @@ def parseAnimationData(rna, args, tokens): fcu = parseAnimDataFCurve(adata, rna, val, sub) else: defaultKey(key, val, sub, 'adata', [], globals(), locals()) - print(adata) return adata def parseAnimDataFCurve(adata, rna, args, tokens): @@ -692,7 +678,6 @@ def parseDriver(adata, dataPath, index, rna, args, tokens): expr = "rna." + words[0] + ']' pwords = words[1].split('"') prop = pwords[1] - #print("prop", expr, prop) bone = eval(expr) return None else: @@ -703,12 +688,9 @@ def parseDriver(adata, dataPath, index, rna, args, tokens): expr += "." + words[n] expr += ".driver_add('%s', index)" % channel - #print("expr", rna, expr) fcu = eval(expr) drv = fcu.driver - #print(" Driver type", drv, args[0]) drv.type = args[0] - #print(" ->", drv.type) for (key, val, sub) in tokens: if key == 'DriverVariable': var = parseDriverVariable(drv, rna, val, sub) @@ -719,9 +701,7 @@ def parseDriver(adata, dataPath, index, rna, args, tokens): def parseDriverVariable(drv, rna, args, tokens): var = drv.variables.new() var.name = args[0] - #print(" Var type", var, args[1]) var.type = args[1] - #print(" ->", var.type) nTarget = 0 for (key, val, sub) in tokens: if key == 'Target': @@ -752,7 +732,6 @@ def parseDriverTarget(var, nTarget, rna, args, tokens): dtype = args[1].capitalize() dtype = 'Object' targ.id = loadedData[dtype][name] - #print(" ->", targ.id) for (key, val, sub) in tokens: if key == 'data_path': words = val[0].split('"') @@ -932,7 +911,7 @@ def doLoadImage(filepath): path1 = os.path.expanduser(filepath) file1 = os.path.realpath(path1) if os.path.isfile(file1): - print( "Found file "+file1 ) + print( "Found file %s." % file1.encode('utf-8','strict') ) try: img = bpy.data.images.load(file1) return img @@ -940,7 +919,7 @@ def doLoadImage(filepath): print( "Cannot read image" ) return None else: - print( "No file "+file1 ) + print( "No such file: %s" % file1.encode('utf-8','strict') ) return None @@ -952,13 +931,13 @@ def loadImage(filepath): file1 = os.path.realpath(path1) (path, filename) = os.path.split(file1) (name, ext) = os.path.splitext(filename) - print( "Loading ", filepath, " = ", filename ) + print( "Loading %s = %s" % (filepath.encode('utf-8','strict'), filename.encode('utf-8','strict')) ) # img = doLoadImage(texDir+"/"+name+".png") # if img: # return img - img = doLoadImage(texDir+"/"+filename) + img = doLoadImage(os.path.join(texDir, filename)) if img: return img @@ -966,7 +945,7 @@ def loadImage(filepath): # if img: # return img - img = doLoadImage(path+"/"+filename) + img = doLoadImage(os.path.join(path, filename)) if img: return img @@ -977,7 +956,7 @@ def loadImage(filepath): TexDir = Draw.PupStrInput("TexDir? ", path, 100) texDir = os.path.expanduser(TexDir) - img = doLoadImage(texDir+"/"+name+".png") + img = doLoadImage(os.path.join(texDir, name+".png")) if img: return img @@ -1035,9 +1014,7 @@ def parseObject(args, tokens): ob = None if ob is None: - print("Create", name, data, datName) ob = createObject(typ, name, data, datName) - print("created", ob) linkObject(ob, data) for (key, val, sub) in tokens: @@ -1056,7 +1033,6 @@ def parseObject(args, tokens): if bpy.context.object == ob: if ob.type == 'MESH': - print("Smooth shade", ob) bpy.ops.object.shade_smooth() else: print("Context", ob, bpy.context.object, bpy.context.scene.objects.active) @@ -1074,7 +1050,6 @@ def linkObject(ob, data): #print("Data", data, ob.data) if data and ob.data is None: ob.data = data - print("Data linked", ob, ob.data) scn = bpy.context.scene scn.objects.link(ob) scn.objects.active = ob @@ -1244,10 +1219,8 @@ def parseMesh (args, tokens): try: me.polygons BMeshAware = True - print("Using BMesh") except: BMeshAware = False - print("Not using BMesh") mats = [] nuvlayers = 0 @@ -1281,7 +1254,6 @@ def parseMesh (args, tokens): parseFaces2BMesh(sub, me) else: parseFaces2NoBMesh(sub, me) - print(me) return me # @@ -1504,6 +1476,7 @@ def doShape(name): else: return (toggle & T_Face) + def parseShapeKeys(ob, me, args, tokens): for (key, val, sub) in tokens: if key == 'ShapeKey': @@ -1521,8 +1494,6 @@ def parseShapeKeys(ob, me, args, tokens): prop = "Mhv" + name parseUnits(prop, ob, sub) ob.active_shape_key_index = 0 - print("Shapekeys parsed") - return def parseUnits(prop, ob, sub): @@ -1552,6 +1523,7 @@ def parseShapeKey(ob, me, args, tokens): MyError("ShapeKey L/R %s" % lr) return + def addShapeKey(ob, name, vgroup, tokens): skey = ob.shape_key_add(name=name, from_mix=False) if name != 'Basis': @@ -1641,13 +1613,6 @@ def parseBone(bone, amt, tokens, heads, tails): # pass elif key == 'hide' and val[0] == 'True': name = bone.name - ''' - #bpy.ops.object.mode_set(mode='OBJECT') - pbone = amt.bones[name] - pbone.hide = True - print("Hide", pbone, pbone.hide) - #bpy.ops.object.mode_set(mode='EDIT') - ''' else: defaultKey(key, val, sub, "bone", [], globals(), locals()) return bone @@ -1675,9 +1640,7 @@ def parsePose (args, tokens): prop = val[1] value = eval(val[2]) pb = pbones[bone] - print("Setting", pb, prop, val) pb[prop] = value - print("Prop set", pb[prop]) else: defaultKey(key, val, sub, "ob.pose", [], globals(), locals()) bpy.ops.object.mode_set(mode='OBJECT') @@ -2150,6 +2113,8 @@ theProperty = None def propNames(string): global alpha7 + #string = string.encode('utf-8', 'strict') + # Alpha 7 compatibility if string[0:2] == "&_": string = "Mhf"+string[2:] @@ -2160,15 +2125,16 @@ def propNames(string): elif string[0] == "*": string = "Mhs"+string[1:] alpha7 = True - elif string.startswith("Hide"): + elif len(string) > 4 and string[0:4] == "Hide": string = "Mhh"+string[4:] alpha7 = True - if string.startswith(("Mha", "Mhf", "Mhs", "Mhh", "Mhv", "Mhc")): + if string[0] == "_": + return None,None + elif (len(string) > 3 and + string[0:3] in ["Mha", "Mhf", "Mhs", "Mhh", "Mhv", "Mhc"]): name = string.replace("-","_") return name, '["%s"]' % name - elif string[0] == "_": - return None,None else: return string, '["%s"]' % string @@ -2182,11 +2148,8 @@ def defProp(args, var, glbals, lcals): rest += ", " + args[4] if name: - expr = 'bpy.types.Object.%s = %sProperty(%s)' % (name, proptype, rest) - print(expr) - exec(expr) - expr = '%s.%s = %s' % (var, name, value) - print(expr) + #expr = 'bpy.types.Object.%s = %sProperty(%s)' % (name, proptype, rest) + expr = '%s["%s"] = %s' % (var, name, value) exec(expr, glbals, lcals) @@ -2204,7 +2167,6 @@ def setProperty(args, var, glbals, lcals): if name: expr = '%s["%s"] = %s' % (var, name, value) exec(expr, glbals, lcals) - print(expr) if len(args) > 2: tip = 'description="%s"' % args[2].replace("_", " ") @@ -2217,9 +2179,6 @@ def setProperty(args, var, glbals, lcals): else: proptype = "Int" theProperty = (name, tip, proptype) - #if proptype == "Int": - # halt - return def defineProperty(args): @@ -2233,12 +2192,7 @@ def defineProperty(args): else: tip = tip + "," + args[1].replace(":", "=").replace('"', " ") expr = "bpy.types.Object.%s = %sProperty(%s)" % (name, proptype, tip) - print(expr) - exec(expr) - if proptype == "Int": - halt theProperty = None - return def defaultKey(ext, args, tokens, var, exclude, glbals, lcals): @@ -3053,11 +3007,12 @@ class ImportMhx(bpy.types.Operator, ImportHelper): print("execute flags %x" % toggle) theScale = self.scale + #filepathname = self.filepath.encode('utf-8', 'strict') try: readMhxFile(self.filepath) bpy.ops.mhx.success('INVOKE_DEFAULT', message = self.filepath) except MhxError: - print("Error when loading MHX file:\n" + theMessage) + print("Error when loading MHX file %s:\n" % self.filepath + theMessage) if self.advanced: writeDefaults()