如何在Maya首次加载时自动执行python脚本

发布于 2024-09-04 08:19:56 字数 415 浏览 4 评论 0原文

我正在尝试弄清楚如何在 Maya 中使用 Python。我想在 Maya 中创建一个架子,当我单击该架子时,它将执行一个包含 python 代码的文件。

首先,我发现我们不能简单地source python 脚本。我遵循了这个教程,所以现在我有了函数psource()。在我的架子上,我只需调用 psource("myPythonScript")

我的问题是我必须在 Maya 首次加载时以某种方式注册 psource()

知道如何做到这一点吗?

I am trying to figure out how to use Python in Maya. I wanted to create a shelf in Maya and when I click that shelf, it will execute a file containing python code.

First thing, I figured out that we can't simply source python script. I followed this tutorial, so now I have a function psource(). In my shelf, I can just call psource("myPythonScript")

My problem is I have to somehow register psource() when Maya first loaded.

Any idea how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝颜夕 2024-09-11 08:19:56

我建议您在调用该函数之前使用按钮导入 Python 模块。假设您的脚本位于 maya/scripts/tep.py 中,您的按钮将执行以下操作:

import tep
tep.psource()

如果您想修改脚本并在每次点击按钮时继续运行新版本,请执行以下操作:

import tep
reload(tep)
tep.psource()

如果您希望模块在 Maya 启动时加载,在 Maya/scripts 目录中创建一个名为 userSetup.py 的文件并让它执行以下操作:

import tep

然后,您的按钮可以简单地:

tep.psource()

或者...

reload(tep)
tep.psource()

I suggest that you import the Python module with your button before calling the function. Assuming your script is in maya/scripts/tep.py, your button would do the following:

import tep
tep.psource()

If you wanted to modify the script and keep running the fresh version every time you hit the button, do this:

import tep
reload(tep)
tep.psource()

And if you want your module to load on Maya startup, create a file called userSetup.py in your maya/scripts directory and have it do this:

import tep

Then, your button can simply just:

tep.psource()

Or...

reload(tep)
tep.psource()
无声情话 2024-09-11 08:19:56

作为 Maya 启动序列的一部分,它将为您执行一个名为 userSetup.py 的文件。在该文件中,您可以坚持使用标准 python 代码来设置环境等。

文档: http://download.autodesk.com/global/docs/maya2013/en_us/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d30e725143

这是 2013 年的 docco,但它在 2011 年和 2012 年也有效。我希望进一步回溯它也是正确的,但我在这里没有运行任何较旧的

内容例如,顺便说一句,我的 userSetup.py 文件如下所示:(

import sys

# import a separate pyscript dir - we keep the standard scriptdir for MEL
sys.path.append(r'C:/Users/tanantish/Documents/maya/2012-x64/pyscripts')

# odds on i'm going to want PyMEL loaded by default
# and we are going to try distinguish it from the old maya.cmds
# since the two since they're similar, but not the same.
# from pymel.core import *
import pymel.core as pm

# and we might as well get maya.cmds in for testing..
import maya.cmds as mc

# import local toolpack
import tantools

根据 @jdi 的评论编辑为上限 userSetup.py )

As part of the Maya startup sequence, it'll execute a file called userSetup.py for you. Within that file you can stick in standard python code to set up your environment, etc.

docs: http://download.autodesk.com/global/docs/maya2013/en_us/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d30e725143

That's the 2013 docco, but it's valid in 2011 and 2012 too. I expect it to be correct going back further as well, but I'm not running anything older here

For an example btw, my userSetup.py file looks like this:

import sys

# import a separate pyscript dir - we keep the standard scriptdir for MEL
sys.path.append(r'C:/Users/tanantish/Documents/maya/2012-x64/pyscripts')

# odds on i'm going to want PyMEL loaded by default
# and we are going to try distinguish it from the old maya.cmds
# since the two since they're similar, but not the same.
# from pymel.core import *
import pymel.core as pm

# and we might as well get maya.cmds in for testing..
import maya.cmds as mc

# import local toolpack
import tantools

(edited to caps out userSetup.py as per @jdi's comment)

蘑菇王子 2024-09-11 08:19:56

您运行的是哪个版本的 Maya?如果高于 8.5,Maya 内置了 python。您放入本地 Maya 脚本目录中的任何 python 脚本都会自动获取源。您可以在脚本编辑器源代码中运行 python 脚本。

自动运行:

  1. 在 myDocs\maya\mayaVersion\scripts 中创建 userSetup.mel 文件
  2. 在 userSetup 内部,使用以下语法导入和运行脚本:
python("from package import module");
python("module.method(\"passedVar1\", \"passedVar2\")");

希望有所帮助

P.S 相同的语法适用于架子按钮。只需确保为 Maya 设置了 python 路径,以便可以找到您的代码。本地脚本目录已经包含在内......

Which version of Maya are you running? If later than 8.5, Maya has python built in. Any python scripts you put in your local Maya script directory gets automatically sourced. You can inside the script editor source and run python scripts.

To automatically run:

  1. Create a userSetup.mel file in myDocs\maya\mayaVersion\scripts
  2. Inside the userSetup, use this syntax to import and run scripts:
python("from package import module");
python("module.method(\"passedVar1\", \"passedVar2\")");

Hope that helps

P.S Same syntax applies for shelf buttons. Just have to make sure that you have your python path set for Maya so that your code can be found. The local script directory is already included.....

弃爱 2024-09-11 08:19:56

我喜欢使用

exec(open('c:\whatever\whatever\scriptname.py'))

看看这是否适合你! :)

I like to use

exec(open('c:\whatever\whatever\scriptname.py'))

See if that works for you! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文