简体   繁体   English

在 Blender 中运行 python 脚本

[英]Running python script in Blender

I installed Blender 2.6 and I'm trying to run a script called drawcar.py (Which uses PyOpenGL)我安装了 Blender 2.6,我正在尝试运行一个名为 drawcar.py 的脚本(使用 PyOpenGL)

I looked around the documentation for importing a script and could only access Blender's python console.我查看了导入脚本的文档,只能访问 Blender 的 python 控制台。

How do I run drawcar.py from the Linux terminal with Blender?如何使用 Blender 从 Linux 终端运行 drawcar.py?

You can also execute the following code in the python console to execute an external script without opening it up in the text editor:您还可以在python控制台中执行以下代码来执行外部脚本,而无需在文本编辑器中打开它:

filename = "/full/path/to/myscript.py"
exec(compile(open(filename).read(), filename, 'exec'))

The above code comes from the following link:以上代码来自以下链接:

Blender - Tips and Tricks Blender - 提示和技巧

  1. Open a Text Editor view in Blender.在 Blender 中打开文本编辑器视图。
  2. Press Alt + O, or go to Text>Open Text Block and open the .py file按 Alt + O,或转到 Text>Open Text Block 并打开 .py 文件
  3. Then simply press Run script :D然后只需按运行脚本:D

Ps Instead of opening a file in step 2, you can also hit the "+ New" button and create a new script instead. Ps 除了在第 2 步中打开文件,您还可以点击“+新建”按钮并创建一个新脚本。

Note : In newer versions the Run Script button label has been replaced with a Play icon :注意:在较新版本中,运行脚本按钮标签已替换为播放图标: 在此处输入图片说明

this answer is too late, but to help anyone with the same problem这个答案为时已晚,但可以帮助任何有同样问题的人

via the terminal:通过终端:

blender yourblendfilenameorpath --python drawcar.py 

from the man pages从手册页

       -P or --python <filename>
              Run the given Python script file.

To run a script by another script or from console:要通过另一个脚本或从控制台运行脚本:

import bpy

script = bpy.data.texts["script_name.py"]
exec(script.as_string())

It is likely that drawcar.py is trying to perform pyOpenGL commands inside Blender, and that won't work without modification.很可能 drawcar.py 正在尝试在 Blender 中执行 pyOpenGL 命令,如果不修改它就无法工作。 I suspect you are getting some import errors too (if you look at the command console).我怀疑您也遇到了一些导入错误(如果您查看命令控制台)。 Blender has it's own internal python wrapper for opengl called bgl , which does include a lot of the opengl standards, but all prefixed by bgl. Blender 有它自己的 opengl 内部 python 包装器bgl ,它确实包含很多 opengl 标准,但都以 bgl 为前缀。

If you have a link to drawcar.py I can have a look at it and tell you what's going on.如果您有 drawcar.py 的链接,我可以查看它并告诉您发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM