Start External Blender Command Line from "regular"

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

mmabob
Posts: 8
Joined: Wed Dec 04, 2002 1:41 am
Location: Canada

Start External Blender Command Line from "regular"

Postby mmabob » Tue Mar 11, 2003 7:06 pm

Hi all,

I have an idea for a distributed internet based rendering solution, but I am having problems with one little part of my Python Script.

I am able to render a single frame from the dos command line, but I am unable to start that same command externally from Python.

This is to be run from "regular" Python, not the python utilities from within Blender.

From the command line I have been able to run:
blender -B filename -f framenumber

I have tried calling this command from Python using:
os.popen('blender -B filename -f framenumber')
or
os.system('blender -B filename -f framenumber')
... with no success.

Any Ideas?
Am I doing something wrong?
Is Python able to call and run external programs?

mmabob

Jamesk
Posts: 239
Joined: Mon Oct 14, 2002 8:15 am
Location: Sweden

Re: Start External Blender Command Line from "regular&q

Postby Jamesk » Tue Mar 11, 2003 8:14 pm

mmabob wrote:I have tried calling this command from Python using:
os.popen('blender -B filename -f framenumber')
or
os.system('blender -B filename -f framenumber')
... with no success.

Any Ideas?
Am I doing something wrong?
Is Python able to call and run external programs?

mmabob


It is able. Try checking the python docs on the spawn-command and it's closest relatives...

Schlops
Posts: 20
Joined: Wed Oct 16, 2002 2:29 pm

Re: Start External Blender Command Line from "regular&q

Postby Schlops » Tue Mar 11, 2003 10:00 pm

mmabob wrote:I have tried calling this command from Python using:
os.popen('blender -B filename -f framenumber')
or
os.system('blender -B filename -f framenumber')
... with no success.


Please define "No success" :wink:
Error messages? Nothing happens? XSI instead of blender starting? popen should work fine, maybe do a x = os.popen('blender -B filename -f framenumber') and a print x afterwards. What is x?
Stay Rude!

mmabob
Posts: 8
Joined: Wed Dec 04, 2002 1:41 am
Location: Canada

Postby mmabob » Tue Mar 11, 2003 10:00 pm

Thanks Jamesk,

I'll have to try that when I get home tonight.

mmabob
Posts: 8
Joined: Wed Dec 04, 2002 1:41 am
Location: Canada

Re: Start External Blender Command Line from "regular&a

Postby mmabob » Tue Mar 11, 2003 10:20 pm

Schlops wrote:Please define "No success" :wink:
Error messages? Nothing happens? XSI instead of blender starting? popen should work fine, maybe do a x = os.popen('blender -B filename -f framenumber') and a print x afterwards. What is x?


Depending on the comand, either nothing happens (Python accepts the command and moves on) or the "most success" I've had is using one of the commands I'll see a full screen black window try to open for a split second, then Python returns with no errors again.

In both cases, there was no evidence of Blender working in the background, and there was obviously no rendered frame in the output folder.

mmabob
Posts: 8
Joined: Wed Dec 04, 2002 1:41 am
Location: Canada

Postby mmabob » Wed Mar 12, 2003 6:29 am

Okay, I've retried popen, system and spawn in the Python IDLE...
Here's what I get.
I typed "import os" first of course.

When I type:
os.system('c:/Blender/farm/blender.exe -b c:\blender\projects\testp.blend -f 170')

The screen goes to a full screen black screen for a brief split second
and Python returns:
0

When I type:
os.popen('c:/Blender/farm/blender.exe -b c:\blender\projects\testp.blend -f 170')

The screen does not change like in the first example
and Python returns:
<open file 'c:/Blender/farm/blender.exe -b c:lender\projects estp.blend -f 170', mode 'r' at 0x00AA0270>

Whn I type:
os.spawnl(os.P_WAIT, 'c:/Blender/farm/blender.exe -b c:\blender\projects\testp.blend -f 170')

The screen does not change like in the first example
and Python returns:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in ?
os.spawnl(os.P_WAIT, 'c:/Blender/farm/blender.exe -b c:\blender\projects\testp.blend -f 170')
File "C:\PYTHON22\lib\os.py", line 530, in spawnl
return spawnv(mode, file, args)
OSError: [Errno 2] No such file or directory

So like I said, I'm not really getting any success...
Has anybody successfuly called a background instance of Blender from a Python script?
If so, could you please help me out with the coding...

Thanks all.

Schlops
Posts: 20
Joined: Wed Oct 16, 2002 2:29 pm

Postby Schlops » Wed Mar 12, 2003 12:08 pm

The os.popen seems to work fine in your case since it returns a open file object. So all you have to do is read every single line of that file to get blenders textual output ("Saved: Blahblahblah Blender quit") and do a close() afterwards to get the returncode.

Code: Select all

>>> import os
>>> render = os.popen('blender2.26 -b muardini0024.blend -f 1')
>>> for x in render.readlines():
...     print x

Now blender should render. Next step:

Code: Select all

>>> y = of.close()
>>> print y

If the print statement gives you None everything is fine (termination without errors).

HTH

Stay Rude!

mmabob
Posts: 8
Joined: Wed Dec 04, 2002 1:41 am
Location: Canada

Postby mmabob » Thu Mar 13, 2003 6:04 am

Okay, okay...

I figured it out.

I needed to os.chdir() to the dirctory containing blender.exe before simply running os.system('blender.exe -b ...etc.')

Thanks for your help everyone!

M.E.Farmer
Posts: 3
Joined: Fri Mar 14, 2003 5:02 am
Location: USA

Postby M.E.Farmer » Fri Mar 14, 2003 5:45 am

Hello , I am new to this forum, but maybe this will help.
In your example you did this:
mmabob wrote:os.spawnl(os.P_WAIT, 'c:/Blender/farm/blender.exe -b c:\blender\projects\testp.blend -f 170')

The problem is with / and \ .
In python / is ok ,but \ needs to be escaped with another \ .
Like this:

Code: Select all

"c:\\windows\\temp"

This is basic python ,but easy to goof .
mmabob wrote:I needed to os.chdir() to the dirctory containing blender.exe before simply running os.system('blender.exe -b ...etc.')

That works because you bypassed your errored \.
Try this:

Code: Select all

os.spawnl(os.P_WAIT, 'c:\\blender\\farm\\blender.exe -b c:\\blender\\projects\\testp.blend -f 170')

or use raw strings:

Code: Select all

os.spawnl(os.P_WAIT, r'c:\blender\farm\blender.exe -b c:\blender\projects\testp.blend -f 170')

:wink: Hope this helps.

mmabob
Posts: 8
Joined: Wed Dec 04, 2002 1:41 am
Location: Canada

Postby mmabob » Wed Mar 19, 2003 10:30 pm

Thanks M.E.Farmer,

That`s exactly what the problem was.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests