-
Notifications
You must be signed in to change notification settings - Fork 2
/
Phys2Export.py
341 lines (285 loc) · 12.6 KB
/
Phys2Export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!BPY
"""
Blender: 2.80
Author: Someone
History:<br>
* v0.1 (28-May-2019) - Export of box,sphere,capsule,mesh,convex hull from blender physics data for physx 3.4.0
"""
#from Blender import *
from xml.dom import minidom
import bpy
from mathutils import Vector, Matrix
import math
import os
import subprocess
import shutil
def shapeBounds(obj):
x = obj.bound_box[6][0] - obj.bound_box[0][0]
y = obj.bound_box[6][1] - obj.bound_box[0][1]
z = obj.bound_box[6][2] - obj.bound_box[0][2]
return (x, y, z)
def removeScaleFromMatrix(m):
print(m)
loc, rot, sca = m.decompose()
print("loc:", loc)
m = Matrix.Translation(loc) @ rot.to_matrix().to_4x4()
print(m)
return (m, sca)
def appendTextNode(parent, name, text):
node = parent.ownerDocument.createElement(name)
node.appendChild( parent.ownerDocument.createTextNode( text ))
parent.appendChild(node)
return node
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def formatMeshData(mesh, transform, tris):
vertices = []
for vx in mesh.vertices:
v = transform @ vx.co
vertices.append( ' '.join(map(str, v)))
triangles = []
if tris:
mesh.calc_loop_triangles()
for tri in mesh.loop_triangles:
triangles.extend( tri.vertices )
return (' '.join(map(str,vertices)), ' '.join(map(str,triangles)))
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def createShape(xShapes, name, transform):
xShape = xShapes.ownerDocument.createElement('PxShape')
saveTransform(xShape, 'LocalPose', transform)
appendTextNode(xShape, 'SimulationFilterData', '65537')
appendTextNode(xShape, 'ContactOffset', '1')
appendTextNode(xShape, 'RestOffset', '0')
appendTextNode(xShape, 'Flags', 'eSIMULATION_SHAPE|eSCENE_QUERY_SHAPE|eVISUALIZATION')
appendTextNode(xShape, 'Name', name)
xMaterial = xShape.appendChild( xShapes.ownerDocument.createElement('Materials') )
appendTextNode(xMaterial, 'PxMaterialRef', '1893755024') # global constant
xShapes.appendChild(xShape)
return xShape
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def saveBoxCollision(xRoot, xShapes, obj, transform):
mat, scale = removeScaleFromMatrix(transform @ obj.matrix_world)
xShape = createShape(xShapes, obj.name, mat)
bounds = shapeBounds(obj)
print('bounds: ', bounds, ' scale: ', scale)
xGeom = xShape.ownerDocument.createElement('Geometry')
xBox = xShape.ownerDocument.createElement('PxBoxGeometry')
appendTextNode(xBox, 'HalfExtents', '{0:f} {1:f} {2:f}'.format( bounds[0] * scale[0] * 0.5, bounds[1] * scale[1] * 0.5, bounds[2] * scale[2] * 0.5 ))
xShape.appendChild(xGeom)
xGeom.appendChild(xBox)
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def saveCapsuleCollision(xRoot, xShapes, obj, transform):
fix = Matrix( [ (0,0,1,0),(0,1,0,0),(-1,0,0,0),(0,0,0,1) ] ) # Rotation fix as blender capsules are in Z axis whereas physx uses X axis
mat, scale = removeScaleFromMatrix(transform @ obj.matrix_world @ fix )
xShape = createShape(xShapes, obj.name, mat)
bounds = shapeBounds(obj)
radius = 0.5 * max( abs(bounds[0] * scale[0]), abs(bounds[1] * scale[1]))
height = abs(bounds[2]*scale[2]) - 2 * radius
print('bounds: ', bounds, 'scale', scale, ' radius: ', radius, 'height', height)
xGeom = xShape.ownerDocument.createElement('Geometry')
xCapsule = xShape.ownerDocument.createElement('PxCapsuleGeometry')
appendTextNode(xCapsule, 'Radius', str(radius))
appendTextNode(xCapsule, 'HalfHeight', str(height * 0.5))
xShape.appendChild(xGeom)
xGeom.appendChild(xCapsule)
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def saveSphereCollision(xRoot, xShapes, obj, transform):
mat, scale = removeScaleFromMatrix(transform @ obj.matrix_world)
xShape = createShape(xShapes, obj.name, mat)
bounds = shapeBounds(obj)
xGeom = xShape.ownerDocument.createElement('Geometry')
xSphere = xShape.ownerDocument.createElement('PxSphereGeometry')
appendTextNode(xSphere, 'Radius', str(max(bounds) * max(scale)))
xShape.appendChild(xGeom)
xGeom.appendChild(xSphere)
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def saveConvexCollision(xRoot, xShapes, obj, transform):
mat, scale = removeScaleFromMatrix(transform @ obj.matrix_world )
scaleMatrix = Matrix([(scale[0],0,0,0),(0,scale[1],0,0),(0,0,scale[2],0),(0,0,0,1)])
# Create convex mesh copy
apply_modifiers = obj.rigid_body.mesh_source == 'FINAL'
depsgraph = bpy.context.evaluated_depsgraph_get()
tobj = obj.evaluated_get(depsgraph) if apply_modifiers else obj
mesh = tobj.to_mesh()
import bmesh
bm = bmesh.new()
bm.from_mesh(mesh)
r = bmesh.ops.convex_hull(bm, input=bm.verts)
#bmesh.ops.triangulate(bm, faces=bm.faces)
bm.to_mesh(mesh)
bm.free()
del bm
# Save mesh
xDoc = xShapes.ownerDocument
points, triangles = formatMeshData(mesh, scaleMatrix, False)
id = str(hash(points)&0xefffffff)
xMesh = xDoc.createElement('PxConvexMesh')
appendTextNode(xMesh, 'Id', id)
appendTextNode(xMesh, 'Points', points)
xRoot.insertBefore(xMesh, xRoot.firstChild)
# Save shape
xShape = createShape(xShapes, obj.name, mat)
xGeom = xShape.appendChild( xDoc.createElement('Geometry') )
xMesh = xGeom.appendChild( xDoc.createElement('PxConvexMeshGeometry') )
xScale = xMesh.appendChild( xDoc.createElement('Scale') )
appendTextNode(xScale, 'Scale', '1 1 1')
appendTextNode(xScale, 'Rotation', '0 0 0 1')
appendTextNode(xMesh, 'ConvexMesh', id)
tobj.to_mesh_clear()
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def saveMeshCollision(xRoot, xShapes, obj, transform):
# Get mesh
apply_modifiers = obj.rigid_body.mesh_source == 'FINAL'
depsgraph = bpy.context.evaluated_depsgraph_get()
tobj = obj.evaluated_get(depsgraph) if apply_modifiers else obj
mesh = tobj.to_mesh()
# Save mesh
xDoc = xShapes.ownerDocument
points, triangles = formatMeshData(mesh, transform @ obj.matrix_world, True)
id = str(hash(points)&0xefffffff)
xMesh = xDoc.createElement('PxBVH33TriangleMesh')
appendTextNode(xMesh, 'Id', id)
appendTextNode(xMesh, 'Points', points)
appendTextNode(xMesh, 'Triangles', triangles)
xRoot.insertBefore(xMesh, xRoot.firstChild)
# Save shape
xShape = createShape(xShapes, obj.name, Matrix())
xGeom = xShape.appendChild( xDoc.createElement('Geometry') )
xMesh = xGeom.appendChild( xDoc.createElement('PxTriangleMeshGeometry') )
xScale = xMesh.appendChild( xDoc.createElement('Scale') )
appendTextNode(xScale, 'Scale', '1 1 1')
appendTextNode(xScale, 'Rotation', '0 0 0 1')
appendTextNode(xMesh, 'TriangleMesh', id)
tobj.to_mesh_clear()
## ------------------------------------------------------------------------------------------------------------------------------------- ##
def saveTransform(xObject, name, m):
q = m.to_quaternion()
p = m.to_translation()
print(name, q, p)
trans = '{0:f} {1:f} {2:f} {3:f} {4:f} {5:f} {6:f}'.format(q[1], q[2], q[3], q[0], p[0], p[1], p[2])
appendTextNode(xObject, name, trans)
def hasCollision(operator, object, types):
if not object.rigid_body: return False
if object.rigid_body.collision_shape in types: return True
operator.report( {'WARNING'}, "Unsupported collision shape : " + object.rigid_body.collision_shape)
return False
def addChildrenToSet(operator, object, set, types):
for child in object.children:
if hasCollision(operator, child, types):
set.add(child)
addChildrenToSet(operator, child, set, types)
def commonParent(a, b):
if not a or not b: return None
# depth in tree
da = 0
db = 0
p = a
while p:
da += 1
p = p.parent
p = b
while p:
db += 1
p = p.parent
while da > db:
da -= 1
a = a.parent
while db > da:
db -= 1
b = b.parent
while a != b:
a = a.parent
b = b.parent
return a;
def save(operator, context, filepath,
objects=0, # How to specify this?
transform=0, # All, Selected, Selected+Children
dynamicObjects=False
):
# just check if there is extension - .xml
if '.repx' not in filepath.lower():
filepath = filepath + ".repx"
print("Saving", str(filepath))
# go to the object mode
if context.active_object:
bpy.ops.object.mode_set(mode='OBJECT')
# List of supported collision types
shapeFunctions = { 'BOX': saveBoxCollision,
'SPHERE': saveSphereCollision,
'CAPSULE': saveCapsuleCollision,
'CONVEX_HULL': saveConvexCollision,
'MESH': saveMeshCollision }
# get objects to export
bodies = None
if objects == 'ALL':
bodies = []
for ob in context.view_layer.objects:
if hasCollision(operator, ob, shapeFunctions.keys()):
bodies.append(ob)
elif objects == 'SELECTED':
bodies = []
for ob in context.view_layer.objects:
if ob.select_get() and hasCollision(operator, ob, shapeFunctions.keys()):
bodies.append(ob)
elif objects == 'CHILDREN':
bodies = set()
for ob in context.view_layer.objects:
if ob.select_get():
if hasCollision(operator, ob, shapeFunctions.keys()): bodies.add(ob)
addChildrenToSet(operator, ob, bodies, shapeFunctions.keys())
# Nothing to export
if len(bodies)==0:
print("No collision to export.")
operator.report( {'WARNING'}, "No collision selected for export")
return {'CANCELLED'}
# Calculate root transform
root = Matrix()
if transform == 'ACTIVE':
root = bpy.context.scene.objects.active.matrix_world.inverted()
elif transform == 'PARENT':
# Locate common parent
parent = None
for o in bodies:
if not parent: parent = o.parent
else: parent = commonParent(parent, o)
if not parent: break
if parent:
root = parent.matrix_world.inverted()
# Write xml
from xml.dom.minidom import Document
xDoc = Document()
xRoot = xDoc.createElement('PhysX30Collection')
xRoot.setAttribute('version', '3.4.0')
# Material
materialID = '1893755024'
xMaterial = xDoc.createElement('PxMaterial')
appendTextNode(xMaterial, 'Id', materialID)
appendTextNode(xMaterial, 'DynamicFriction', '0.3')
appendTextNode(xMaterial, 'StaticFriction', '0.3')
appendTextNode(xMaterial, 'Restitution', '0.5')
appendTextNode(xMaterial, 'FrictionCombineMode', 'eAVERAGE')
appendTextNode(xMaterial, 'RestitutionCombineMode', 'eAVERAGE')
xDoc.appendChild(xRoot)
xRoot.appendChild(xMaterial)
r = filepath.rfind('/') + 1
if r == 0: r = filepath.rfind('\\') + 1
title = filepath[r:]
# All shapes in a single actor? Make this an option. Also be good to use: body.rigid_body.type == { 'ACTIVE', 'PASSIVE' }
# and perhaps some material properties from the rigid boty rather than hard coded here
xActor= xDoc.createElement('PxRigidDynamic' if dynamicObjects else 'PxRigidStatic')
appendTextNode(xActor, 'Id', str(hash(title)&0xefffffff))
appendTextNode(xActor, 'Name', title)
appendTextNode(xActor, 'ActorFlags', 'eVISUALIZATION')
appendTextNode(xActor, 'GlobalPose', '0 0 0 1 0 0 0') # I assume this is quaternion.xyzw position.xyz
xShapes = xDoc.createElement('Shapes')
xRoot.appendChild(xActor)
xActor.appendChild(xShapes)
for body in bodies:
saveShape = shapeFunctions[ body.rigid_body.collision_shape ]
saveShape(xRoot, xShapes, body, root)
# Write xml file
data = xDoc.toprettyxml(indent=' ')
f = open( filepath, 'wb' )
f.write( bytes(data,'utf-8') )
f.close()
print("done.")
return {'FINISHED'}