# how to change a mesh given by mathematical function with time i.e. how to include the time parameter in the mathematical equation?

I am trying to make an animation in which a mathematical function changes as a function of time. Given below are two images which are z math surfaces(from the "extra objects" add on) whose equations are:

The first image should change in the other for which the mathematical function can be given as $\sin x+t\cos y$, where $t$ varies from $1$ to $-1$

This is a simple mathematical surface parametrized in $t$ ($t$ obviously is a function of time and in our case a function the frame number).

Here I have picked up simple equations just to get the idea of implementing it and I actually intend to use it for more complex functions and even for the XYZ math surface. How do I go about it? (I would prefer a way without scripting but if I have to use scripting then please tell me in detail as to how to proceed with it.) I'm not aware of a simple way to do this without scripting, so here's a scripting based solution, using a frame change app handler.

frame_change_pre app handlers run before every frame change, so they're ideal to update a mesh every frame.

This function alters the active object's vertices Z values according to the equation z = sin( x ) + t * cos( y ). Here, t is a normalized value that maps frame numbers between 1-100 to values between -1 and 1.

import bpy
from math import sin, cos

def update_z_from_equation( scene ):
o = scene.objects.active

frameMin, frameMax = 1, 100
tMin, tMax         = -1, 1

t = tMin + ( scene.frame_current / frameMax ) * ( tMax - tMin ) * tMax

for v in o.data.vertices:
v.co.z = sin( v.co.x ) + t * cos( v.co.y )

bpy.app.handlers.frame_change_pre.append( update_z_from_equation )


To use this add a grid plane, subdivide a few times, scale X5 in edit mode, then run the script once and keep the plane selected and active.

• It is also possible to add keyframes to each vertex. – sambler Aug 7 '17 at 12:41
• Didn't know that, cool! – TLousky Aug 7 '17 at 12:53
• Right as usual @batFINGER, will fix – TLousky Aug 7 '17 at 13:26

I would suggest using Animation Nodes for more interactive results.

The Grid Mesh node generates evenly spaced vectors (Which can act as your 2D coordinates plane). I separate the vectors into their x,y,z components (Z is zero in the grid), for every vector I compute the equation you provided above in terms of the x and y values I extracted, then I form a new vector from the x, y and z where z is the value we computed, resulting in points that satisfy your function: In order to make it a 3D mesh with faces, we use the original polygon indices and new vectors to make a mesh as follows, this is just how meshed are output to object: Last step is to introduce the (t) in your equation, which will be time, we can use the animate float node with the starting and ending values -1, 1 respectively as you stated in the question. And of course Duration is the duration of the animation. Resulting in the animation as you see: If you want to color the surface based on its z value, where the higher the point is the higher the red value and the slower it is the bluer it is, you can use the following shading node tree: • this method definitely needs me to learn a lot. Could you suggest me some topics from the manual to read for a good start. I have never used the node editor before.It also promises to give me freedom to control my mesh surface mathematically. – Gouri Shanker Aug 7 '17 at 13:44
• @GouriShanker I edited the question for more details, please see the documentation here for more information on each node and information on how to install Animation Nodes: animation-nodes-manual.readthedocs.io/en/latest – Omar Emara Aug 7 '17 at 15:09
• I could follow till end but then how should I actually change the time during the animation. when I am running the animation the frames are changing and the other set keyframes are responding as they should be. But the function is not changing. I tried to set key frames to the time node but it did not help. – Gouri Shanker Aug 12 '17 at 14:56
• @GouriShanker Can you show me your node tree? – Omar Emara Aug 13 '17 at 9:37
• sorry, I could not update due to net connection issues. I could work out the last part by using the 'time info' node. thanks. – Gouri Shanker Aug 13 '17 at 9:47

Join as Shapes.

Make one mesh with your equation (1) Then make another with equation (2), making sure x and y subdivisions remain the same. You need to have the same amount of verts to join as shapes. Select both meshes and choose Join as shapes from shape key extras. The Basis shapekey will be eqtn (1) and the other eqtn (2). Here is an example with 0 at frame 1, 1 at end frame. • This definitely seems to be the simplest one but with may limitations. Here seems to work fine because the two functions happen to be quite similar and hece the intermediate shapes of the function seems to follow the desired equation. I doubt if we actually have good mathematical control over in between shapes. Nevertheless it was a really great help as I can solve my problems in many other cases in a very neat and simple manner> – Gouri Shanker Aug 7 '17 at 13:33
• In hindsight would suggest using z = 0 as the Basis shape. If two functions are used g(x, y) and h(x, y) then the shape keys will equate to w1 * g() + w2 * h() where w1, w2 are the key values of shapes g and h respectively. As long as w1 + w2 = 1 and both are in range [0, 1] should give reasonable result. – batFINGER Aug 7 '17 at 13:49
• seems like I will have linear combinations of the two (or more) functions. Can this way of using the shape functions help me to model functions like:-sin(kx-wt) i.e when the time parameter is not a linear coefficiants? – Gouri Shanker Aug 7 '17 at 14:38
• For non-linear combinations, just add more shapes, with the equation for that frame, the shape keyed to 1 on that frame. Eg in the least linear case, if time was in frames, sin(kx) on frame 0, sin(kx - w) on frame 1.... which would be a PITA doing via UI, but would be a simple script to put together, which can do if you are interested. – batFINGER Aug 7 '17 at 15:15