News:

Simutrans Wiki Manual
The official on-line manual for Simutrans. Read and contribute.

Blender Rendering Scripts

Started by The Hood, June 04, 2009, 10:03:27 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

The Hood

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...!!!

vilvoh

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.


# 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()

Escala Real...a blog about Simutrans in Spanish...

The Hood

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.

vilvoh

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.


Escala Real...a blog about Simutrans in Spanish...

The Hood

Thanks.  I'll have a look tonight hopefully.  I'm beginning to realise how little I know about blender!

The Hood

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?

vilvoh

I really don't know because I'ven't tried it. Does it have any options menu?

Escala Real...a blog about Simutrans in Spanish...

The Hood

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

VS

It says that you are just like me :D

My projects... Tools for messing with Simutrans graphics. Graphic archive - templates and some other stuff for painters. Development logs for most recent information on what is going on. And of course pak128!

The Hood

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!

leopard

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
My Simutrans on the apple mac homepage http://www.aleopardstail.com/simutrans/Index.html

Spike

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.

jamespetts

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.
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

skreyola

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.
--Skreyola
You can also help translate for your language with SimuTranslator.

jamespetts

#14
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.
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.