Running python script in Blender - python

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?

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

Open a Text Editor view in Blender.
Press Alt + O, or go to Text>Open Text Block and open the .py file
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 :

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

Related

Can manim be used in pycharm?

I have been programming with python for about half a year, and I would like to try manim ( the animation programme of 3blue1brown from youtube), but I am not sure where to start. I have not installed it, but I have tried to read up on it. And to be honest I do not understand much of the requirements of the program, and how to run it.
Google has left me without much help, so I decided to check here to see if anyone here is able to help.
From what I understand, you run manim directly in python and the animations are based on a textfile with code i assume is LaTex. I have almost no experience with python itself, but I have learned to use it through Thonny, and later Pycharm.
My main questions are: (Good sources to how to do this without being a wizard would be really helpful if they exist☺️)
Is it possible to install manim in pycharm, and how? Do i need some extra stuff installed to pycharm in order to run it? (I run a windows 64-bit computer)
If i manage to do this in pycharm, Will I then be able to code the animations directly in pycharm (in .py or .txt files), or is it harder to use in pycharm?
All help or insights is very appreciated😅 As I said I am not extremely knowledgeable in computers, but I am enjoying learning how to code and applications of coding😊
I recommend you this playlist
I always uses pycharm for manim.
Firstly i setup python interpreter by just open File->Settings->Projet->Project Interpreter then just press on little gear icon to add python interpreter to Existing environment and locate C:\Python3x\python.exe
Then just open a terminal from left-down corner and run some basic commands to run manim as mentioned in tutorials or manim github page.
Something that works nicely for me is to run manimgl.exe from Python in PyCharm using the subprocess module. It also goes well with using the run shortcut while iterating with small edits.
I like to do this from the script in which my main scene is defined, for example, I have main.py which defines MyScene:
from manimlib import *
class MyScene(Scene):
def construct(self):
...
if __name__ == '__main__':
import subprocess
params = 'manimgl main.py MyScene -c WHITE'.split()
subprocess.run(params,
check=True,
capture_output=True,
text=True)
# Possibly look at captured output here
The code inside if __name__ ... does not execute when the same script is loaded by manim. What is nice is that one can easily add automation steps before or after the actual execution if needed and it keeps everything related in a single script.
Edit: I also end the animations in the construct() method of MyScene with exit() to terminate the preview. I honestly don't know if this is good practice, but it works well for my usage pattern.
Note that this does require that manimgl.exe reside somewhere that is in your path, in my case (Windows) I installed this for my global Python interpreter. I followed the instructions on GitHub and it works for me because the following is in my path:
C:\Users\XXX\AppData\Local\Programs\Python\Python38\Scripts\
It may vary depending on where your Python is installed. For a venv, you could do something like:
params = '.\venv\Scripts\manimgl.exe ...'.split()
Yes, you can
1.Write your code in pycharm
2.save it
3.copy that .py file to where you installed manim. In my case, it is
This pc>> C drive >> manim-master >> manim-master
4.select on the path and type "cmd" to open terminal from there
Type this on the terminal
python -m manim -pql projectname.py
This will do.
To play back the animation or image, open the media folder.

Notepad++ won't work with Python

I'm just getting into programming and took the common advice of using a free text editor with python. I seem to be running into a problem and everything I've tried so far hasn't worked.
I downloaded Anaconda from this url and whenever I put the location of python.exe into the run bar: C:\Users\Home\Anaconda2\python.exe all I get is the python console to pop up. I've tried adding: "$(FULL_CURRENT_PATH)" to the end as one user had suggested, but that just opens up the console and closes it immediately after. Any idea what I'm doing wrong?
First, I usually create a space for all my projects. Usually C:\projects so it's easy to find. Then I create a folder for specific work. I'd do C:\projects\learningPython.
Now, create a new file and save it to C:\projects\learningPython\hello-world.py
In the file put this line
print('Hello World!')
Then click save.
Now open a command prompt (enter cmd in the run bar)
now enter this in the command prompt:
python C:\projects\learningPython\hello-world.py
Hopefully this will work for you. Let me know if you do not see 'Hello World' appear on the command prompt.
After you followed nlloyd instructions and you are assured that your setup works and you can use Python from the commandline, I would use the nppexec Plugin to integrate Python (and other external tools) with notepad++ . (You can get it from Notepad++ PluginCentral or directly via the Plugins -> PluginManager.)
There I would create a nppexec script: with the following lines (use Plugins -> NppExec -> Execute... ):
npp_save
C:\Tools\Python\Python.exe $(FULL_CURRENT_PATH)
change C:\Tools\Python\Python.exe to whereever you have installed python
save this nppexec script
use the commands Execute... and Direct execute previous from Plugins -> Nppexec menu to select the nppexec script (if you have several) and repeatedly use the same nppexec script

Blender2Ogre - Execute python script in command line

I would like to transform blender meshes into ogre meshes. I downloaded this add-on which is a python script that allows me to do it.
I already installed this extension in blender (user preferences - addons - Install) and works perfectly.
Now I would like to execute the same script in the command line so I can transform any mesh without opening the blender user interface.
Using cmd (I'm a windows user) I go to the folder where blender is installed and run the following command:
blender.exe --background --python ./2.76/scripts/addons/io_export_ogreDotScene.py box.blend
but nothing happens. I think it's because the script was written only to be used inside blender, not through the command line.
Is there a workaround to execute this script ?
I'm not really good in python, so reading the script is not helping me at all.
I'll appreciate any help I can get form you guys !!
I was able to decode the script and make it run from the command line. It converts a blender file sent as a parameter and converts it in a ogre mesh file.
If you need it, let me know.

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Running Python from the Windows Command Line

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]

Categories