如何在Maya首次加载时自动执行python脚本
我正在尝试弄清楚如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议您在调用该函数之前使用按钮导入 Python 模块。假设您的脚本位于 maya/scripts/tep.py 中,您的按钮将执行以下操作:
如果您想修改脚本并在每次点击按钮时继续运行新版本,请执行以下操作:
如果您希望模块在 Maya 启动时加载,在 Maya/scripts 目录中创建一个名为 userSetup.py 的文件并让它执行以下操作:
然后,您的按钮可以简单地:
或者...
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:
If you wanted to modify the script and keep running the fresh version every time you hit the button, do this:
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:
Then, your button can simply just:
Or...
作为 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 文件如下所示:(
根据 @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:
(edited to caps out userSetup.py as per @jdi's comment)
您运行的是哪个版本的 Maya?如果高于 8.5,Maya 内置了 python。您放入本地 Maya 脚本目录中的任何 python 脚本都会自动获取源。您可以在脚本编辑器源代码中运行 python 脚本。
自动运行:
希望有所帮助
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:
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.....
我喜欢使用
exec(open('c:\whatever\whatever\scriptname.py'))
看看这是否适合你! :)
I like to use
exec(open('c:\whatever\whatever\scriptname.py'))
See if that works for you! :)