3
$\begingroup$

I would like to write a webservice, which renders images using blender using the passed pose of the camera. This means I need to start blender within python, pass the scene, place the camera and render an image to a file.

Does anyone have a hint where to start looking first?

$\endgroup$
  • $\begingroup$ So your webservice will have blender on the server, correct? Then an external party will connect to the web service, and pass the scene and camera, and then your web service will render the image and return it? I'm assuming you don't just want the client to pass a blend file? $\endgroup$ – Todd McIntosh Apr 23 '16 at 18:07
  • $\begingroup$ Actually, the client (javascript, not actually human user) passes the camera position and orientation (quaternion). Then the webservice is expected to deliver the image of the scene from that pose (ori+pos). $\endgroup$ – mojovski Apr 23 '16 at 19:09
  • $\begingroup$ So you have a presetup blend file on the server, and the only external input is the camera info? $\endgroup$ – Todd McIntosh Apr 23 '16 at 19:22
  • $\begingroup$ How does the client generate the camera info? In a local blend file with the same scene, or is it arbitrary? $\endgroup$ – Todd McIntosh Apr 23 '16 at 19:23
  • $\begingroup$ ... it doesnt matter, or does it? What I want to do is something like this in pseudocode: scene=Bleder.new scene.loadFile(myFileName) scene.setCameraPose(position, quaternion) scene.render(outputfile_path) $\endgroup$ – mojovski Apr 23 '16 at 19:41
3
$\begingroup$

Ok, so I'll attempt to answer this one. Some of it will be pseudocode, and some will be actual code.

The basic pipeline would be:

  1. Javascript submission of camera data in the form of text variables to your web service. The syntax of this data will be up to you since you're building the web service api yourself.

  2. Once the request has been made and received by the web service, your back end code will need to generate a Shell script command to call an instance of your blender application on the server.

Syntax would something like:

blender -b scene.blend -P pythonscript.py -o //outputpath -F PNG -x 1 -a -- camVar1 camVar2 camVar3

where

  • scene.blend is your scene file on the server
  • pythonscript.py is your launch script that will process your input variables
  • everything following the "--" are raw input variables and the camVar values are written into the command from the backend code

Your pythonscript.py file will start like this:

import sys

# these variables are read backwards from the end of the shell command    
camVar1 = sys.argv[-3]
camVar2 = sys.argv[-2]
camVar3 = sys.argv[-1]

See also: How to pass command line arguments to a Blender Python script?

Then in the same python script you can set the camera properties from the input variables, and render out the image to the output folder.

The web service backend would have to be monitoring the output path for the rendered image to be written, and then serve it back to the initial web request. It may not be fast enough for an immediate web response, so you may have to serve the image via a image link notification.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Let me know if this is enough detail to get you going. $\endgroup$ – Todd McIntosh Apr 23 '16 at 22:44
  • $\begingroup$ Note that it may be simpler to pass in json data (as an argument, or the path to a json file for eg). $\endgroup$ – ideasman42 Apr 23 '16 at 23:57

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.