Skip to content

Commit

Permalink
use binary command structure for servoanim export
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Dec 26, 2023
1 parent 188486e commit 87a5fdd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
5 changes: 3 additions & 2 deletions addon/ops/arduino_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ArduinoExport(Operator, BaseExport, ExportHelper):
)
)

def export(self, positions, context):
def export(self, positions, filepath, context):
variable_type = 'int' if self.precision == 0 else 'float'
fps, frames, seconds = self.get_time_meta(context.scene)
filename = self.get_blend_filename()
Expand Down Expand Up @@ -93,4 +93,5 @@ def export(self, positions, context):
if self.namespace:
content += f"\n}} // namespace {context.scene.name}\n"

return content
with open(filepath, 'w', encoding='utf-8') as file:
file.write(content)
16 changes: 12 additions & 4 deletions addon/ops/base_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


class BaseExport:
COMMAND_START = 0x3C
COMMAND_END = 0x3E

precision: bpy.props.IntProperty(
name="Precision",
description="The number of decimal digits to round to",
Expand Down Expand Up @@ -33,7 +36,7 @@ def execute(self, context):

try:
positions = calculate_positions(context, self.precision)
content = self.export(positions, context)
self.export(positions, self.filepath, context)
except RuntimeError as error:
self.report({'ERROR'}, str(error))

Expand All @@ -44,16 +47,21 @@ def execute(self, context):
if original_live_mode is True:
bpy.ops.servo_animation.start_live_mode('INVOKE_DEFAULT')

with open(self.filepath, 'w', encoding='utf-8') as file:
file.write(content)

end = time.time()
duration = round(end - start)
self.report(
{'INFO'}, f"Animation servo positions exported after {duration} seconds")

return {'FINISHED'}

@classmethod
def get_command(cls, servo_id, position):
command = [cls.COMMAND_START, servo_id]
command += position.to_bytes(2, 'big')
command += [cls.COMMAND_END]

return command

@staticmethod
def get_time_meta(scene):
fps = scene.render.fps
Expand Down
7 changes: 5 additions & 2 deletions addon/ops/json_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class JsonExport(Operator, BaseExport, ExportHelper):
maxlen=255
)

def export(self, positions, context):
def export(self, positions, filepath, context):
fps, frames, seconds = self.get_time_meta(context.scene)
filename = self.get_blend_filename()

Expand All @@ -45,4 +45,7 @@ def export(self, positions, context):
"servos": servos
}

return json.dumps(data, indent=4)
content = json.dumps(data, indent=4)

with open(filepath, 'w', encoding='utf-8') as file:
file.write(content)
18 changes: 9 additions & 9 deletions addon/ops/servoanim_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ class ServoanimExport(Operator, BaseExport, ExportHelper):
maxlen=255
)

def export(self, positions, context):
fps, frames, seconds = self.get_time_meta(context.scene)
positions_keys = list(positions.keys())
ids = ",".join(str(x) for x in positions_keys)
LINE_BREAK = 10

content = f"fps:{fps} frames:{frames} seconds:{seconds} ids:{ids}\n\n"
def export(self, positions, filepath, context):
_fps, frames, _seconds = self.get_time_meta(context.scene)
content = []

for frame in range(frames):
for servo_id in range(255):
if servo_id not in positions:
continue
pos = positions[servo_id][frame]
content += str(pos) + " "
content = content[:-1] + "\n"
position = positions[servo_id][frame]
content += self.get_command(servo_id, position)
content.append(self.LINE_BREAK)

return content
with open(filepath, 'wb') as file:
file.write(bytes(content))
Binary file modified examples/IK/ik.servoanim
Binary file not shown.

0 comments on commit 87a5fdd

Please sign in to comment.