Skip to content

Commit

Permalink
Fix bug (model3doExported): Fix generating hierarchy node list
Browse files Browse the repository at this point in the history
Removed hierarchy node list sorting function because mesh nodes are considered to be already in order when exporting.
Fixed _get_hnode_idx to get correct parent idx.
  • Loading branch information
smlu committed May 29, 2020
1 parent 715d4ea commit d4c236a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 83 deletions.
2 changes: 1 addition & 1 deletion ijim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Indiana Jones and the Infernal Machine",
"description": "Import-Export game model (.3do) and material (.mat)",
"author": "smlu",
"version": (0, 9, 1),
"version": (0, 9, 2),
"blender": (2, 79, 0),
"location": "File > Import-Export",
"wiki_url": "https://github.com/smlu/blender-ijim",
Expand Down
91 changes: 9 additions & 82 deletions ijim/model/model3doExporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _get_face_tex_mode(face: bmesh.types.BMFace, bmesh: bmesh.types.BMesh):
return _get_face_property_or_default(face, tag, 3)


def _add_mesh_to_model3d(mesh: bpy.types.Mesh, model: Model) -> int:
def _model3do_add_mesh(model: Model, mesh: bpy.types.Mesh) -> int:
if mesh is None:
return -1

Expand Down Expand Up @@ -187,14 +187,13 @@ def _get_obj_hnode_name(obj: bpy.types.Object):

def _get_hnode_idx(nodes: List[MeshHierarchyNode], name):
for idx, node in enumerate(nodes):
if name in node.name:
if name == node.name:
return idx
return -1

def _get_obj_hnode_idx(nodes: List[MeshHierarchyNode], obj: bpy.types.Object):
if obj is None:
return -1

name = _get_obj_hnode_name(obj)
return _get_hnode_idx(nodes, name)

Expand All @@ -204,7 +203,7 @@ def _get_hnode_last_sibling(first_child: MeshHierarchyNode, nodes: List[MeshHier
return _get_hnode_last_sibling(nodes[sidx], nodes)
return first_child

def _update_model3do_hirarchy(model: Model, mesh_idx: int, obj: bpy.types.Object, parent: bpy.types.Object):
def _model3do_add_hnode(model: Model, mesh_idx: int, obj: bpy.types.Object, parent: bpy.types.Object):
name = _get_obj_hnode_name(obj)
if name in model.hierarchyNodes:
return
Expand Down Expand Up @@ -232,88 +231,18 @@ def _update_model3do_hirarchy(model: Model, mesh_idx: int, obj: bpy.types.Object
_set_hnode_location(node, obj)
model.hierarchyNodes.append(node)

def _sort_model3do_hirarchy(model: Model):
ohl = model.hierarchyNodes

# First stage: Order node idxs by type
otd = {
MeshNodeType.Nothing : [],
MeshNodeType.Vehicle : [],
MeshNodeType.Torso : [],
MeshNodeType.Head : [],
MeshNodeType.Torso : [],
MeshNodeType.LeftArm : [],
MeshNodeType.LeftHand : [],
MeshNodeType.LeftHand2 : [],
MeshNodeType.RightArm : [],
MeshNodeType.RightHand : [],
MeshNodeType.RightHand2 : [],
MeshNodeType.Hip : [],
MeshNodeType.RightLeg : [],
MeshNodeType.LeftLeg : [],
MeshNodeType.BackPart : [],
MeshNodeType.FrontPart : [],
MeshNodeType.BackWheel : [],
MeshNodeType.FrontWheel : [],
}

for idx, n in enumerate(ohl):
otd[n.type] += [idx]

# Second stage: Grup node idxs by parent and order by parents by type
hnond = OrderedDict()
for v in otd.values():
for idx in v:
n = ohl[idx]
hnond[n.name] = idx

lidx = []
for idx in hnond.values():
if idx not in lidx:
n = ohl[idx]
if n.parentIdx > -1:
pn = ohl[n.parentIdx].name
pidx = hnond[pn]
if pidx not in lidx:
lidx.append(pidx)
lidx.append(idx)

# Third stage: Make new list of hirarchy nodes and update child/sibling idxs
nhl = []
for idx in lidx:
n = ohl[idx]

if n.parentIdx > -1:
n.parentIdx = _get_hnode_idx(nhl, ohl[n.parentIdx].name)
pn = nhl[n.parentIdx]

node_idx = len(nhl)
if pn.firstChildIdx == -1:
pn.firstChildIdx = node_idx
else:
snode = nhl[pn.firstChildIdx]
snode = _get_hnode_last_sibling(snode, nhl)
snode.siblingIdx = node_idx

n.siblingIdx = -1
n.firstChildIdx = -1
nhl.append(n)

# Set new hirarchy list
model.hierarchyNodes = nhl

def _add_obj_to_model3do(obj: bpy.types.Object, model: Model, parent: bpy.types.Object = None):
def _model3do_add_obj(model: Model, obj: bpy.types.Object, parent: bpy.types.Object = None):
if 'EMPTY' != obj.type != 'MESH' or _is_aux_obj(obj):
return

mesh_idx = _add_mesh_to_model3d(obj.data, model)
mesh_idx = _model3do_add_mesh(model, obj.data)
if mesh_idx > -1:
mesh = model.geosets[0].meshes[mesh_idx]
_set_mesh_properties(mesh, obj)

_update_model3do_hirarchy(model, mesh_idx, obj, parent)
_model3do_add_hnode(model, mesh_idx, obj, parent)
for child in obj.children:
_add_obj_to_model3do(child, model, obj)
_model3do_add_obj(model, child, obj)

def makeModel3doFromObj(name, obj: bpy.types.Object):
model = Model(name)
Expand All @@ -328,12 +257,10 @@ def makeModel3doFromObj(name, obj: bpy.types.Object):
model.radius = radius_obj.dimensions[0] / 2

for child in obj.children:
_add_obj_to_model3do(child, model, obj)

_sort_model3do_hirarchy(model)
_model3do_add_obj(model, child, obj)

return model


def exportObject(obj: bpy.types.Object, path: str):
bpy.path.ensure_ext(path, '.3do')
print("exporting 3DO: %r..." % (path), end="")
Expand Down

0 comments on commit d4c236a

Please sign in to comment.