Problem rendering - can't get camera active (python script)

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
werty_TSS
Posts: 0
Joined: Thu Dec 20, 2012 7:45 am
Location: Austin, TX

Problem rendering - can't get camera active (python script)

Post by werty_TSS » Thu Jan 10, 2013 11:48 am

Hi,

I've got a python script to post treat a series of X3D files. I import one after another, treat the file and export as PNG. Problem is when I want to render the scene, it says "no camera" which is not true, there is a camera. My problem is that I can't script it to have it set my camera as active so it can render.

here s the script. It's not pretty (yet), i'm new with that...

At the begining, i set up the bathymetry of the region i want to plot. Then, I keep the bathymetry and change the chemical file for each time step, which i save.

Code: Select all

#-- Place lights
#----------------
bpy.ops.object.lamp_add(type = 'HEMI', view_align = False, location=(3,15,3), rotation=(0,0,0))
bpy.ops.transform.rotate(value=(-3.1415/2,),axis=(1,0,0), constraint_axis=(True,True,True), constraint_orientation='GLOBAL')




#-- Place camera
#----------------
bpy.ops.object.camera_add(view_align = True, enter_editmode=False, location = (3,18,0), rotation= (0,0,0))    # !!! CHECK GLOBAL/LOCAL ORIGINS FOR THE ROTATION 
bpy.ops.transform.rotate(value=(-3.1415/2,),axis=(1,0,0), constraint_axis=(True,True,True), constraint_orientation='GLOBAL') 
bpy.ops.transform.rotate(value=(3.1415,),axis=(0,1,0), constraint_axis=(True,True,True), constraint_orientation='GLOBAL')



bpy.ops.object.add()                                              # ?
bpy.data.objects['Empty'].select=True                             # ?
bpy.data.objects['Empty'].name="Aux_Empty"                        # ?
bpy.context.active_object.location =(0.0, 0.0, 0.0)               # ?
bpy.data.objects['Aux_Empty'].select=False                        # ?



#bpy.context.scene.frame_set(currentframe)                                      # ?
bpy.data.objects['Aux_Empty'].select=True                                       # ?
bpy.context.scene.objects.active = bpy.data.objects['Aux_Empty']                # ?
obj = bpy.context.object                                                        # ?
obj.location[2] = 0.0                                                           # ?
obj.keyframe_insert(data_path='location')                                       # ?
bpy.data.objects['Aux_Empty'].select=False                                      # deselect before deleting stuff
    
#-- Import the bathymetry
bpy.ops.import_scene.x3d(filepath= path_to_bathy+filename_of_bathy+'.x3d', filter_glob="*.x3d;*.wrl", axis_forward='Z', axis_up='Y')
bpy.data.objects['ShapeIndexedFaceSet'].name="imported_bathymetry"
bpy.data.objects['imported_bathymetry'].select=True
bpy.context.scene.objects.active = bpy.data.objects['imported_bathymetry']
bpy.context.active_object.material_slots[0].material.use_vertex_color_paint=True
bpy.data.objects['imported_bathymetry'].select=False 
bpy.ops.object.delete()
bpy.data.objects['imported_bathymetry'].select=True
bpy.context.scene.cursor_location = (0.0, 0.0, 0.0)              #set the cursor to the center
bpy.ops.object.origin_set(type = 'ORIGIN_CURSOR')                #set the origin to the cursor
bpy.ops.object.origin_set()                                      #set the geometry to the origin
bpy.ops.transform.resize(value=(scalefactor,scalefactor,scalefactor))                    #resize the bathymetry
bpy.ops.transform.translate(value=(0, -.7, 0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL')   #choose better value for the translation !!!
bpy.ops.object.shade_smooth()                                    #smooth the bathymetry



#-- Treatment of the files for each timestep
#--------------------------------------------  
for currentframe in range(startframe_ofx3d,(endframe_ofx3d+1)):
    #---------------------------Import the x3d file---------------
    bpy.ops.import_scene.x3d(filepath= path_tox3d+filename_ofx3d+str(currentframe)+'.x3d', filter_glob="*.x3d;*.wrl", axis_forward='Z', axis_up='Y')
    
    #--rename the geometry to "imported geometry"
    bpy.data.objects['ShapeIndexedFaceSet'].name="imported_geometry"
    bpy.data.objects['imported_geometry'].select=True
    bpy.context.scene.objects.active = bpy.data.objects['imported_geometry']
    bpy.context.active_object.material_slots[0].material.use_vertex_color_paint=True
    bpy.data.objects['imported_geometry'].select=False 
    
    #-- delete all of the artefacts added when importing the x3d file
    bpy.ops.object.delete()
    
    #--Rescpale and reorientate the imported geometry
    bpy.data.objects['imported_geometry'].select=True
    bpy.context.scene.cursor_location = (0.0, 0.0, 0.0)              #set the cursor to the center
    bpy.ops.object.origin_set(type = 'ORIGIN_CURSOR')                #set the origin to the cursor
    bpy.ops.object.origin_set()                                      #set the geometry to the origin
    bpy.ops.transform.resize(value=(scalefactor,scalefactor,scalefactor))                    #resize the geometry
    
    #-- set the rendering options
    bpy.data.scenes['Scene'].render.resolution_percentage=100
    bpy.data.objects['imported_geometry'].select = False
    bpy.data.objects['Camera'].select = True
    

    
    bpy.ops.render.render(write_still=True)
    bpy.data.images['Render Result'].file_format='PNG'
    bpy.data.images['Render Result'].save_render(filepath=path_toimage+filename_ofimage+str(currentframe)+'.png')
    bpy.data.objects['imported_geometry'].select=True
thanks in advance.

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Fri Jan 11, 2013 1:34 am

selecting a camera object won't help, you need to set the scene's camera:

Code: Select all

bpy.context.scene.camera = bpy.data.objects['Camera']
I'm sitting, waiting, wishing, building Blender in superstition...

werty_TSS
Posts: 0
Joined: Thu Dec 20, 2012 7:45 am
Location: Austin, TX

Post by werty_TSS » Fri Jan 11, 2013 2:01 am

CoDEmanX wrote:selecting a camera object won't help, you need to set the scene's camera:

Code: Select all

bpy.context.scene.camera = bpy.data.objects['Camera']
aaaah yeah I see! it works now, thanks a lot!

Post Reply