Paraview -> Blender python pipeline

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Reynolds
Posts: 19
Joined: Tue Mar 27, 2012 7:54 pm

Paraview -> Blender python pipeline

Postby Reynolds » Tue Mar 27, 2012 8:11 pm

Hello folks on this forum,
I've recently started using blender for scientific visualization purposes and I am fascinated by its capabilities. After some initial "still shots", I'd like to try my hand on a more advanced topic, i.e. transient rendering.

Here's is what I would like to do (please excuse me not using the correct blender terms, I'm not familiar with your lingo yet):

I created time-dependent x3d files with paraview, file names like "file001.x3d, file002.x3d....". These files contain time-dependent geometry I would like to render, so file001 contains the geometry at time 1s, file002 at 2s and so on.

I would like to import these files in blender consecutively, and render them with changing camera angles and such. So in my head, there's some type of list that contains the following information:

file001 -> frame 1 -> Camera position x1,.... Rotation r1
file002 -> frame 2 -> Camera position x2,.... Rotation r2


The loop would look something like this:

(available are x3d-files from file000.x3d to filennn.x3d:

for file=000 to nnn do
delete old geometry in blender/frame
import x3d geometry in blender
put the geometry in the current frame
find (from some file or so) the camera position and other things according to the file number
move the camera
render image
end do

I have tried (and succeded) at rendering a still geometry and moving the camera around to create movies, now what I would like to do is to extend it to a "frame dependent" geometry/x3d.


I'm not asking anybody to do this for me (although if someone has some python snippets that might help, please post them), but maybe you could point me in the right direction, give some hints, maybe clarify what I should do.... any general comment, a guideline or such would be highly appreciated. I guess my first conceptual concern would be to understand how I can tie a frame to a given (like in a textfile or in blender) camera position..... Is it for example possible to do the following:

Take a single file, i.e. single geometry. Set up all the camera movement with frames, lets say from frame 1 to 100, then somehow save this data. Now rerun the whole process, but load a new geometry for each frame??


sorry again for not being able to express my questions clearer, and thanks a lot for your help!

All the best
reynolds

ldo
Posts: 544
Joined: Sun Nov 07, 2010 11:30 pm
Location: Hamilton, New Zealand

Postby ldo » Sun Apr 01, 2012 3:03 am

Certainly possible. The question is, do you want to learn to do it for yourself, or are you trying to get somebody else to do it for you?

Reynolds
Posts: 19
Joined: Tue Mar 27, 2012 7:54 pm

Postby Reynolds » Sun Apr 01, 2012 8:12 pm

ldo wrote:Certainly possible. The question is, do you want to learn to do it for yourself, or are you trying to get somebody else to do it for you?


I'd like to do it myself, but I'm a little bit stuck at the moment and can't find a way to import x3d in python. Is there any reference on how to that?
Thank you!

Reynolds
Posts: 19
Joined: Tue Mar 27, 2012 7:54 pm

Postby Reynolds » Sun Apr 01, 2012 8:31 pm

Reynolds wrote:
ldo wrote:Certainly possible. The question is, do you want to learn to do it for yourself, or are you trying to get somebody else to do it for you?


I'd like to do it myself, but I'm a little bit stuck at the moment and can't find a way to import x3d in python. Is there any reference on how to that?
Thank you!



I just found out that blender has these nice tooltips in python, so that when you hover over the menu, it gives you the python command. That's nice! I managed to import an x3d file, now I'm working on getting it rendered... I will post back!

jbodart
Posts: 2
Joined: Tue Apr 06, 2010 5:56 pm

Postby jbodart » Fri Apr 20, 2012 6:30 am

Hi Reynolds,

I went through this issue and in my opinion the best way to do it is to use .ply file. I don't think paraview can export this kind of file but vtk can easily do it.

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

Postby CoDEmanX » Fri Apr 20, 2012 7:43 pm

Hey Reynolds,

do your x3d models share the same geometry (same vertex count, basically all verts moved to another location)?

If yes, then google for Blender Shape Keys!

If no, read my post here, as it's very similar: http://www.blender.org/forum/viewtopic.php?t=23355
I'm sitting, waiting, wishing, building Blender in superstition...

Reynolds
Posts: 19
Joined: Tue Mar 27, 2012 7:54 pm

Postby Reynolds » Tue May 08, 2012 10:48 pm

Hey folks,
thank to you all for your feedback. I've got a basic code together, that does what I'm after:

import a time series of x3d.files (one after the other so not to blow up my RAM), take the camera position and other stuff from the .blend file, render the image and save it.

It's just the basic stuff, nothing fancy or elegant, but what's nice is that I can set up all my cameras and create a nice background and lighting in the blender UI, then save the .blend file, and run the whole job in batchmode like this:

blender -b myblender.blend -s 0 -e 100 -P mypythonstuff.py


If anyone is interested, I'll be happy to post the code.

all the best
Reynolds

Klaus@RZG
Posts: 4
Joined: Thu May 10, 2012 4:28 pm

Postby Klaus@RZG » Thu May 10, 2012 4:44 pm

Hey there Reynolds,

it would be great if you could post your code snippet which shows the loop which reads the .x3d files and updates the data in Blender.

I have an existing visualization in ParaView (camera animation + time-varying data) and am currently working on improving it using Blender.

Thanks very much!

Best,
Klaus

gl1w07
Posts: 4
Joined: Mon May 14, 2012 11:54 am

Postby gl1w07 » Mon May 14, 2012 12:25 pm

Hi Reynolds,

I would be very interested if your offer to post the code still holds.
I am also keen to combine Paraview and Blender rendering.

Many thanks

Georges



Reynolds wrote:Hey folks,
thank to you all for your feedback. I've got a basic code together, that does what I'm after:

import a time series of x3d.files (one after the other so not to blow up my RAM), take the camera position and other stuff from the .blend file, render the image and save it.

It's just the basic stuff, nothing fancy or elegant, but what's nice is that I can set up all my cameras and create a nice background and lighting in the blender UI, then save the .blend file, and run the whole job in batchmode like this:

blender -b myblender.blend -s 0 -e 100 -P mypythonstuff.py


If anyone is interested, I'll be happy to post the code.

all the best
Reynolds

Reynolds
Posts: 19
Joined: Tue Mar 27, 2012 7:54 pm

Postby Reynolds » Mon May 14, 2012 6:09 pm

hey folks,
I've just ported the script to blender 2.63, it seems to be working nicely. Give me a few days to clean up the code (it's very "informal"), and I'll be happy to post what i've got. One warning though: I'm no expert in this, and it's probably not very elegant or efficient coding!

Regards,
reynolds

gl1w07
Posts: 4
Joined: Mon May 14, 2012 11:54 am

Postby gl1w07 » Tue May 15, 2012 11:19 pm

Hi Reynolds,

Great!
I look forward to seeing your code.
Many thanks

G


Reynolds wrote:hey folks,
I've just ported the script to blender 2.63, it seems to be working nicely. Give me a few days to clean up the code (it's very "informal"), and I'll be happy to post what i've got. One warning though: I'm no expert in this, and it's probably not very elegant or efficient coding!

Regards,
reynolds

gl1w07
Posts: 4
Joined: Mon May 14, 2012 11:54 am

Postby gl1w07 » Mon May 28, 2012 9:23 am

Hi Reynolds,

I was wondering whether you had a chance to look at cleaning your code.
Let me know if I can help.

G


gl1w07 wrote:Hi Reynolds,

Great!
I look forward to seeing your code.
Many thanks

G


Reynolds wrote:hey folks,
I've just ported the script to blender 2.63, it seems to be working nicely. Give me a few days to clean up the code (it's very "informal"), and I'll be happy to post what i've got. One warning though: I'm no expert in this, and it's probably not very elegant or efficient coding!

Regards,
reynolds

Klaus@RZG
Posts: 4
Joined: Thu May 10, 2012 4:28 pm

Postby Klaus@RZG » Tue May 29, 2012 11:22 am

Hi Reynolds,

I'd like to ask if you got a chance to do the code cleaning?

It would be really great to have a first idea on how to start transferring a Python-based time animation. Code cleanliness is not so important :)

In case you decide against making the current code version public I would really appreciate if you sent me a PM with the code. In this case I can offer to post it in the current thread after cleaning and extending it. I'm a sophisticated Python programmer but a novice to Blender.

Best regards,
Klaus

Reynolds
Posts: 19
Joined: Tue Mar 27, 2012 7:54 pm

Postby Reynolds » Thu May 31, 2012 12:13 am

Klaus@RZG wrote:Hi Reynolds,

I'd like to ask if you got a chance to do the code cleaning?

It would be really great to have a first idea on how to start transferring a Python-based time animation. Code cleanliness is not so important :)

In case you decide against making the current code version public I would really appreciate if you sent me a PM with the code. In this case I can offer to post it in the current thread after cleaning and extending it. I'm a sophisticated Python programmer but a novice to Blender.

Best regards,
Klaus



Hello guys,
sorry for the late reply, I got bogged down by work.

Here is a VERY dirty version with lots of commented out lines, but I did this with it :

http://www.youtube.com/watch?v=RXsmcejFYv8, so the basic functionality is there.

Code: Select all

import bpy

##################################################################

path_tox3d='C:/Users/xxx/Documents/blender/'
path_toimage='C:/Users/xxx/Documents/blender/'
filename_ofx3d='projectname_'
filename_ofimage='newimage_'
startframe_ofx3d=0
endframe_ofx3d=10



##################################################################
#---------------------------------------------------------------#
##  Delete everything from scene before importing              ##

#bpy.ops.object.select_all(action='TOGGLE')
#bpy.ops.object.delete()

#---------------------------------------------------------------#
##  Add a text                                                  #
#bpy.ops.object.text_add()
#bpy.ops.object.editmode_toggle()
#bpy.ops.font.delete()
#bpy.ops.font.text_insert(text="Frame: ")
#bpy.ops.object.editmode_toggle()
#bpy.ops.object.select_all(action='TOGGLE')
#bpy.data.objects['Text'].name="Frametext"

#---------------------------------------------------------------#
#bpy.context.scene.frame_set(currentframe)
bpy.ops.object.add()
bpy.data.objects['Empty'].select=True
bpy.data.objects['Empty'].name="Aux_Empty"
#bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
bpy.context.active_object.location =(0.0, 0.0, 0.0)
bpy.data.objects['Aux_Empty'].select=False
 

for currentframe in range(startframe_ofx3d,endframe_ofx3d):   
    bpy.context.scene.frame_set(currentframe)
    #bpy.ops.object.add()
    bpy.data.objects['Aux_Empty'].select=True
    bpy.context.scene.objects.active = bpy.data.objects['Aux_Empty']
    #bpy.data.objects['Empty'].name="MyEmpty"
    obj = bpy.context.object
    obj.location[2] = 0.0
    #obj.keyframe_insert()
    obj.keyframe_insert(data_path='location')
    #bpy.context.active_object.location =(0.0, 0.0, 0.0)
    bpy.data.objects['Aux_Empty'].select=False
 
    #---------------------------------------------------------------#
    ## 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')
    #---------------------------------------------------------------#
    ## add the frame number to the text
    #bpy.data.objects["Frametext"].select=True
    #bpy.ops.object.editmode_toggle()
    #bpy.ops.font.delete()
    #bpy.ops.font.text_insert(text="Frame: "+str(currentframe))
    #bpy.ops.object.editmode_toggle()
    #bpy.data.objects["Frametext"].select=False
       
    #---------------------------------------------------------------#
    ## 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.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    bpy.data.objects['imported_geometry'].select=False
    ## rename the default lamp to imported_lamp and delete right away
    bpy.data.objects['TODO'].name="imported_lamp"
    bpy.data.objects['imported_lamp'].select=True
    bpy.ops.object.delete()
    ## rename the default camera to imported_camera and delete right away
    bpy.data.objects['Viewpoint'].name="imported_camera"
    bpy.data.objects['imported_camera'].select=True
    bpy.ops.object.delete()
   
    #bpy.context.active_object.location = position
   



   
      #for object_name in candidate_list:
    #      bpy.data.objects[object_name].select = True
     #     bpy.context.active_object.location = position
    #  bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)


    bpy.data.scenes['Scene'].render.resolution_percentage=100

    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
    bpy.ops.object.delete()


bpy.data.objects['Aux_Empty'].select=True
bpy.ops.object.delete()


Caveat: This only works for the blender render engine, I would love to get it to work with cycles (the new render engine), if there's anyone who could help me it or would just like to clean up my code and repost it here, please feel free!

All the best, einen schönen Abend

Reynolds

gl1w07
Posts: 4
Joined: Mon May 14, 2012 11:54 am

Postby gl1w07 » Thu May 31, 2012 1:37 am

Hi Reynolds,

Many thanks for posting the code.
That's very generous of you.

G

Reynolds wrote:
Klaus@RZG wrote:Hi Reynolds,

I'd like to ask if you got a chance to do the code cleaning?

It would be really great to have a first idea on how to start transferring a Python-based time animation. Code cleanliness is not so important :)

In case you decide against making the current code version public I would really appreciate if you sent me a PM with the code. In this case I can offer to post it in the current thread after cleaning and extending it. I'm a sophisticated Python programmer but a novice to Blender.

Best regards,
Klaus



Hello guys,
sorry for the late reply, I got bogged down by work.

Here is a VERY dirty version with lots of commented out lines, but I did this with it :

http://www.youtube.com/watch?v=RXsmcejFYv8, so the basic functionality is there.

Code: Select all

import bpy

##################################################################

path_tox3d='C:/Users/xxx/Documents/blender/'
path_toimage='C:/Users/xxx/Documents/blender/'
filename_ofx3d='projectname_'
filename_ofimage='newimage_'
startframe_ofx3d=0
endframe_ofx3d=10



##################################################################
#---------------------------------------------------------------#
##  Delete everything from scene before importing              ##

#bpy.ops.object.select_all(action='TOGGLE')
#bpy.ops.object.delete()

#---------------------------------------------------------------#
##  Add a text                                                  #
#bpy.ops.object.text_add()
#bpy.ops.object.editmode_toggle()
#bpy.ops.font.delete()
#bpy.ops.font.text_insert(text="Frame: ")
#bpy.ops.object.editmode_toggle()
#bpy.ops.object.select_all(action='TOGGLE')
#bpy.data.objects['Text'].name="Frametext"

#---------------------------------------------------------------#
#bpy.context.scene.frame_set(currentframe)
bpy.ops.object.add()
bpy.data.objects['Empty'].select=True
bpy.data.objects['Empty'].name="Aux_Empty"
#bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
bpy.context.active_object.location =(0.0, 0.0, 0.0)
bpy.data.objects['Aux_Empty'].select=False
 

for currentframe in range(startframe_ofx3d,endframe_ofx3d):   
    bpy.context.scene.frame_set(currentframe)
    #bpy.ops.object.add()
    bpy.data.objects['Aux_Empty'].select=True
    bpy.context.scene.objects.active = bpy.data.objects['Aux_Empty']
    #bpy.data.objects['Empty'].name="MyEmpty"
    obj = bpy.context.object
    obj.location[2] = 0.0
    #obj.keyframe_insert()
    obj.keyframe_insert(data_path='location')
    #bpy.context.active_object.location =(0.0, 0.0, 0.0)
    bpy.data.objects['Aux_Empty'].select=False
 
    #---------------------------------------------------------------#
    ## 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')
    #---------------------------------------------------------------#
    ## add the frame number to the text
    #bpy.data.objects["Frametext"].select=True
    #bpy.ops.object.editmode_toggle()
    #bpy.ops.font.delete()
    #bpy.ops.font.text_insert(text="Frame: "+str(currentframe))
    #bpy.ops.object.editmode_toggle()
    #bpy.data.objects["Frametext"].select=False
       
    #---------------------------------------------------------------#
    ## 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.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    bpy.data.objects['imported_geometry'].select=False
    ## rename the default lamp to imported_lamp and delete right away
    bpy.data.objects['TODO'].name="imported_lamp"
    bpy.data.objects['imported_lamp'].select=True
    bpy.ops.object.delete()
    ## rename the default camera to imported_camera and delete right away
    bpy.data.objects['Viewpoint'].name="imported_camera"
    bpy.data.objects['imported_camera'].select=True
    bpy.ops.object.delete()
   
    #bpy.context.active_object.location = position
   



   
      #for object_name in candidate_list:
    #      bpy.data.objects[object_name].select = True
     #     bpy.context.active_object.location = position
    #  bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)


    bpy.data.scenes['Scene'].render.resolution_percentage=100

    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
    bpy.ops.object.delete()


bpy.data.objects['Aux_Empty'].select=True
bpy.ops.object.delete()


Caveat: This only works for the blender render engine, I would love to get it to work with cycles (the new render engine), if there's anyone who could help me it or would just like to clean up my code and repost it here, please feel free!

All the best, einen schönen Abend

Reynolds


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest