I am having a "python script fail" on my project that I am working on and I do not know why. my script is programmed to turn on one lamp at a time and render an image with a different lamp turned on for each render.

import bpy, bgl, blf,sys
sceneKey = bpy.data.scenes.keys()[0]
filepath = "G:\rtitrial2"
# Loop all objects and try to find the Lamps
print ('Looping Lamps')
# first run through all of the lamps turning them off
for obj in bpy.data.objects:
    if ( obj.type =='LAMP'):
        obj.hide_render = True
        l = l + 1
print('You have hidden ' + str(l) + "lamps")

# now we can go through and
# individually turn them on
# and render out a picture
for obj in bpy.data.objects:
    if ( obj.type =='LAMP'):
        print (obj.name)
        obj.hide_render = False
        bpy.data.scenes[sceneKey].render.image_settings.file_format = 'JPEG'
        bpy.data.scenes[sceneKey].render.filepath = filepath + '//lamp_' + 
        # Render Scene and store the scene
        bpy.ops.render.render( write_still=True )
        obj.hide_render = True
  • $\begingroup$ Please indent your entire script with four spaces. That will format your question properly. $\endgroup$ – dr. Sybren Sep 15 '17 at 21:14
  • $\begingroup$ the forward slash is what solved it. Thank you very much. $\endgroup$ – JackG Sep 16 '17 at 1:49
  • $\begingroup$ I've moved my comment to an answer so you can mark this question as answered. $\endgroup$ – dr. Sybren Sep 16 '17 at 10:12
  • $\begingroup$ All the lamps in the scene with list comprehension lamps = [o for o in scene.objects if o.type == 'LAMP'] can be iterated with for lamp in lamps: and has len(lamps) members. $\endgroup$ – batFINGER Sep 16 '17 at 17:33

Instead of using bpy.data.scenes[sceneKey], just use bpy.context.scene to access the current scene. You also don't need the parentheses around conditions (it's Python, not C or Java). More important is the string assigned to filepath, though. \r means "carriage return". Just use forward slashes, Windows can handle those too.

Alternatively you can use \\r.


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.