python 使用Python的Autodesk Maya热键

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python的Autodesk Maya热键相关的知识,希望对你有一定的参考价值。

from __future__ import print_function
from collections import namedtuple
from maya import cmds
import pymel.core as pmc
from maya.utils import executeDeferred
from functools import wraps


KeySequence = namedtuple('KeySequence', 'ctrl shift alt key full')


def command_exists(name):
    '''Check if a runTimeCommand exists'''

    return cmds.runTimeCommand(name, query=True, exists=True)


def parse_key_sequence(key_sequence):
    '''
    Returns a KeySequence object from a string like::

        "Ctrl+Alt+O"
        "ctrl+shift+m"
        "m"
        "alt+M"
    '''

    keys = KeySequence(False, False, False, '', key_sequence)
    key_sequence = key_sequence.lower()

    for modifier in ['ctrl', 'shift', 'alt']:
        group = modifier + '+'
        if group in key_sequence:
            keys = keys._replace(**{modifier: True})
            key_sequence = key_sequence.replace(group, '')
    keys = keys._replace(key=key_sequence)

    return keys


def defer(fn):
    '''Delays execution of the decorated function until Maya is idle.'''

    @wraps(fn)
    def deferred(*args, **kwargs):
        executeDeferred(fn, *args, **kwargs)
    return deferred


@defer
def create_hotkey(key_sequence, name, command, category='Custom Scripts', release_name=None, release_command=None):

    keys = parse_key_sequence(key_sequence)
    params = dict(
        keyShortcut=keys.key,
        ctrlModifier=keys.ctrl,
        altModifier=keys.alt,
        shiftModifier=keys.shift,
        name=name
    )

    if not command_exists(name):
        cmds.runTimeCommand(
            name,
            category=category,
            command=command,
            commandLanguage='python'
        )
        cmds.nameCommand(
            name,
            ann=name,
            sourceType='python',
            command=name
        )

    if release_name and release_command:
        if not command_exists(release_name):
            cmds.runTimeCommand(
                release_name,
                category=category,
                command=release_command,
                commandLanguage='python'
            )
            cmds.nameCommand(
                name,
                ann=name,
                sourceType='python',
                command=name
            )
        params['releaseName'] = release_name

    pmc.hotkey(**params)

create_hotkey('Ctrl+Alt+H', 'helloWorld', 'print("Hello World!")')

以上是关于python 使用Python的Autodesk Maya热键的主要内容,如果未能解决你的问题,请参考以下文章

使用 python 自动化 Autodesk Inventor

Autodesk 的 Fbx Python 和线程

如何在 Python 脚本中为 Autodesk Maya 2016 中的对象添加颜色?

如何为 Autodesk Maya OSX 将 py 文件编译为 .so

Autodesk Forge - 使用 curl 下载文件

Autodesk Forge - 从 .ipt 转换为 .stl 出现错误 (UnicodeEncodeError)