Hello :
In Short :
Failed to remove a shortcut with python like : remove Shift + 1
for move camera and add it to my own tool .
More Descriptions :
I am trying to remove a C4D shortcut and add it to my own tools , but it didn't work in my plugins :
I want my tool has 4 shortcut , like :
- Ctrl + Shift + Alt + 1 : Do A
- Ctrl + Shift + 1 : Do B
- Shift + 1 : Do C
- Ctrl + 1 : Do D
But some of them likeCtrl + 1
has employed . Here is little codes :
#--------------------------------------------------------------------
# Plugin Class
#--------------------------------------------------------------------
### ========== Execute ========== ###
#? 缓存灯光
class CacheLight(LightSolo):
def __init__(self) -> None:
pass
# Override - Called when the plugin is selected by the user.
def Execute(self, doc=c4d.documents.BaseDocument):
self.CacheState()
return True
#? 重置灯光
class ResetLight(LightSolo):
def __init__(self) -> None:
pass
# Override - Called when the plugin is selected by the user.
def Execute(self, doc=c4d.documents.BaseDocument):
alllights = self.getAllLights()
for light in alllights:
self.ResetState(light) # 重置灯光
return True
#? 灯光组solo
class SoloLightGroup(LightSolo):
def __init__(self) -> None:
pass
# Override - Called when the plugin is selected by the user.
def Execute(self, doc=c4d.documents.BaseDocument):
self.SoloLightGroup() # 灯光组solo
return True
#? 灯光solo
class SoloLights(LightSolo):
def __init__(self) -> None:
pass
# Override - Called when the plugin is selected by the user.
def Execute(self, doc=c4d.documents.BaseDocument):
self.SoloLights() # 灯光solo
return True
#--------------------------------------------------------------------
# Shortcut.
#--------------------------------------------------------------------
def RemoveShortcut(qualifier,key):
"""
Remove Shortcut by given qualifier and key
Args:
qualifier (int): modifier key
key (int): ascii code of key
"""
for x in range(c4d.gui.GetShortcutCount()):
shortcutBc = c4d.gui.GetShortcut(x)
# Check if shortcut is stored in the basecontainer.
if shortcutBc[0] == qualifier and shortcutBc[1] == key:
index = x
try:
c4d.gui.RemoveShortcut(index)
except:
print ("Shortcut {} remove failed".format(c4d.gui.Shortcut2String(qualifier, key)))
return False
def AddShortCut(qualifier,key,pluginID):
"""
Add Shortcut by given qualifier and key to given ID
Args:
qualifier (int): modifier key
key (int): ascii code of key
pluginID (int): plugin ID
"""
for x in range(c4d.gui.GetShortcutCount()):
shortcutBc = c4d.gui.GetShortcut(x)
# Check if shortcut is stored in the basecontainer.
if shortcutBc[0] == qualifier and shortcutBc[1] == key:
if shortcutBc[c4d.SHORTCUT_PLUGINID] == pluginID:
print ("Shortcut {} is already Used for Command ID: {}".format(c4d.gui.Shortcut2String(qualifier, key), shortcutBc[c4d.SHORTCUT_PLUGINID]))
return
# Define shortcut container
bc = c4d.BaseContainer()
bc.SetInt32(c4d.SHORTCUT_PLUGINID, pluginID)
bc.SetLong(c4d.SHORTCUT_ADDRESS, 0)
bc.SetLong(c4d.SHORTCUT_OPTIONMODE, 0)
# User defined key
bc.SetLong(0, qualifier)
bc.SetLong(1, key)
return c4d.gui.AddShortcut(bc)
### ========== Register Plugin ========== ###
if __name__ == "__main__":
key = 49 # num 1 next ~
# remove shortcut reg with Cinema 4D for [move camera]
RemoveShortcut(7,key)
RemoveShortcut(4,key)
RemoveShortcut(1,key)
RemoveShortcut(3,key)
# Add own shortcut
AddShortCut(7,key,SUB_PLUNIGID_CACHE)
AddShortCut(4,key,SUB_PLUNIGID_SOLOG)
AddShortCut(1,key,PLUNIGID)
AddShortCut(3,key,SUB_PLUNIGID_RESET)
c4d.EventAdd()
# Plugins Register
iconfile = c4d.bitmaps.BaseBitmap()
iconfile.InitWith(ICONPATH)
c4d.plugins.RegisterCommandPlugin(
id = SUB_PLUNIGID_CACHE,
str = PLUNGINNAME + "Cache State",
info = c4d.PLUGINFLAG_HIDEPLUGINMENU, # hide
help = "Cache All Light Objects State",
dat = CacheLight(),
icon = iconfile
)
c4d.plugins.RegisterCommandPlugin(
id = SUB_PLUNIGID_RESET,
str = PLUNGINNAME + "Reset State",
info = c4d.PLUGINFLAG_HIDEPLUGINMENU, # hide
help = "Reset All Light Objects State",
dat = ResetLight(),
icon = iconfile
)
c4d.plugins.RegisterCommandPlugin(
id = SUB_PLUNIGID_SOLOG,
str = PLUNGINNAME + "Solo Group",
info = 0, # c4d.PLUGINFLAG_COMMAND_STICKY,
help = "Solo Selected Light Objects in Group",
dat = SoloLightGroup(),
icon = iconfile
)
c4d.plugins.RegisterCommandPlugin(
id = PLUNIGID,
str = TITLE,
info = c4d.PLUGINFLAG_HIDEPLUGINMENU, # hide
help = INFO,
dat = SoloLights(),
icon = iconfile
)