better ui and modal cancel interaction

- add range in ui when animation is checked
- fix rig target collection UI
- add camera frames in rig mode
- fix animated interpolation frame range in rig mode
- add redraw timer to refresh view
master
pullusb 2024-02-08 18:27:07 +01:00
parent 539f62f3df
commit 6707d693d6
5 changed files with 246 additions and 196 deletions

View File

@ -1,7 +1,7 @@
bl_info = { bl_info = {
"name": "gp interpolate", "name": "gp interpolate",
"author": "Christophe Seux, Samuel Bernou", "author": "Christophe Seux, Samuel Bernou",
"version": (0, 7, 0), "version": (0, 7, 1),
"blender": (3, 6, 0), "blender": (3, 6, 0),
"location": "Sidebar > Gpencil Tab > Interpolate", "location": "Sidebar > Gpencil Tab > Interpolate",
"description": "Interpolate Grease pencil strokes over 3D", "description": "Interpolate Grease pencil strokes over 3D",

View File

@ -48,7 +48,7 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
next : bpy.props.BoolProperty(name='Next', default=True, options={'SKIP_SAVE'}) next : bpy.props.BoolProperty(name='Next', default=True, options={'SKIP_SAVE'})
def apply_and_store(self): def apply_and_store(self):
self.store = [] # self.store = []
# item = (prop, attr, [new_val]) # item = (prop, attr, [new_val])
for item in self.store_list: for item in self.store_list:
prop, attr = item[:2] prop, attr = item[:2]
@ -64,6 +64,7 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
self.debug = False self.debug = False
self.status = 'START' self.status = 'START'
self.store_list = [] self.store_list = []
self.store = []
self.loop_count = 0 self.loop_count = 0
self.start = time() self.start = time()
self.scan_time = None self.scan_time = None
@ -71,17 +72,24 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
self.toolcol = None self.toolcol = None
self.gp = context.object self.gp = context.object
self.settings = context.scene.gp_interpo_settings self.settings = context.scene.gp_interpo_settings
self.frames_to_jump = None
self.cancelled = False self.cancelled = False
self._timer = context.window_manager.event_timer_add(0.01, window=context.window)
context.window_manager.modal_handler_add(self) context.window_manager.modal_handler_add(self)
self._timer = context.window_manager.event_timer_add(0.01, window=context.window)
context.area.header_text_set('Starting interpolation | Esc: Cancel')
return {'RUNNING_MODAL'} return {'RUNNING_MODAL'}
def exit_modal(self): def exit_modal(self, context, status='INFO', text=None):
bpy.context.window_manager.progress_end() # Pgs context.area.header_text_set(None)
wm = context.window_manager
wm.progress_end() # Pgs
wm.event_timer_remove(self._timer)
self.restore() self.restore()
if self.debug: if self.debug:
## show as solid ?
# if self.plane is not None:
# self.plane.display_type = 'SOLID'
if self.scan_time is not None: if self.scan_time is not None:
print(f"Paste'n'place time {time()-self.start - self.scan_time}s") print(f"Paste'n'place time {time()-self.start - self.scan_time}s")
else: else:
@ -93,28 +101,37 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
bpy.data.collections.remove(self.toolcol) bpy.data.collections.remove(self.toolcol)
cancel_state = '(Stopped!) ' if self.cancelled else '' cancel_state = '(Stopped!) ' if self.cancelled else ''
self.report({'INFO'}, f'{cancel_state}{self.loop_count} interpolated frame(s) ({time()-self.start:.3f}s)') mess = f'{cancel_state}{self.loop_count} interpolated frame(s) ({time()-self.start:.3f}s)'
if text:
print(mess)
self.report({status}, text)
else:
## report standard info
if self.loop_count > 1:
self.report({'INFO'}, mess)
def modal(self, context, event): def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}: if event.type in {'RIGHTMOUSE', 'ESC'}:
print('Cancelling') print('Cancelling')
self.status = 'CANCELLED'
self.cancelled = True self.cancelled = True
self.exit_modal() context.area.header_text_set(f'Cancelling')
self.exit_modal(context)
return {'CANCELLED'} return {'CANCELLED'}
if event.type == 'TIMER': if self.frames_to_jump:
frame_num = len(self.frames_to_jump)
percentage = (self.loop_count) / (frame_num) * 100
context.area.header_text_set(f'Interpolation {percentage:.0f}% {self.loop_count + 1}/{frame_num} | Esc: Cancel')
# (frame: {self.frames_to_jump[self.loop_count]})
if self.status == 'START': if self.status == 'START':
scn = bpy.context.scene scn = bpy.context.scene
# auto_key_status = context.tool_settings.use_keyframe_insert_auto
# context.tool_settings.use_keyframe_insert_auto = True
## Determine on what key/keys to jump ## Determine on what key/keys to jump
self.frames_to_jump = following_keys(forward=self.next, all_keys=self.settings.use_animation) self.frames_to_jump = following_keys(forward=self.next, all_keys=self.settings.use_animation)
if not len(self.frames_to_jump): if not len(self.frames_to_jump):
self.report({'WARNING'}, 'No keyframe available in this direction') self.exit_modal(context, status='WARNING', text='No keyframe available in this direction')
return {'CANCELLED'} return {'CANCELLED'}
# print('self.frames_to_jump: ', self.frames_to_jump) # print('self.frames_to_jump: ', self.frames_to_jump)
@ -130,6 +147,12 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
col = scn.collection col = scn.collection
# print('----') # print('----')
if not self.gp.data.layers.active:
self.exit_modal(context, status='ERROR', text='No active layer')
return {'CANCELLED'}
if not self.gp.data.layers.active.active_frame:
self.exit_modal(context, status='ERROR', text='No active frame')
return {'CANCELLED'}
tgt_strokes = [s for s in self.gp.data.layers.active.active_frame.strokes if s.select] tgt_strokes = [s for s in self.gp.data.layers.active.active_frame.strokes if s.select]
@ -140,7 +163,7 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
tgt_strokes = self.gp.data.layers.active.active_frame.strokes tgt_strokes = self.gp.data.layers.active.active_frame.strokes
if not tgt_strokes: if not tgt_strokes:
self.report({'ERROR'}, 'No stroke selected !') self.exit_modal(context, status='ERROR', text='No stroke selected !')
return {'CANCELLED'} return {'CANCELLED'}
included_cols = [c.name for c in self.gp.users_collection] included_cols = [c.name for c in self.gp.users_collection]
@ -148,7 +171,7 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
if self.settings.method == 'BONE': if self.settings.method == 'BONE':
if not self.settings.target_rig or not self.settings.target_bone: if not self.settings.target_rig or not self.settings.target_bone:
self.report({'ERROR'}, 'No Bone selected') self.exit_modal(context, status='ERROR', text='No Bone selected')
return {'CANCELLED'} return {'CANCELLED'}
included_cols.append('interpolation_tool') included_cols.append('interpolation_tool')
@ -180,7 +203,7 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
elif self.settings.method == 'OBJECT': elif self.settings.method == 'OBJECT':
if not self.settings.target_object: if not self.settings.target_object:
self.report({'ERROR'}, 'No Object selected') self.exit_modal(context, status='ERROR', text='No Object selected')
return {'CANCELLED'} return {'CANCELLED'}
col = scn.collection # Reset collection filter col = scn.collection # Reset collection filter
target_obj = self.settings.target_object target_obj = self.settings.target_object
@ -196,18 +219,18 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
# (bpy.context.scene.render, 'simplify_subdivision', 0), # (bpy.context.scene.render, 'simplify_subdivision', 0),
] ]
# TODO: for now, the collection filter is not used at all in GEOMETRY mode # TODO: collection filter for GEOMETRY mode optimization
# it can be used to hide collection for faster animation mode
if self.settings.method == 'BONE': ## Hide optimizations (safe for Bone Mode only, if no error)
## TEST: Add collections containing rig (cannot be excluded) # if self.settings.method == 'BONE':
# rig_parent_cols = [c.name for c in scn.collection.children_recursive if self.settings.target_rig.name in c.all_objects] # ## TEST: Add collections containing rig (cannot be excluded)
# included_cols += rig_parent_cols # # rig_parent_cols = [c.name for c in scn.collection.children_recursive if self.settings.target_rig.name in c.all_objects]
for vlc in context.view_layer.layer_collection.children: # # included_cols += rig_parent_cols
self.store_list.append( # for vlc in context.view_layer.layer_collection.children:
# (vlc, 'exclude', vlc.name not in included_cols), # If excluded rig does not update ! # self.store_list.append(
(vlc, 'hide_viewport', vlc.name not in included_cols), # viewport viz # # (vlc, 'exclude', vlc.name not in included_cols), # If excluded rig does not update !
) # (vlc, 'hide_viewport', vlc.name not in included_cols), # viewport viz
# )
# print(f'Preparation {time()-start:.4f}s') # print(f'Preparation {time()-start:.4f}s')
## Set everything in SETUP list ## Set everything in SETUP list
@ -276,7 +299,7 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
s.select = sid == si s.select = sid == si
for ip, p in enumerate(stroke.points): for ip, p in enumerate(stroke.points):
p.select = ip == i p.select = ip == i
self.report({'ERROR'}, f'Stroke {si} point {i} could not find underlying geometry') self.exit_modal(context, status='ERROR', text=f'Stroke {si} point {i} could not find underlying geometry')
return {'CANCELLED'} return {'CANCELLED'}
stroke_data.append((stroke, point_co_world, object_hit, hit_location, tri, tri_indices)) stroke_data.append((stroke, point_co_world, object_hit, hit_location, tri, tri_indices))
@ -294,7 +317,11 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
bpy.context.window_manager.progress_begin(self.frames_to_jump[0], self.frames_to_jump[-1]) # Pgs bpy.context.window_manager.progress_begin(self.frames_to_jump[0], self.frames_to_jump[-1]) # Pgs
self.status = 'LOOP' self.status = 'LOOP'
elif self.status == 'LOOP':
## -- LOOPTIMER
if event.type == 'TIMER':
if self.status == 'LOOP':
f = self.frames_to_jump[self.loop_count] f = self.frames_to_jump[self.loop_count]
bpy.context.window_manager.progress_update(f) # Pgs bpy.context.window_manager.progress_update(f) # Pgs
scn = bpy.context.scene scn = bpy.context.scene
@ -385,9 +412,12 @@ class GP_OT_interpolate_stroke(bpy.types.Operator):
self.loop_count += 1 self.loop_count += 1
if self.loop_count >= len(self.frames_to_jump): if self.loop_count >= len(self.frames_to_jump):
self.exit_modal() self.exit_modal(context)
return {'FINISHED'} return {'FINISHED'}
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
# context.area.tag_redraw()
return {'RUNNING_MODAL'} return {'RUNNING_MODAL'}

View File

@ -45,7 +45,7 @@ class GP_PG_interpolate_settings(PropertyGroup):
items= ( items= (
('FRAME', 'Frame', 'prev/next scene frame depending on the padding options', 0), ('FRAME', 'Frame', 'prev/next scene frame depending on the padding options', 0),
('GPKEY', 'GP Key', 'prev/next Grease pencil key', 1) , ('GPKEY', 'GP Key', 'prev/next Grease pencil key', 1) ,
('RIGKEY', 'Rig Key', 'prev/next armatures keys in targeted collection', 2), ('RIGKEY', 'Rig Key', 'prev/next armatures keys in targeted collection (camera keys are included)', 2),
), ),
default='FRAME', default='FRAME',
description='Select how the previous or next frame should be chosen' description='Select how the previous or next frame should be chosen'
@ -59,8 +59,8 @@ class GP_PG_interpolate_settings(PropertyGroup):
target_collection : PointerProperty( target_collection : PointerProperty(
name='Collection', name='Collection',
type=bpy.types.Collection, type=bpy.types.Collection,
description='Target collection to check armature keyframes from', description='Target collection to get armature keyframes\
# placeholder='Collection' \nNothing = every armature in sccene',
) )
target_rig : PointerProperty( target_rig : PointerProperty(

23
ui.py
View File

@ -6,9 +6,9 @@ class GP_PT_interpolate(bpy.types.Panel):
bl_space_type = 'VIEW_3D' bl_space_type = 'VIEW_3D'
bl_region_type = 'UI' bl_region_type = 'UI'
@classmethod # @classmethod
def poll(cls, context): # def poll(cls, context):
return context.object # and context.object.type == 'GPENCIL' # return context.object
def draw(self, context): def draw(self, context):
settings = bpy.context.scene.gp_interpo_settings settings = bpy.context.scene.gp_interpo_settings
@ -22,10 +22,20 @@ class GP_PT_interpolate(bpy.types.Panel):
prev_icon, next_icon = 'FRAME_PREV', 'FRAME_NEXT' prev_icon, next_icon = 'FRAME_PREV', 'FRAME_NEXT'
else: else:
prev_icon, next_icon = 'PREV_KEYFRAME', 'NEXT_KEYFRAME' prev_icon, next_icon = 'PREV_KEYFRAME', 'NEXT_KEYFRAME'
prev_text = next_text = ''
scn = context.scene
if settings.use_animation:
prev_icon, next_icon = 'PLAY_REVERSE', 'PLAY'
## Show animation range:
prev_text = f'{scn.frame_preview_start if scn.use_preview_range else scn.frame_start} < {scn.frame_current}'
next_text = f'{scn.frame_current} > {scn.frame_preview_end if scn.use_preview_range else scn.frame_end}'
row = col.row(align=True) row = col.row(align=True)
row.scale_x = 3 row.scale_x = 3
row.operator("gp.interpolate_stroke", text="", icon=prev_icon).next = False row.operator("gp.interpolate_stroke", text=prev_text, icon=prev_icon).next = False
row.operator("gp.interpolate_stroke", text="", icon=next_icon).next = True row.operator("gp.interpolate_stroke", text=next_text, icon=next_icon).next = True
col.prop(settings, 'use_animation', text='Animation') col.prop(settings, 'use_animation', text='Animation')
col.prop(settings, 'method', text='Method') col.prop(settings, 'method', text='Method')
@ -39,7 +49,6 @@ class GP_PT_interpolate(bpy.types.Panel):
col.prop(settings, 'use_bone_rotation', text='Use Bone Rotation') col.prop(settings, 'use_bone_rotation', text='Use Bone Rotation')
elif settings.method == 'GEOMETRY': elif settings.method == 'GEOMETRY':
col.prop(settings, 'target_collection', text='Collection')
col.prop(settings, 'search_range') col.prop(settings, 'search_range')
col.prop(settings, 'remove_occluded') col.prop(settings, 'remove_occluded')
@ -54,6 +63,8 @@ class GP_PT_interpolate(bpy.types.Panel):
if settings.mode == 'FRAME': if settings.mode == 'FRAME':
col.prop(settings, 'padding') col.prop(settings, 'padding')
if settings.mode == 'RIGKEY':
col.prop(settings, 'target_collection', text='Collection')
## Parent still full WIP ## Parent still full WIP
# col = layout.column() # col = layout.column()
# col.label(text='Layer Parent') # col.label(text='Layer Parent')

View File

@ -191,6 +191,8 @@ def plane_on_bone(bone, arm=None, cam=None, set_rotation=True, mesh=True):
plane = bpy.data.objects.get('interpolation_plane') plane = bpy.data.objects.get('interpolation_plane')
if not plane: if not plane:
plane = create_plane(name='interpolation_plane') plane = create_plane(name='interpolation_plane')
# Display type as Wire for a discrete XP
plane.display_type = 'WIRE'
if plane.name not in col.objects: if plane.name not in col.objects:
col.objects.link(plane) col.objects.link(plane)
@ -395,17 +397,17 @@ def following_keys(forward=True, all_keys=False) -> list:# -> list[int] | list |
cur_frame = bpy.context.scene.frame_current cur_frame = bpy.context.scene.frame_current
settings = bpy.context.scene.gp_interpo_settings settings = bpy.context.scene.gp_interpo_settings
if settings.mode == 'FRAME':
jump = settings.padding * direction
if all_keys:
scn = bpy.context.scene scn = bpy.context.scene
if forward: if forward:
limit = scn.frame_preview_end if scn.use_preview_range else scn.frame_end limit = scn.frame_preview_end if scn.use_preview_range else scn.frame_end
else: else:
limit = scn.frame_preview_start if scn.use_preview_range else scn.frame_start limit = scn.frame_preview_start if scn.use_preview_range else scn.frame_start
frames = []
if settings.mode == 'FRAME':
jump = settings.padding * direction
if all_keys:
limit += direction # offset by one for limit to be in range limit += direction # offset by one for limit to be in range
return list(range(cur_frame + jump , limit, jump)) return list(range(cur_frame + jump , limit, jump))
else: else:
@ -419,10 +421,17 @@ def following_keys(forward=True, all_keys=False) -> list:# -> list[int] | list |
col = settings.target_collection col = settings.target_collection
if not col: if not col:
col = bpy.context.scene.collection col = bpy.context.scene.collection
for arm in [o for o in col.all_objects if o.type == 'ARMATURE']:
if not arm.animation_data or not arm.animation_data.action: objs = [o for o in col.all_objects if o.type == 'ARMATURE']
# Add camera moves detection
objs += [bpy.context.scene.camera]
for obj in objs:
print(obj.name)
if not obj.animation_data or not obj.animation_data.action:
continue continue
frames = [k.co.x for fc in arm.animation_data.action.fcurves for k in fc.keyframe_points] frames += [k.co.x for fc in obj.animation_data.action.fcurves for k in fc.keyframe_points]
if not frames: if not frames:
return [] return []
@ -433,9 +442,9 @@ def following_keys(forward=True, all_keys=False) -> list:# -> list[int] | list |
if all_keys: if all_keys:
frames = list(set(frames)) frames = list(set(frames))
if forward: if forward:
frame_list = [int(f) for f in frames if f > cur_frame] frame_list = [int(f) for f in frames if f > cur_frame and f <= limit]
else: else:
frame_list = [int(f) for f in frames if f < cur_frame] frame_list = [int(f) for f in frames if f < cur_frame and f >= limit]
return frame_list return frame_list
if forward: if forward: