47

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?

1
  • 1
    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¶ This page describes how Python is integrated and lists all the ways to start python scripts or integrate as extension. Commented Feb 9, 2019 at 2:34

5 Answers 5

43

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

Sign up to request clarification or add additional context in comments.

2 Comments

Waaay easier than the accepted answer and doesn't require constant re-load when making changes
But whenever I try to run the script, it reruns the entire script and doesn't delete the last one and mixing both scripts.
37
  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

7 Comments

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!
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: blender.org/documentation/blender_python_api_2_59_2/… It could also be in the File menu though.
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 "<blender_console>", line 1, in <module> File "/Users/sandeepsingh/Desktop/objc.py", line 9, in <module> import Blender ImportError: No module named 'Blender' but here shows me error too ?
Any help will be appreciated..Blender version is 2.65a.
In windows you can show the Blender system console by going to the menu Window -> Toggle System Console
|
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.

1 Comment

Is it possible to pass arguments to the python script?
16

To run a script by another script or from console:

import bpy

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.