Skip to content

Commit

Permalink
Update exporter to latest version of extension (2042c002)
Browse files Browse the repository at this point in the history
  • Loading branch information
eoineoineoin committed Jul 10, 2024
1 parent cc3966f commit 8e356cd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,15 @@ def _generateShapeData(self, node, glNode, export_settings):

if node.rigid_body.collision_shape == "CONVEX_HULL":
shape.type = "mesh"
shape.mesh = Mesh(glNode.mesh, convexHull=True)
shape.mesh = Mesh(glNode.mesh)
shape.extensions = {
rigidBody_Extension_Name: RigidBodiesShapeExtension(convexHull=True)
}
shape.mesh.skin = glNode.skin
return shape
elif node.rigid_body.collision_shape == "MESH":
shape.type = "mesh"
shape.mesh = Mesh(glNode.mesh, convexHull=False)
shape.mesh = Mesh(glNode.mesh)
shape.mesh.skin = glNode.skin
return shape
# If the shape is a geometric primitive, we may have to apply modifiers
Expand Down Expand Up @@ -693,12 +696,12 @@ def _generateShapeData(self, node, glNode, export_settings):
)
node_ext.collider.shape = shape_obj

shape_alignment.extensions[
rigidBody_Extension_Name
] = self.Extension(
name=rigidBody_Extension_Name,
extension=node_ext.to_dict(),
required=False,
shape_alignment.extensions[rigidBody_Extension_Name] = (
self.Extension(
name=rigidBody_Extension_Name,
extension=node_ext.to_dict(),
required=False,
)
)
glNode.children.append(shape_alignment)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def gather_import_node_after_hook(self, vnode, gltf_node, blender_object, gltf):
self.gather_import_node_after_hook_2(vnode, gltf_node, blender_object, gltf)
except:
import traceback

print(traceback.format_exc())

def gather_import_node_after_hook_2(self, vnode, gltf_node, blender_object, gltf):
Expand Down Expand Up @@ -138,10 +137,17 @@ def gather_import_node_after_hook_2(self, vnode, gltf_node, blender_object, gltf
# <todo.eoin Figure out if we can hook in a different mesh/skin/weights
# other than the one associated with this node
if shape.mesh != None:
if shape.mesh.convexHull:
blender_object.rigid_body.collision_shape = "CONVEX_HULL"
else:
blender_object.rigid_body.collision_shape = "MESH"
blender_object.rigid_body.collision_shape = "MESH"

if (
shape.extensions
and rigidBody_Extension_Name in shape.extensions
):
rbShapeExt = RigidBodiesShapeExtension.from_dict(
shape.extensions[rigidBody_Extension_Name]
)
if rbShapeExt.convexHull:
blender_object.rigid_body.collision_shape = "CONVEX_HULL"

# todo.eoin Collision systems

Expand Down
33 changes: 22 additions & 11 deletions addons/KHR_physics_rigid_bodies/io/com/gltf2_io_collision_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,26 @@ def from_dict(obj):

class Mesh:
mesh: Optional[int] = None
convexHull: Optional[bool] = None
skin: Optional[int] = None
# Todo: looks like blender never sets instance weights, always creating a new
# mesh instance instead. Not sure this can ever be populated. Verify.
# Blender never sets instance weights, always creating a new mesh instance.
# (See __gather_weights in gltf2_blender_gather_nodes), so neither weights
# nor useNodeWeights will ever be populated. May change in the future.
weights: Optional[list[float]] = None
useNodeWeights: Optional[bool] = None
extensions: Optional[Dict[str, Any]] = None
extras: Any = None

def __init__(self, mesh, convexHull = None):
def __init__(self, mesh):
self.mesh = mesh
self.convexHull = convexHull

def to_dict(self):
result = {}
result["mesh"] = self.mesh
result["convexHull"] = from_union([from_bool, from_none], self.convexHull)
result["skin"] = self.skin
result["weights"] = from_union([ lambda x: from_list(lambda x: from_float(x), x), from_none], self.weights)
result["weights"] = from_union(
[lambda x: from_list(lambda x: from_float(x), x), from_none], self.weights
)
result["useNodeWeights"] = self.useNodeWeights
result["extensions"] = from_union(
[lambda x: from_dict(from_extension, x), from_none], self.extensions
)
Expand All @@ -186,8 +188,15 @@ def from_dict(obj):
if obj == None:
return None
mesh = from_union([from_int, from_none], obj.get("mesh"))
convexHull = from_union([from_bool, from_none], obj.get("convexHull"))
result = Mesh(mesh, convexHull=convexHull)
result = Mesh(mesh)
result.skin = from_union([from_int, from_none], obj.get("skin"))
result.useNodeWeights = from_union(
[from_bool, from_none], obj.get("useNodeWeights")
)
result.weights = from_union(
[lambda x: from_list(lambda x: from_float(x), x), from_none],
obj.get("weights"),
)
result.extensions = from_union(
[lambda x: from_dict(lambda x: from_dict(lambda x: x, x), x), from_none],
obj.get("extensions"),
Expand Down Expand Up @@ -219,9 +228,11 @@ def to_dict(self):
result["cylinder"] = from_union(
[lambda x: to_class(Cylinder, x), from_none], self.cylinder
)
result["mesh"] = from_union(
[lambda x: to_class(Mesh, x), from_none], self.mesh
result["mesh"] = from_union([lambda x: to_class(Mesh, x), from_none], self.mesh)
result["extensions"] = from_union(
[lambda x: from_dict(from_extension, x), from_none], self.extensions
)
result["extras"] = from_extra(self.extras)
return result

@staticmethod
Expand Down
19 changes: 19 additions & 0 deletions addons/KHR_physics_rigid_bodies/io/com/gltf2_io_rigid_bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,25 @@ def from_dict(obj):
return joint


class RigidBodiesShapeExtension:
convexHull: Optional[bool] = None

def __init__(self, convexHull=None):
super().__init__()
self.convexHull = convexHull

def to_dict(self):
result = {}
if self.convexHull != None:
result["convexHull"] = self.convexHull
return result

@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
return RigidBodiesShapeExtension(obj.get("convexHull"))


class RigidBodiesNodeExtension:
motion: Optional[Motion] = None
collider: Optional[Collider] = None
Expand Down

0 comments on commit 8e356cd

Please sign in to comment.