2
$\begingroup$

I thought it was going to be an easy one for my first ever script but boy was I wrong. All I wanted was a script that creates a basic plane and set it to be a Shadow Catcher so I wrote this basic code:

import bpy

# Create plane
bpy.ops.mesh.primitive_plane_add(
    size=10, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
shadow_catcher = bpy.context.active_object

# Set plane to be a shadow catcher
bpy.context.scene.render.engine = 'CYCLES'
shadow_catcher.cycles.is_shadow_catcher = True
shadow_catcher.cycles_visibility.glossy = False
shadow_catcher.cycles_visibility.diffuse = False
shadow_catcher.cycles_visibility.transmission = False
shadow_catcher.cycles_visibility.scatter = False

This worked for creating the plane and setting the values but the results are not represented in the view port, It does work when rendering but I would like to be able to preview it. I've read about

bpy.context.scene.update()

and that it was later replaced by:

bpy.context.view_layer.update()

Since it is also mentioned in the Stale Data entry in the API documentation I've tried and implement a view layer update like so:

import bpy

# Create plane
bpy.ops.mesh.primitive_plane_add(
    size=10, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
shadow_catcher = bpy.context.active_object

# Set plane to be a shadow catcher
bpy.context.scene.render.engine = 'CYCLES'
shadow_catcher.cycles.is_shadow_catcher = True
shadow_catcher.cycles_visibility.glossy = False
shadow_catcher.cycles_visibility.diffuse = False
shadow_catcher.cycles_visibility.transmission = False
shadow_catcher.cycles_visibility.scatter = False

# Update view layer
bpy.context.view_layer.update()

but I don't see any difference. I wonder whether I'm not implementing the update() function correctly or whether it's not the problem in the first place, any help would be greatly appreciated.

$\endgroup$

1 Answer 1

2
$\begingroup$

You can just call Object.update_tag() to update the actual display data:

import bpy

C = bpy.context

# Set the engine to cycles
C.scene.render.engine = 'CYCLES'

# Create the primitive
bpy.ops.mesh.primitive_plane_add(size=10)

# Get a reference to the primitive
shadow_catcher = C.active_object

# Set plane to be a shadow catcher
shadow_catcher.cycles.is_shadow_catcher = True

# Update tag call on the shadow catcher object
shadow_catcher.update_tag()
$\endgroup$
1
  • 3
    $\begingroup$ Thank you so much! This works has expected $\endgroup$
    – MrWhite
    Sep 8, 2021 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.