0

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

首先,我发现我们不能简单地使用sourcepython 脚本。我遵循了本教程,所以现在我有了一个函数psource()。在我的书架上,我可以打电话psource("myPythonScript")

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

知道怎么做吗?

4

4 回答 4

2

我建议您在调用函数之前使用按钮导入 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()
于 2011-08-30T14:37:12.877 回答
2

作为 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)

于 2012-10-05T11:41:17.953 回答
0

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.....

于 2010-09-09T21:53:26.813 回答
-2

我喜欢用

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

看看这是否适合你!:)

于 2010-08-22T20:56:49.420 回答