Skip to content

Commit

Permalink
issue #11, blender 2.7 stuff and python2 removed
Browse files Browse the repository at this point in the history
I also tested a few smaller things and added the lines from helour.
rigify itself is there but looks odd, some of the drivers work some not

Or I am not smart enough ... I dunno if things can be closed.
please check.
  • Loading branch information
black-punkduck committed Nov 23, 2021
1 parent 72fcc5d commit 51c9475
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 280 deletions.
61 changes: 2 additions & 59 deletions 9_export_mhx2/save_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@
import log
import sys

python3 = sys.version_info[0] >= 3


def saveJson(struct, filepath, binary=False):
if binary:
if python3:
bdata = bytes(encodeJsonData3(struct, ""), 'utf8')
else:
bdata = encodeJsonData2(struct, "")
bdata = bytes(encodeJsonData3(struct, ""), 'utf8')
with gzip.open(filepath, 'wb') as fp:
fp.write(bdata)
else:
if python3:
string = encodeJsonData3(struct, "")
else:
string = encodeJsonData2(struct, "")
string = encodeJsonData3(struct, "")
with codecs.open(filepath, "w", encoding="utf-8") as fp:
fp.write(string)
fp.write("\n")
Expand Down Expand Up @@ -94,54 +85,6 @@ def encodeJsonData3(data, pad=""):
log.debug(data)
raise RuntimeError("Can't encode: %s %s" % (data, data.type))

def encodeJsonData2(data, pad=""):
if data is None:
return "null"
elif isinstance(data, (bool, np.bool_)):
if data:
return "true"
else:
return "false"
elif isinstance(data, (float, np.float32, np.float64)):
if abs(data) < 1e-6:
return "0"
else:
if abs(data) > 99999: # otherwise %.5g will destroy index numbers bigger than 100000 by writing 1e05
return "%d" % int(data)
else:
return "%.5g" % data
elif isinstance(data, (int, np.int32, np.uint32)):
return str(data)
elif isinstance(data, (str, unicode)):
return "\"%s\"" % data
elif isinstance(data, (list, tuple, np.ndarray)):
if leafList(data):
string = "["
string += ",".join([encodeJsonData2(elt) for elt in data])
return string + "]"
else:
string = "["
string += ",".join(
["\n " + pad + encodeJsonData2(elt, pad+" ")
for elt in data])
if string == "[":
return "[]"
else:
return string + "\n%s]" % pad
elif isinstance(data, dict):
string = "{"
string += ",".join(
["\n %s\"%s\" : " % (pad, key) + encodeJsonData2(value, pad+" ")
for key,value in data.items()])
if string == "{":
return "{}"
else:
return string + "\n%s}" % pad
else:
log.debug(data)
raise RuntimeError("Can't encode: %s %s" % (data, data.type))


def leafList(data):
for elt in data:
if isinstance(elt, (list,tuple,dict)):
Expand Down
27 changes: 6 additions & 21 deletions import_runtime_mhx2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
bl_info = {
'name': 'Import-Runtime: MakeHuman Exchange 2 (.mhx2)',
'author': 'Thomas Larsson',
'version': (0,31),
'version': (0,32),
'blender': (2, 80, 0),
'location': "File > Import-Export",
'description': 'Import files in the new MakeHuman eXchange format (.mhx2)',
Expand All @@ -36,10 +36,7 @@
import importlib
importlib.reload(utils)
importlib.reload(import_props)
if utils.b28():
importlib.reload(buttons28)
else:
importlib.reload(buttons27)
importlib.reload(buttons28)
importlib.reload(armature)
importlib.reload(hm8)
importlib.reload(error)
Expand All @@ -66,10 +63,7 @@
print("Loading MHX2 importer-runtime v %d.%d" % bl_info["version"])
from . import utils
from . import import_props
if utils.b28():
from . import buttons28
else:
from . import buttons27
from . import buttons28
from . import armature
from . import materials
from . import shaders
Expand All @@ -90,10 +84,7 @@
from .error import *
from .utils import *

if not b28():
Region = "TOOLS"
else:
Region = "UI"
Region = "UI"

#------------------------------------------------------------------------
# Setup panel
Expand Down Expand Up @@ -658,10 +649,7 @@ def register():
for cls in classes:
bpy.utils.register_class(cls)

if b28():
bpy.types.TOPBAR_MT_file_import.append(menu_func)
else:
bpy.types.INFO_MT_file_import.append(menu_func)
bpy.types.TOPBAR_MT_file_import.append(menu_func)


def unregister():
Expand All @@ -685,10 +673,7 @@ def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)

if b28():
bpy.types.TOPBAR_MT_file_import.remove(menu_func)
else:
bpy.types.INFO_MT_file_import.remove(menu_func)
bpy.types.TOPBAR_MT_file_import.remove(menu_func)


if __name__ == "__main__":
Expand Down
16 changes: 8 additions & 8 deletions import_runtime_mhx2/armature/rigify.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ def rigifyMhx(context, parser, taken={}):
raise RuntimeError("Rigify: %s is neither an armature nor has armature parent" % ob)
activateObject(context, rig)

group = None
for grp in bpy.data.collections:
if rig.name in grp.objects:
group = grp
collection = None
for col in bpy.data.collections:
if rig.name in col.objects:
collection = col
break
print("Group: %s" % group)
print("Collection: %s" % collection)

# Rename some bones
for bname,bname1 in Renames:
Expand Down Expand Up @@ -360,8 +360,8 @@ def rigifyMhx(context, parser, taken={}):
if nname is None:
print("Warning: missing vertex group %s" % gname)

if group:
group.objects.link(gen)
if collection:
collection.objects.link(gen)

# Parent widgets under empty
empty = bpy.data.objects.new("Widgets", None)
Expand All @@ -378,7 +378,7 @@ def rigifyMhx(context, parser, taken={}):

#Clean up
setattr(gen, ShowXRay, True)
#setattr(gen, DrawType, 'STICK')
setattr(gen, DrawType, 'WIRE')
gen.MhxRigify = False
name = rig.name
deleteObject(context, rig)
Expand Down
2 changes: 1 addition & 1 deletion import_runtime_mhx2/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def draw(self, context):
global theErrorLines
for line in theErrorLines:
print(line)
self.layout.label(line)
self.layout.label(text=line)


class MhxError(Exception):
Expand Down
24 changes: 4 additions & 20 deletions import_runtime_mhx2/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
from .hm8 import *
from .error import *
from .utils import *
if not b28():
from .buttons27 import Mhx2Import
else:
from .buttons28 import Mhx2Import
from .buttons28 import Mhx2Import

LowestVersion = 22
HighestVersion = 49
Expand Down Expand Up @@ -209,7 +206,7 @@ def build(struct, cfg, context):
setMhHuman(mhHuman)
scn.MhxDesignHuman = getMhHuman()["name"]

if b28() and mhHuman:
if mhHuman:
col = bpy.data.collections.new(mhHuman['name'].split(':', 1)[0])
bpy.context.collection.children.link(col)
scn['MHCollection'] = col
Expand Down Expand Up @@ -341,9 +338,6 @@ def build(struct, cfg, context):
if cfg.useOverride:
deleteAllSelected(human, proxies, context)

if not b28():
addToGroup(groupName, rig, human, proxies)

if cfg.useOverride and cfg.mergeBodyParts:
from .merge import mergeBodyParts
proxyTypes = ["Eyes", "Eyebrows", "Eyelashes", "Teeth", "Tongue", "Genitals"]
Expand Down Expand Up @@ -372,20 +366,10 @@ def build(struct, cfg, context):
activateObject(context, proxy)
bpy.ops.object.mode_set(mode='OBJECT')

if b28() and 'MHCollection' in scn:
if 'MHCollection' in scn:
del scn['MHCollection']


def addToGroup(groupName, rig, human, proxies):
grp = bpy.data.groups.new(groupName)
if rig:
grp.objects.link(rig)
if human:
grp.objects.link(human)
for _,ob in proxies:
grp.objects.link(ob)


def getEffectiveHuman(human, proxy, useProxy):
if proxy and (useProxy or not human):
return proxy
Expand Down Expand Up @@ -552,4 +536,4 @@ def initialize():

def uninitialize():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.utils.unregister_class(cls)
21 changes: 6 additions & 15 deletions import_runtime_mhx2/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import bpy
import os
import math
from .utils import b28
D = math.pi/180

# ---------------------------------------------------------------------
Expand Down Expand Up @@ -133,8 +132,7 @@ def buildHairMaterialCycles(mat, rgb):

def buildMaterialCycles(mat, mhMat, scn, cfg):
print("Creating CYCLES material", mat.name)
if b28():
mat.blend_method = 'HASHED'
mat.blend_method = 'HASHED'
mat.use_nodes= True
mat.node_tree.nodes.clear()
tree = NodeTree(mat.node_tree)
Expand All @@ -145,27 +143,21 @@ def buildMaterialCycles(mat, mhMat, scn, cfg):
principled.inputs['Base Color'].default_value[0:3] = mhMat['diffuse_color']
principled.inputs['Roughness'].default_value = 1.0 - mhMat['shininess']

color_space = 'sRGB' if b28() else None

diffuseTex = tree.addTexImageNode(mhMat, texco, "diffuse_texture", cfg, color_space)
diffuseTex = tree.addTexImageNode(mhMat, texco, "diffuse_texture", cfg, 'sRGB')
if diffuseTex:
links.new(diffuseTex.outputs['Color'], principled.inputs['Base Color'])
links.new(diffuseTex.outputs['Alpha'], principled.inputs['Alpha'])

color_space = 'Non-Color' if b28() else None

bumpTex = tree.addTexImageNode(mhMat, texco, "bump_map_texture", cfg, color_space)
bumpTex = tree.addTexImageNode(mhMat, texco, "bump_map_texture", cfg, 'Non-Color')
bumpMap = None
if bumpTex:
if not b28(): bumpTex.color_space = 'NONE'
bumpMap = tree.addNode(5, 'ShaderNodeBump')
bumpMap.inputs['Strength'].default_value = mhMat.get('bump_map_intensity', 1.0)
links.new(bumpTex.outputs['Color'], bumpMap.inputs['Height'])
links.new(bumpMap.outputs['Normal'], principled.inputs['Normal'])

normalTex = tree.addTexImageNode(mhMat, texco, "normal_map_texture", cfg, color_space)
normalTex = tree.addTexImageNode(mhMat, texco, "normal_map_texture", cfg, 'Non-Color')
if normalTex:
if not b28(): normalTex.color_space = 'NONE'
normalMap = tree.addNode(4, 'ShaderNodeNormalMap')
normalMap.space = 'TANGENT'
normalMap.uv_map = "UVMap"
Expand All @@ -176,9 +168,8 @@ def buildMaterialCycles(mat, mhMat, scn, cfg):
else:
links.new(normalMap.outputs['Normal'], principled.inputs['Normal'])

glossyTex = tree.addTexImageNode(mhMat, texco, "specular_map_texture", cfg, color_space)
glossyTex = tree.addTexImageNode(mhMat, texco, "specular_map_texture", cfg, 'Non-Color')
if glossyTex:
if not b28(): glossyTex.color_space = 'NONE'
invertColor = tree.addNode(5, 'ShaderNodeInvert')
links.new(glossyTex.outputs['Color'], invertColor.inputs['Color'])
links.new(invertColor.outputs['Color'], principled.inputs['Roughness'])
Expand Down Expand Up @@ -529,4 +520,4 @@ def initialize():

def uninitialize():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.utils.unregister_class(cls)
Loading

0 comments on commit 51c9475

Please sign in to comment.