Skip to content

Commit

Permalink
v0.0.4 - Fix issue where mapping to insert key frame would stop mappi…
Browse files Browse the repository at this point in the history
…ng to selection groups to work
  • Loading branch information
EldinZenderink committed Sep 7, 2024
1 parent effba1f commit 41422f3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
71 changes: 36 additions & 35 deletions MidiControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ class ControllerButtonBindingState:

# Controller to register keyframe(s) (note: all properties)
key_frame_control = None
bind_control_state = ControllerButtonBindingState.NONE
button_velocity_pressed = 0
key_frame_bind_control_state = ControllerButtonBindingState.NONE
keyframe_insert_button_velocity_pressed = 0

# Selection group buttons bound
selection_to_map = None
bind_selection_state = ControllerButtonBindingState.NONE
select_group_bind_selection_state = ControllerButtonBindingState.NONE
controller_selection_mapping = {}
button_velocity_pressed = 0
select_group_button_velocity_pressed = 0

# Frame position update
controllers_to_set_frame = {
Expand Down Expand Up @@ -211,6 +211,7 @@ def obj_prop_change_update(self):
new_obj['value'] = getattr(obj, prop)
self.current_object_data[f"{prop}"] = copy.copy(
new_obj)
self.mapping_error = None
else:
self.mapping_error = f"Failed parsing properties, too many!"

Expand Down Expand Up @@ -273,6 +274,7 @@ def obj_prop_change_update(self):
new_obj["type"] = str(type(value))
self.current_object_data[prop] = copy.copy(
new_obj)
self.mapping_error = None
else:
self.mapping_error = f"Failed parsing properties, too many!"

Expand Down Expand Up @@ -311,13 +313,14 @@ def frame_update(self):
self.controllers_to_set_frame_current_frame = bpy.context.scene.frame_current

def redraw_ui(self):
for screen in self.screens:
try:
if self.screens == None:
return
try:
for screen in self.screens:
for area in screen.areas:
area.tag_redraw()
except Exception as e:
print(e)
print("Screen error")
except Exception as e:
print("Screen error")

def update_data(self, mapping, new_value):
for obj in bpy.context.selected_objects:
Expand Down Expand Up @@ -402,7 +405,7 @@ def save_to_blend(self):
"controller_names": self.controller_names,
"controller_mapping": self.controller_property_mapping,
"selection_groups": self.controller_selection_mapping,
"controller_keyframe_bind": {"controller": self.key_frame_control, "velocity": self.button_velocity_pressed}
"controller_keyframe_bind": {"controller": self.key_frame_control, "velocity": self.keyframe_insert_button_velocity_pressed}
}
try:
setattr(self.context.scene, 'midicontrol_data', json.dumps(to_save))
Expand All @@ -419,7 +422,7 @@ def load_from_blend(self):
self.controller_selection_mapping = json_object["selection_groups"]
self.key_frame_control = json_object[
"controller_keyframe_bind"]["controller"]
self.button_velocity_pressed = json_object[
self.keyframe_insert_button_velocity_pressed = json_object[
"controller_keyframe_bind"]["velocity"]
self.loaded_from_blend = True
except Exception as e:
Expand All @@ -436,38 +439,36 @@ def midi_callback(self, midi_data):
value = midi_data[0][2]

if velocity != self.midi_last_control_velocity:
if self.bind_control_state == self.ControllerButtonBindingState.PENDING:
if self.key_frame_bind_control_state == self.ControllerButtonBindingState.PENDING:
self.key_frame_control = control

if str(control) == str(self.key_frame_control):
if self.button_velocity_pressed == 0:
self.button_velocity_pressed = velocity
self.bind_control_state = self.ControllerButtonBindingState.NONE

self.keyframe_insert_button_velocity_pressed = velocity
# self.save_to_blend()
self.key_frame_bind_control_state = self.ControllerButtonBindingState.BOUND

elif self.select_group_bind_selection_state == self.ControllerButtonBindingState.PENDING:
new_selection_mapping = {
"name": self.selection_to_map["name"],
"selected_objects": self.selection_to_map["selected"],
"velocity": velocity
}
self.controller_selection_mapping[str(
control)] = new_selection_mapping
self.select_group_button_velocity_pressed = velocity

self.select_group_bind_selection_state = self.ControllerButtonBindingState.BOUND

elif self.key_frame_bind_control_state == self.ControllerButtonBindingState.BOUND and \
velocity == self.keyframe_insert_button_velocity_pressed and \
self.key_frame_control == control:
self.insert_keyframes()

elif velocity == self.button_velocity_pressed:
self.insert_keyframes()

if self.bind_selection_state == self.ControllerButtonBindingState.PENDING:
if self.button_velocity_pressed == 0:
new_selection_mapping = {
"name": self.selection_to_map["name"],
"selected_objects": self.selection_to_map["selected"],
"velocity": velocity
}
self.controller_selection_mapping[str(
control)] = new_selection_mapping
self.bind_selection_state = self.ControllerButtonBindingState.NONE
# self.save_to_blend()

else:
elif self.select_group_bind_selection_state == self.ControllerButtonBindingState.BOUND and \
velocity == self.select_group_button_velocity_pressed:
if str(control) in self.controller_selection_mapping:
self.select_objects(
self.controller_selection_mapping[str(control)]["selected_objects"])

if str(control) == str(self.key_frame_control) and velocity == self.button_velocity_pressed:
self.insert_keyframes()

self.midi_last_control_velocity = velocity

Expand Down
12 changes: 6 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ def execute(self, context):
midi_control = scene.MidiControl

if self.start:
midi_control.bind_control_state = midi_control.ControllerButtonBindingState.PENDING
midi_control.key_frame_bind_control_state = midi_control.ControllerButtonBindingState.PENDING
elif self.reset:
midi_control.bind_control_state = midi_control.ControllerButtonBindingState.NONE
midi_control.key_frame_bind_control_state = midi_control.ControllerButtonBindingState.NONE
midi_control.key_frame_control = None
# Very cheeky
# midi_control.save_to_blend()
Expand Down Expand Up @@ -270,10 +270,10 @@ def execute(self, context):
"name": self.name
}
midi_control.selection_to_map = copy.copy(to_map)
midi_control.bind_selection_state = midi_control.ControllerButtonBindingState.PENDING
midi_control.select_group_bind_selection_state = midi_control.ControllerButtonBindingState.PENDING
else:
midi_control.selection_to_map = None
midi_control.bind_selection_state = midi_control.ControllerButtonBindingState.NONE
midi_control.select_group_bind_selection_state = midi_control.ControllerButtonBindingState.NONE

# Very cheeky
# midi_control.save_to_blend()
Expand Down Expand Up @@ -538,7 +538,7 @@ def draw(self, context):
op.reset = False
op.start = True

if midi_control.bind_control_state == midi_control.ControllerButtonBindingState.PENDING:
if midi_control.key_frame_bind_control_state == midi_control.ControllerButtonBindingState.PENDING:
row = box.row()
row.label(text="Press a button to bind!")

Expand Down Expand Up @@ -761,7 +761,7 @@ def draw(self, context):

if midi_control.midi_open:
layout.label(text="Map Current Selection")
if midi_control.bind_selection_state == midi_control.ControllerButtonBindingState.NONE:
if midi_control.select_group_bind_selection_state != midi_control.ControllerButtonBindingState.PENDING:
box = layout.box()
row = box.row()
row.label(text="Selection Group Name:")
Expand Down

0 comments on commit 41422f3

Please sign in to comment.