Skip to content

Commit

Permalink
Fix #9, add .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
bartteunis committed Jan 25, 2022
1 parent 016258c commit 685fe53
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
40 changes: 20 additions & 20 deletions operator_rigidbody_export.py → __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"description": "Export the current scene's rigid body world to a json file",
"author": "Bart Teunis",
"version": (0, 7, 2),
"blender": (2, 80, 0),
"blender": (3, 0, 0),
"location": "File > Export",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "https://github.com/bartteunis/blender-rigidbody-exporter/wiki",
"doc_url": "https://github.com/bartteunis/blender-rigidbody-exporter/wiki",
"category": "Import-Export"}

import bpy
Expand All @@ -15,57 +15,57 @@
def write_scene_physics(context, exporter, filepath, selection_only):
s = context.scene
w = s.rigidbody_world

object_list = []
object_mapping = {} # Used to store object index in array <-> object name
constraint_list = []
world_settings = {}
result = {"objects": object_list, "constraints": constraint_list}

result["version"] = bl_info["version"]

result["world"] = {
"gravity": s.gravity[:],
"time_scale": w.time_scale,
"steps_per_second": w.steps_per_second,
"steps_per_second": w.substeps_per_frame,
"solver_iterations": w.solver_iterations,
}

# Determine what we need first
props = {'angular_damping','collision_shape','enabled','friction','kinematic','linear_damping','mass','restitution','type'}
oprops = {'location','rotation_euler','scale','dimensions','name'}

# Iterate over rigid body objects
if selection_only:
items = [obj for obj in w.collection.objects if obj in bpy.context.selected_objects]
else:
items = w.collection.objects

for obj in items:
body = obj.rigid_body

physics_settings = {x:body.path_resolve(x) for x in props}
physics_settings['collision_group'] = [i for i, x in enumerate(body.collision_collections) if x == True][0]
object_settings = {x:obj.path_resolve(x)[:] for x in oprops}

# Get reference to object data
d = obj.data

# Select the necessary stuff (multiple face loops)
physics_settings['coords'] = []
if body.collision_shape == 'MESH' or body.collision_shape == 'CONVEX_HULL':
for poly in d.polygons:
l = len(poly.loop_indices)
if (l < 3 or l > 8):
exporter.report({'WARNING'}, "Object: " + obj.name + " - Convex polygon shape's vertex count is less than 3 or more than 8")

vtx_indices = [d.loops[x].vertex_index for x in poly.loop_indices]
ordered_verts = [getattr(d.vertices[x].co,exporter.coordinates)[:] for x in vtx_indices]
physics_settings['coords'].append(ordered_verts)

object_list.append({'object': object_settings, 'physics': physics_settings})
object_mapping[obj.name] = len(object_list)-1 # Index in list

# Iterate over Empty's containing constraints
if w.constraints != None:
for obj in w.constraints.objects:
Expand All @@ -80,11 +80,11 @@ def write_scene_physics(context, exporter, filepath, selection_only):
"enabled": c.enabled
}
constraint_list.append(constraint_settings)

f_collision = open(filepath,'w')
json.dump(result,f_collision)
f_collision.close()

return {'FINISHED'}

# ExportHelper is a helper class, defines filename and
Expand All @@ -107,12 +107,12 @@ class ExportRigidBody(Operator, ExportHelper):
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)

selection_only : BoolProperty(
name="Selection Only",
description="Export selected only",
)

coordinates : EnumProperty(
name="Coordinate",
description="Which values to export",
Expand Down Expand Up @@ -146,4 +146,4 @@ def unregister():
register()

# test call
bpy.ops.export_rigidbody.box2d('INVOKE_DEFAULT')
bpy.ops.export_rigidbody.box2d('INVOKE_DEFAULT')

0 comments on commit 685fe53

Please # to comment.