The International Simutrans Forum

 

Author Topic: Blender Rendering Scripts  (Read 4514 times)

0 Members and 1 Guest are viewing this topic.

Offline The Hood

  • Devotee
  • *
  • Posts: 2889
  • pak128.Britain developer
Blender Rendering Scripts
« on: June 04, 2009, 10:03:27 AM »
Just wondering if anyone has or knows of any python scripts which will automatically rotate and render a blender model in the 8 orientations we have in simutrans (saving the 8 rotations as different png files)?  Don't know why I didn't think of this before but it would save a lot of time...!!!

Offline vilvoh

  • One of the good guys
  • Administrator (Inactive)
  • *
  • Posts: 4504
  • I'm the constructor, the architect
    • Escala real
Re: Blender Rendering Scripts
« Reply #1 on: June 04, 2009, 10:34:07 AM »
I don't know if such a script exits but you can also do it just using a camera and a circular path. Link the camera movement to the path, and create 8 frames for the eight different positions of the camera. Then bake the animation and wait for the result. The main disadvantage is that you have to repeat this process with every new scene, unless you always use the same master scene with the animation previously set up, and the script would help to automatize the whole process.

On the other hand, the problem of the script is that you need some reference element, that must act as the center of the camera rotation.

Perhaps this script is what you're looking for (link) but probably you may need to modify it with a little help.

Quote
ssrot-blender.txt
This is a python script that will rotate multiple objects in 9 degree increments and save the renders to disk. If you open it in the blender text editor you can press alt+p to execute it. You'll probably have to edit it slightly to suit your model.

Code: [Select]
# rotation script

#

# first press the big render button and make

# sure it looks how the first frame should be,

# then press alt + p to run this script and

# it'll save to the 'rotated' directory.

#

# june 18 2005 s

# aug 6 2005 s & db



FILENAME = "nwb"

OBJECTNAME ="Cube"

CAMERAANGLE = 0



dodebug = 0

dorolls = 1

doleftrolls = 1

dorightrolls = 1



import Blender

import math



from Blender import *

from Blender.Scene import Render



# code by timmeh (#blenderchat)

def save_frame(frameno):

scn = Scene.GetCurrent()

context = scn.getRenderingContext()



#context.setImageType(Render.TARGA)

context.setImageType(Render.PNG)



# should probably set a frame and at the end of the prog render

# the animation instead of rendering each frame one by one.

# that would also give the advantage of being able to render to

# a video directly (i think).

context.setRenderPath("rotated/%s%02d-" % (FILENAME, frameno))



context.startFrame(1)

context.endFrame(1)



context.renderAnim()



# code by timmeh (#blenderchat)

def save_roll_frame(frameno):

scn = Scene.GetCurrent()

context = scn.getRenderingContext()



#context.setImageType(Render.TARGA)

context.setImageType(Render.PNG)



# should probably set a frame and at the end of the prog render

# the animation instead of rendering each frame one by one.

# that would also give the advantage of being able to render to

# a video directly (i think).

context.setRenderPath("rotated/%sroll%03d-" % (FILENAME, frameno))



context.startFrame(1)

context.endFrame(1)



context.renderAnim()



def to_rad(a):

return a * math.pi / 180



def set_angle(obj, degrees):

if CAMERAANGLE:

sigma = to_rad(0 - degrees)

x = math.cos(sigma)

y = math.sin(phi) * math.sin(sigma)

obj.RotZ = math.atan2(y, x)

else:

obj.RotZ = - degrees * math.pi / 180



# main

# get object

obj = Blender.Object.Get(OBJECTNAME)



# calc phi

phi = to_rad(CAMERAANGLE)



# debug, rotates once (doesn't render, see 3d view)

if dodebug:

set_angle(obj, 45)

Blender.Redraw()

else:

# 40 normal rotations

for i in range(40):

set_angle(obj, i * 9)

#Blender.Redraw()

save_frame(i)



# 400 rolls

if dorolls:

if doleftrolls:

# left rolls

for j in range(5):

obj.RotY = to_rad((5 - j) * 9)

for i in range(40):

set_angle(obj, i * 9)

#Blender.Redraw()

save_roll_frame(j * 40 + i)

# right rolls

if dorightrolls:

for j in range(5):

obj.RotY = - to_rad((j + 1) * 9)

for i in range(40):

set_angle(obj, i * 9)

#Blender.Redraw()

save_roll_frame(200 + j * 40 + i)



# restore back to normal

obj.RotZ = 0

obj.RotY = 0

Blender.Redraw()

Offline The Hood

  • Devotee
  • *
  • Posts: 2889
  • pak128.Britain developer
Re: Blender Rendering Scripts
« Reply #2 on: June 04, 2009, 11:02:10 AM »
That looks pretty useful, I'll have a play.  My general plan is to rotate the objects and keep the camera / lights in the same place each time.

Offline vilvoh

  • One of the good guys
  • Administrator (Inactive)
  • *
  • Posts: 4504
  • I'm the constructor, the architect
    • Escala real
Re: Blender Rendering Scripts
« Reply #3 on: June 04, 2009, 11:11:57 AM »
It doesn't matter if you rotate the camera+lights or the object, but take into account that probably you'll have more objects than cameras and lights in the scene. I mean, the lfewer objects, the easier to rotate them..

You may be also interested in this one called Render Cameras. Seems eaiser to use. You only need to place as many cameras as you want (in your case 2, 4 or 8), and the script will render the scene from all camera views, saving the output in a directory.


Offline The Hood

  • Devotee
  • *
  • Posts: 2889
  • pak128.Britain developer
Re: Blender Rendering Scripts
« Reply #4 on: June 04, 2009, 01:49:26 PM »
Thanks.  I'll have a look tonight hopefully.  I'm beginning to realise how little I know about blender!

Offline The Hood

  • Devotee
  • *
  • Posts: 2889
  • pak128.Britain developer
Re: Blender Rendering Scripts
« Reply #5 on: June 04, 2009, 09:51:55 PM »
OK I got somewhere with the first script but annoyingly the script rotates some of the objects in my model clockwise and some anticlockwise.  Why is this so illogical?

Offline vilvoh

  • One of the good guys
  • Administrator (Inactive)
  • *
  • Posts: 4504
  • I'm the constructor, the architect
    • Escala real
Re: Blender Rendering Scripts
« Reply #6 on: June 05, 2009, 06:34:33 AM »
I really don't know because I'ven't tried it. Does it have any options menu?

Offline The Hood

  • Devotee
  • *
  • Posts: 2889
  • pak128.Britain developer
Re: Blender Rendering Scripts
« Reply #7 on: June 05, 2009, 07:32:13 AM »
No, but I thought of a workaround that might combine the best elements of both scripts (the second script worked brilliantly except it doesn't allow the current lighting to work).  Unfortunately I thought of that workaround once I'd gone to bed!!! What does that say about me :S

Offline VS

  • Senior Plumber (Devotee)
  • Devotee
  • *
  • Posts: 4855
  • Vladimír Slávik
    • VS's Simutrans site
  • Languages: CS,EN
Re: Blender Rendering Scripts
« Reply #8 on: June 05, 2009, 08:04:26 AM »
It says that you are just like me :D

Offline The Hood

  • Devotee
  • *
  • Posts: 2889
  • pak128.Britain developer
Re: Blender Rendering Scripts
« Reply #9 on: June 05, 2009, 10:34:56 PM »
Hmm, it's annoying that the python commands don't have the same effect as the commands you have in the actual program.  I think I'll give up on this for now and just get on with drawing.  I wish they had a macro writer like in Excel - would be much more useful as a starting point!

Offline leopard

  • Devotees (Inactive)
  • *
  • Posts: 97
  • Life in the Urban Jungle
    • A Leopard's Tail
Re: Blender Rendering Scripts
« Reply #10 on: June 13, 2009, 10:38:18 AM »
can blender output to .pov files? or is there a converter?

thinking since pov has a variable that can be set on the command line, and since its input is pure text files its reasonably simple to use 'sed' to adapt files prior to rendering

Offline Spike

  • *
  • Posts: 1361
  • First Simutrans Developer and Graphics Artist
Re: Blender Rendering Scripts
« Reply #11 on: June 13, 2009, 10:51:26 AM »
There is a converter, but when I tried to use it, the results were a bit disappointing. Povray has quite different ways to work with light and textures, Blender data is kind of hard to convert to something Povray can use. Povray itself has superior rendering abilities, IMO, so I blame the converter and not Povray for the troubles.

Edit: With Povray it's easy to render 4 or 8 views of an object, either by the "clock" variable, or by a scene file that shows the object 4 or 8 times in the needed rotations.

Offline jamespetts gb

  • Simutrans-Extended project coordinator
  • Devotee
  • *
  • Posts: 18547
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Blender Rendering Scripts
« Reply #12 on: July 17, 2010, 03:39:34 PM »
I have been looking into automation, or at least, semi-automation, of the rendering process of late, and came upon this thread that looks as if it should be of some interest (although, as stated in my reply to that thread, I cannot get that program to work, and it only works in Windows, which may not be much use for The Hood).

The fiddliest bit is not, I find, the exporting from Blender (which is easy if one uses CTRL to constrain the rotations to the nearest 5 degrees, in which case an export to individual black-background .png files can take less than a minute for a single vehicle), but the processing of the graphics from the files exported by Blender into something that Simutrans can use. I have generally done this by cutting the vehicle graphic itself out of its black background (using select all by colour then invert), then pasting it over a series of similar vehicle graphics in an already set up .png file. However, this is very difficult, because it requires precise manual alignment of each individual rotation, and then pixel by pixel editing of any overspill of the underlying graphic.

I thought that a solution might be to use one graphic file per rotation, so as to eliminate the alignment and pixel editing that consumes most of the time in graphics processing. To do that, I first had to ensure that the rotations were exactly centred, and that the pivot point was therefore in the right place. I am not sure whether I have used the best technique for this, but I tried so far the "median point" and "bounding box centres" options for the pivot point. Is there a way, I wonder, to select 0,0,0 automatically using the 3d cursor? And, if there is, are all the graphics so aligned so as to make 0,0,0 the centre such that pivoting from that point will produce even results?

The next step was to use the flood fill tool in the Gimp to replace the black with the correct blue colour for the background of each picture: this was a fairly quick operation, but did require the "threshold" to be reduced from its default of 15 to 0.

Having tried that and imported the graphics, I found that the alignment was not correct: the vehicles (a two car class 456 EMU) appeared to float a metre or two off the track. Comparing that with the alignment of a similar vehicle (the class 319), it seems that vehicle graphics need to be lower than the centre of the picture for them to be aligned correctly with the ways. However, I do not know how to automate or semi-automate this process of lowering. By how much do they need to be lowered? Is it better to lower them in Blender along the z axis, or to make adjustments in the GIMP?

I should be interested in anyone's thoughts on this, especially since an effective workflow could make the production of graphics for Pak128.Britain considerably more efficient and therefore the numbers of such graphics released over any given time period considerably greater.

Offline skreyola

  • *
  • Posts: 337
  • Trivial Troubleshooter
Re: Blender Rendering Scripts
« Reply #13 on: July 20, 2010, 06:06:31 AM »
Another quick tip for rotation in Blender: numpad 4 or 6 will rotate the view by 15 degrees, so to go from one ST direction to the next, you'd just hit one of them three times.

Though, I think it would make more sense to set up eight cameras and lock their positions and rotations so they're in the right places already. Then you could just cycle through the cameras and render from each one without fiddling with that step. Seems to me that finding the right distance and angle would be the fiddliest part of the process. I'm feirly new to Blender.

Offline jamespetts gb

  • Simutrans-Extended project coordinator
  • Devotee
  • *
  • Posts: 18547
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Blender Rendering Scripts
« Reply #14 on: July 25, 2010, 12:27:47 AM »
Some experimentation has revealed that selecting all objects and setting moving them by -4.0 on the z axis seems to get them at least more or less in the correct vertical alignment.

Edit: Here are some sample images:

Edit 2: This technique, unfortunately, does not work, as the images are not correctly aligned relative to each other when they are produced. Does anyone have any ideas as to how to refine this technique in order to enable it to work effectively? The alternative is very time-consuming and greatly reduces the number of graphics that can be produced in any given time.
« Last Edit: July 25, 2010, 06:12:37 PM by jamespetts »