Cannot display a render preview of a scene by using python script -- need advices

Hi, guys,

I want to code a python script to display a render preview of a scene (using Cycles) directly without clicking the “Rendered” item in space view button.

I have checked the python command of “Rendered” is:

bpy.data.screens[“Default”].(null) = ‘RENDERED’

So I type the following codes in the “Text Editor”

python code:
"
import bpy

bpy.context.scene.render.engine = ‘CYCLES’

bpy.data.screens[“Default”].(null) = ‘RENDERED’

"

and click “Run Script” button.

However, an info [Editor Type] displays :
"bpy.data.screens[“Default”].(null) = ‘RENDERED’

^

SyntaxError: invalid syntax
location:<unkonwn location>: -1
Error: Python script fail, look in the console for now…."

The scene is not changed, and the screen lay-out is “Default” .

I tested the script in both 2.67a and 2.65 in two different desktops. The problem is the same.

Could anyone give me some ideas to fix this problem?

Thanks in advance.

What does the console tell you?

By the way, I think it’s hilarious that your handle is “forlogin”. :slight_smile:

as log as the password isn’t forpassword xD

if you see (null) in operator log, then it means you can’t use it, for some reason the logger couldn’t find the actual data path.

You need to do:

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        area.spaces[0].viewport_shade = 'RENDERED'

I’ve been WONDERING what purpose “areas” served! Thanks.

screens = UI layouts, there is only one active layout.

areas = the editor rectangles in a screen, area.type defines the editor type (e.g. OUTLINER, VIEW_3D etc.)

regions = “sub-editor rectangles”, better known as sidebars. Note that an area’s header is also a region.

spaces = data stored along with an area (like viewport_shade). Not sure why / when there can be multiple spaces. Note that VIEW_3D doesn’t store most data in spaces[0]., but in spaces[0].region_3d. (or region_quadview.* respectively)

It’s a farmous ID and hard to forget. It’s lucky for me to regist this ID at blenderartist :yes:

Thanks a lot for these detailed explanations. The code on the 3rd floor works! Thank you again.