42

I installed Blender 2.6 and I'm trying to run a script called drawcar.py (Which uses PyOpenGL)

I looked around the documentation for importing a script and could only access Blender's python console.

How do I run drawcar.py from the Linux terminal with Blender?

CyberShot
  • 2,265
  • 6
  • 28
  • 36
  • _For sake of completeness_ (since this SO page is high on Googles results) -- there is a comprehensive and canonical introduction page in the official documentation. Just as usual, it is somewhat hidden :-D [Python API Overview¶](https://docs.blender.org/api/blender_python_api_current/info_overview.html) This page describes how Python is integrated and lists all the ways to start python scripts or integrate as extension. – Ichthyo Feb 09 '19 at 02:34

5 Answers5

39

You can also execute the following code in the python console to execute an external script without opening it up in the text editor:

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

Raunaq
  • 1,853
  • 1
  • 14
  • 19
35
  1. Open a Text Editor view in Blender.
  2. Press Alt + O, or go to Text>Open Text Block and open the .py file
  3. Then simply press Run script :D

P.s. Instead of opening a file in step 2, you can also hit the "+ New" button and create a new script instead.

Note : In newer versions the Run Script button label has been replaced with a Play icon : enter image description here

Gorgious
  • 123
  • 6
Ertyui
  • 858
  • 9
  • 18
  • 3
    Thanks, I just figured this out but I don't see a way to see the output of running the script. I checked Blender's console--nothing! – CyberShot Jul 22 '12 at 23:00
  • 7
    It's not in the console of blender, unfortunately. It's in the terminal window of Blender. For linux/osx, you'll have to run blender from the terminal. And I think for windows there previously was a commandline window opened up alongside of blender. You can still turn this on in the help menu, as stated by this article: http://www.blender.org/documentation/blender_python_api_2_59_2/info_tips_and_tricks.html It could also be in the File menu though. – Ertyui Jul 22 '12 at 23:17
  • 3
    Hi, I have press the run script button but unfortunately it gives me error "Python script fails. look in the console for now" And in console there is nothing. So how can i guess what error with script? Tried to run this with python console using this filename = "/Users/sandeepsingh/Desktop/objc.py" exec(compile(open(filename).read(), filename, 'exec')) Traceback (most recent call last): File "", line 1, in File "/Users/sandeepsingh/Desktop/objc.py", line 9, in import Blender ImportError: No module named 'Blender' but here shows me error too ? – Sandeep Singh Jan 16 '13 at 11:44
  • Any help will be appreciated..Blender version is 2.65a. – Sandeep Singh Jan 16 '13 at 11:46
  • 9
    In windows you can show the Blender system console by going to the menu Window -> Toggle System Console – Mr Bell Oct 06 '13 at 16:50
  • even though the original question is not asking for it, I would edit the answer to add in step 2 : "or click on "+" to create a new text and paste your text", since this stackexchange page comes first in a google search for "run python script in blender". – ChameleonScales Aug 13 '17 at 22:20
  • I've added a P.s. Adding it to step 2 would make the sentence too long ;) – Ertyui Aug 15 '17 at 07:57
17

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.
Nick Brady
  • 6,084
  • 1
  • 46
  • 71
enas
  • 171
  • 1
  • 3
16

To run a script by another script or from console:

import bpy

script = bpy.data.texts["script_name.py"]
exec(script.as_string())
Jerryno
  • 891
  • 8
  • 16
0

It is likely that drawcar.py is trying to perform pyOpenGL commands inside Blender, and that won't work without modification. 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.

If you have a link to drawcar.py I can have a look at it and tell you what's going on.

zeffii
  • 534
  • 9
  • 22