forked from NTUEELightDance/LightDance-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EDITOR-NTUEELightDance#535 [BLENDER] Dancer name tag (NTUEELightDance…
…#536) * added dancer name tag * disabled deleting objects
- Loading branch information
1 parent
555972a
commit 26d45c0
Showing
4 changed files
with
217 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
from . import animation, objects, waveform | ||
from . import animation, keymap, name_tag, objects, waveform | ||
|
||
|
||
def mount_handlers(): | ||
animation.mount() | ||
objects.mount() | ||
waveform.mount() | ||
name_tag.mount() | ||
keymap.mount() | ||
|
||
|
||
def unmount_handlers(): | ||
animation.unmount() | ||
objects.unmount() | ||
waveform.unmount() | ||
name_tag.unmount() | ||
keymap.unmount() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from typing import Dict, List, Optional, cast | ||
|
||
import bpy | ||
|
||
default_clipboard_keymaps = ["view3d.copybuffer", "view3d.pastebuffer"] | ||
default_delete_keymaps = { | ||
"Object Mode": ["object.delete", "anim.keyframe_delete_v3d"], | ||
"Dopesheet": ["action.delete"], | ||
"Outliner": ["outliner.delete", "anim.keyframe_delete"], | ||
"User Interface": ["anim.keyframe_delete_button"], | ||
} | ||
|
||
|
||
def check_keymaps_exist( | ||
keymaps: List[bpy.types.KeyMapItem], | ||
names: List[str], | ||
ctrl: List[int], | ||
oskey: List[int], | ||
) -> List[Optional[bpy.types.KeyMapItem]]: | ||
wm = bpy.context.window_manager | ||
kc_items = cast(Dict[str, bpy.types.KeyMap], wm.keyconfigs.default.keymaps)[ | ||
"3D View" | ||
].keymap_items | ||
kc_items = cast(List[bpy.types.KeyMapItem], kc_items) | ||
|
||
results: List[Optional[bpy.types.KeyMapItem]] = [None] * len(names) | ||
|
||
for keymap in keymaps: | ||
for i in range(len(names)): | ||
if ( | ||
keymap.idname == names[i] | ||
and keymap.ctrl == ctrl[i] | ||
and keymap.oskey == oskey[i] | ||
): | ||
results[i] = keymap | ||
|
||
return results | ||
|
||
|
||
def mount(): | ||
wm = bpy.context.window_manager | ||
global default_keymaps | ||
default_keymaps = cast(List[bpy.types.KeyMapItem], []) | ||
# Disable delete keymaps | ||
for keymap_class, keymap_list in default_delete_keymaps.items(): | ||
km_items = cast(Dict[str, bpy.types.KeyMap], wm.keyconfigs.default.keymaps)[ | ||
keymap_class | ||
].keymap_items | ||
km_items = cast(List[bpy.types.KeyMapItem], km_items) | ||
for keymap in km_items: | ||
if keymap.idname in keymap_list: | ||
keymap.active = False | ||
default_keymaps.append(keymap) | ||
|
||
# Active clipboard keymaps and disable default keymaps | ||
|
||
view_3d_km_items = cast(Dict[str, bpy.types.KeyMap], wm.keyconfigs.default.keymaps)[ | ||
"3D View" | ||
].keymap_items | ||
view_3d_km_items = cast(List[bpy.types.KeyMapItem], view_3d_km_items) | ||
|
||
for keymap in view_3d_km_items: | ||
if keymap.idname in default_clipboard_keymaps: | ||
keymap.active = False | ||
default_keymaps.append(keymap) | ||
|
||
new_keymaps_config = ( | ||
[ | ||
"lightdance.copy", | ||
"lightdance.copy", | ||
"lightdance.paste", | ||
"lightdance.paste", | ||
], | ||
["C", "C", "V", "V"], | ||
[1, 0, 1, 0], | ||
[0, 1, 0, 1], | ||
) | ||
|
||
new_keymaps = check_keymaps_exist( | ||
view_3d_km_items, | ||
new_keymaps_config[0], | ||
new_keymaps_config[2], | ||
new_keymaps_config[3], | ||
) | ||
|
||
view_3d_km_items = cast(bpy.types.KeyMapItems, view_3d_km_items) | ||
for i in range(len(new_keymaps)): | ||
new_keymap = new_keymaps[i] | ||
|
||
if new_keymap is None: | ||
new_keymaps[i] = view_3d_km_items.new( | ||
new_keymaps_config[0][i], | ||
new_keymaps_config[1][i], | ||
ctrl=new_keymaps_config[2][i], | ||
oskey=new_keymaps_config[3][i], | ||
value="PRESS", | ||
) | ||
|
||
else: | ||
new_keymap.active = True | ||
|
||
global clipboard_keymaps | ||
|
||
clipboard_keymaps = cast(List[bpy.types.KeyMapItem], new_keymaps) | ||
|
||
|
||
def unmount(): | ||
for keymap in clipboard_keymaps: | ||
keymap.active = False | ||
for keymap in default_keymaps: | ||
keymap.active = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from typing import Any, List, Optional, Tuple, cast | ||
|
||
import blf | ||
import bpy | ||
from bpy_extras.view3d_utils import location_3d_to_region_2d | ||
from mathutils import Vector | ||
|
||
from ..core.states import state | ||
from ..core.utils.ui import redraw_area | ||
|
||
|
||
class NameTagSettings: | ||
def __init__(self): | ||
self.x_offset: float = 0 | ||
self.y_offset: float = 0 | ||
self.z_offset: float = 6.5 | ||
self.fontsize: int = 25 | ||
self.text_rgba: Tuple[float, float, float, float] = (1, 1, 1, 1) | ||
self.font_id: int = 0 | ||
self.name_tag_handle: Any = None | ||
self.name_tag_draw: Any = None | ||
self.region: Optional[bpy.types.Region] = None | ||
|
||
|
||
name_tag_settings = NameTagSettings() | ||
|
||
|
||
def name_tag_draw(): | ||
global name_tag_settings | ||
|
||
blf.size(name_tag_settings.font_id, name_tag_settings.fontsize) | ||
blf.color(name_tag_settings.font_id, *name_tag_settings.text_rgba) | ||
if name_tag_settings.region: | ||
region = name_tag_settings.region | ||
region_data = cast(bpy.types.RegionView3D, region.data) | ||
else: | ||
return | ||
|
||
dancer_names = state.dancer_names | ||
for name in dancer_names: | ||
dancer_obj = cast(bpy.types.Object, bpy.data.objects[name]) | ||
dancer_location = dancer_obj.location | ||
text_location_3d = Vector( | ||
( | ||
dancer_location[0] + name_tag_settings.x_offset, | ||
dancer_location[1] + name_tag_settings.y_offset, | ||
dancer_location[2] + name_tag_settings.z_offset, | ||
) | ||
) | ||
text_view_2d = location_3d_to_region_2d(region, region_data, text_location_3d) | ||
text_w, text_h = cast( | ||
Tuple[float, float], blf.dimensions(name_tag_settings.font_id, name) | ||
) | ||
blf.position( | ||
name_tag_settings.font_id, | ||
text_view_2d[0] - text_w / 2, | ||
text_view_2d[1] - text_h / 2, | ||
0, | ||
) | ||
blf.draw(name_tag_settings.font_id, name) | ||
|
||
|
||
def name_tag_handler(): | ||
global name_tag_settings | ||
if name_tag_settings.name_tag_draw is not None: | ||
bpy.types.SpaceView3D.draw_handler_remove( | ||
name_tag_settings.name_tag_handle, "WINDOW" | ||
) | ||
name_tag_settings.name_tag_draw = bpy.types.SpaceView3D.draw_handler_add( | ||
name_tag_draw, (), "WINDOW", "POST_PIXEL" | ||
) | ||
|
||
|
||
def mount(): | ||
global name_tag_settings | ||
screen = cast(bpy.types.Screen, bpy.data.screens["Layout"]) | ||
area = next( | ||
area | ||
for area in cast(List[bpy.types.Area], screen.areas) | ||
if area.type == "VIEW_3D" | ||
) | ||
region = next( | ||
region | ||
for region in cast(List[bpy.types.Region], area.regions) | ||
if region.type == "WINDOW" | ||
) | ||
name_tag_settings.region = region | ||
name_tag_settings.name_tag_handle = bpy.types.SpaceView3D.draw_handler_add( | ||
name_tag_handler, (), "WINDOW", "POST_PIXEL" | ||
) | ||
print("Name tag mounted") | ||
redraw_area({"VIEW_3D"}) | ||
|
||
|
||
def unmount(): | ||
global name_tag_settings | ||
if name_tag_settings.name_tag_handle is not None: | ||
bpy.types.SpaceView3D.draw_handler_remove( | ||
name_tag_settings.name_tag_handle, "WINDOW" | ||
) | ||
name_tag_settings.name_tag_handle = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters