script for automatically inserting shape keys into action

Hi All, I am trying to write a python script that takes an input in some array and automatically insert key frames for shape keys. Since I am new to scripting, I dont have exact idea of modules. Please give me pointers for the following:

  1. Read entry from array --> This is usual python and I know what to do.
  2. Given a frame index, shape key index and a float, I know how much of a shape key is to be keyframed at given frame. --> How do I insert that key frame using python?

Thanks much in advance for the help…

a good start may be to look at the MDD importer, that imports shape keys into blender, its included in blenders CVS that youll need to run it.

Where do i find this script? I am stuck at the same question 2, and i cannot figure out how to set the value of the key (the slider) from a script.

If anyone know, please tell me…

keep track of new scripts in Blender
http://projects.blender.org/viewcvs/viewcvs.cgi/blender/release/scripts/?cvsroot=bf-blender&sortby=date#dirlist

Thanks, those will come in handy…

One question though: This script is working with Mesh (not NMesh), but Mesh does not have a key variable or a getKey() function like NMesh. If i try to do the same as in this script, writing ‘me.key’ with ‘me’ a Mesh will throw an exeption in the interactive console saying that Mesh has no attribute ‘key’.

This is Mr** larry smith ,the CEO of MobilePhones& computersDirectLimited.
We are legit,and registered company from uk,We are the leading
exporter&factory for mobile phone and Portable DVD,Car
DVD,MP3,MP4,LCD TV,Plasma TV,IP phone,USB Phone,bluetooth
earphone,bluetooth USB dongle,Laptops,etc.

we have all brands of Mobile
Phones,Ipods,xbox
360, Sidekicks,Nextels phone,Laptops for sell at cheap and
affordable prices, they ranges from Nokia/Samsung/LG/Sony
Ericsson/Motorola/Alcatel/panasonic With Bluetooth, all
Brands and Models of Nextel Phones, we want you to get back
to us with your quote so that we can begin a good business
relationship.

EMAIL ADDRESS: [email protected]
*
WE ARE SELLING ALL KINDS OF MOBILE PHONES & LAPTOP LIKE:
SONY VAIO A217S-- 100GB-- 512MB RAM-- XP HOME-------------$300
SONY VAIO B1VP-- 40GB HD-- 512MB RAM-- XP PRO--------------$330
SONY VAIO T370P/L-- 60GB HD-- 512MB RAM-- XP----------------$200
SONY VAIO A215Z 60GB HD-- 512MB RAM-- XP------------------$2450
SONY VAIO A397XP-- 80GB HD-- 512MB RAM-- XP----------------$300
SONY VAIO B100B08 60GB HD-- 512MB RAM-- XP---------------$250
SONY VAIO B100B08 60GB HD-- 512MB RAM-- XP---------------$400
SONY VAIO FS295VP 80GB HD-- 512MB RAM-- XP---------------$350
SONY VAIO FS215Z 100GB HD-- 512MB RAM-- XP---------------$350
SONY VAIO A417M 80GB HD-- 512MB RAM-- XP-----------------$450
SONY VAIO B1VP-- 40GB HD-- 512MB RAM-- XP PRO-------------$200
SONY VAIO T370P/L-- 60GB HD-- 512MB RAM-- XP PRO----------$400
SONY VAIO LAPTOP-- VGN-A117S---------------$300
*
All Toshiba laptops
Toshiba Satellite PRO L10--------------- $320
Toshiba M200--------------- $500
Toshiba R100--------------- $450
Toshiba Qosmio E10--------------- $650
tshiba Satellite PRO L20--------------- $250
Toshiba M100--------------- $680
Toshiba M300--------------- $740
Toshiba Portege--------------- A200 $320
Toshiba Satellite L10--------------- $330
Toshiba Qosmio F20--------------- $500
*
Dell Laptops
Dell Latitude D600--------------- $290
Dell Latitude D500--------------- $200
Dell Inspiron 6000--------------- $350
Dell Latitude D505--------------- $340
Dell Latitude D610--------------- $460
Dell Latitude D510--------------- $320
Dell Inspiron 9300--------------- $530
*
ipods
20GB iPod 20GB iPod …45USD
Apple 4 GB iPod Mini Pink M9435LL/A …40 USD
Apple 40 GB iPod photo…40 USD
Apple 4 GB iPod Mini Silver M9160LL/A …40 USD
Apple 60 GB iPod Photo M9830LL/A…60 USD
Apple 60 GB iPod photo …55 USD
Apple 30 GB iPod Photo M9829LL/A…50 USD
Apple 512 MB iPod Shuffle MP3 Player…40 USD
Apple 4 GB iPod Mini Blue M9436LL/A…45 USD
Apple 2 GB iPod Nano…50 USD
Apple 4 GB iPod Nano…60 USD
Apple 30 GB iPod Vidoe…110 USD
Apple 60 GB iPod Vidoe…150 USD
*
play station 1… $120
play station 2 …$130
play station 3…$150
x_box 360…$200
GARMIN 396…$150
*
Here are thePrice list for different products;
NOKIA 7600(UNLOCK)–US$170
NOKIA 8910 Titanium-- US$100
NOKIA 8850 SPECIAL EDITION-- US$105
NOKIA 8850 GOLD EDITION-- US$100
NOKIA 8910 Black-- US$110
NOKIA 9210 Communicator-- US$110
NOKIA 9210i Communicator-- US$110
NOKIA 8910i-- US$150
NOKIA 9500 AT JUST $195usd,
NOKIA 8850 GOLD EDITION-- US$120,
NOKIA 9300 AT JUST $160usd,
Nokia N70 At JUST $140usd
NOKIA N80 AT JUST $180usd
NOKIA N90 AT JUST $200usd
NOKIA N91 AT JUST $220usd
NOKIA N92 AT JUST $245usd
NOKIA N93 AT JUST $270usd
NOKIA N73 AT JUST $210usd
Nokia Vertu 155$
Nokia 5140i 125$
Nokia 6230i 115$
Nokia 6021 105$
Nokia 6030 115$
Nokia 6680 125$
Nokia 6681 120$
Nokia 6101 110$
Nokia 6822 110$
Nokia 7710 110$
Nokia 6170 145$
Nokia 6260 145$
Nokia 3510i 95$
Nokia 6630 120$
Nokia 7260 120$
Nokia 7270 110$
Nokia 7280 120$
Nokia 6670 110$
Nokia 6020 120$
Nokia 3220 65$
Nokia N-GAGE QD 75$
Nokia 7610 120$
Nokia 5140 85$
Nokia 6610i 120$
Nokia 7200 133$
Nokia 6230 145$
Nokia 6820 120$
Nokia 7600 105$
Nokia 6600 120$
Nokia 6800 105$
Nokia 6220 120$
Nokia 6620 120$
Nokia 7250i 65$
Nokia 8910i 120$
Nokia 6100 95$

SONY ERICSSON P800(UNLOCK)-- US$180
SONY ERICSSON P900(UNLOCK)—US$200
SONY ERICSSON P980i(UNLOCK)—US$200
SONYERICSSON P990 JUST FOR …$210USD
SONY ERICSSON W800i AT JUST $140usd
SONY ERICSSON W900i AT JUST $220usd
SONY ERICSSON S700i AT JUST $125usd
Sony Ericsson P910i…270 USD
Sony Ericsson T230…80 USD
Sony Ericsson T310…80 USD
Sony Ericsson T610…100 USD
Sony Ericsson Z1010…180 USD
Sony Ericsson Z200…90 USD
Sony Ericsson Z600…130 USD
Sony Ericssson T630…130 USD
Sony Ericsson S700i…170 USD
Sony Ericsson S750i…170 USD

MOTOROLA MPX 220 AT JUST $120usd,
MOTOROLA MPX 300 AT JUST $160usd,
MOTOROLA V661 AT JUST $145USD
Motorola A388C…170 USD
Motorola A760…250 USD
Motorola A768…260 USD
Motorola A768i…200 USD
Motorola A780…290 USD
Motorola C550…90 USD
Motorola C650…100 USD
Motorola E365…100 USD
Motorola E398…120 USD
Motorola E680…240 USD
Motorola RAZR V3…270 USD
Motorola V220…170 USD
Motorola V303…100 USD
Motorola V400…150 USD
Motorola V500…150 USD
Motorola V501…200 USD
Motorola V525…150 USD
Motorola V600 (OEM) w/ Bluetooth Headset…260 USD
Motorola V600 OEM…180 USD
Motorola V690…170 USD
Motorola V750…180 USD
Motorola V80…200 USD
Motorola V80 with Bluetooth…260 USD
Motorola V872…200 USD
Motorola V878…180 USD
Motorola V300…150 USD
SAMSUNG D500 AT JUST $160usd,
SAMSUNG D415 AT JUST $120usd
Samsung E600…140 USD
Samsung E800…180 USD
Samsung P510…120 USD
Samsung SGH-D410.200 USD
Samsung SGH-D500…270 USD
Samsung SGH-E700…150 USD
Samsung SGH-E715…170 USD
Samsung SGH-P100…130 USD
Samsung SGH-P400…95 USD
Samsung SGH-P408…240 USD
Samsung SGH-P730…150 USD
Samsung SGH-S200…90 USD
Samsung SGH-S300…70 USD
Samsung SGH-S300M…100 USD
Samsung SGH-S500…100 USD
Samsung SGH-V200…110 USD
Samsung SGH-X400…100 USD
Samsung SGH-X430…100 USD
Samsung SGH-X600…100 USD
Samsung X450…100 USD

Siemens
A50--------------- $23.00
ME45--------------- $29.00
SL55 $65.00
SL6--------------- $126.00
CFX65--------------- $104.00
XdaII US--------------- $200
Treo 650--------------- US$350

NEXTEL 1930 JUST FOR…$120USD
NEXTEL i870at JUST FOR …$140usd
NEXTEL i450 JUST FOR …90usd
NEXTEL 1860 JUST FOR …$110USD
*
SIDEKICK 2 JUST FOR …$120USD
sidekick 1 JUST FOR … $100 USD
T-MOBILE SIDEKICK 3 JUST FOR $150USD
*
for more info, and order inquiry,pls feel free to contact us or
Send ur email at [email protected]
+447029597212
*
thanks for you inquiry,as we look forward in placing your
order,and giving you the most competent services.
Best regard

for MDD import I added a mesh.key variable for Mesh, (but thats only in the CVS)

Oh ok… So, I can not write the script using Mesh in 2.42a?
btw, Bernoulli, are you going to write the same script :slight_smile:

I am writing a script that will animate ocean surface using data that are simulated in Matlab. However, i will only have samples for each 10th frame or so, so using shape keys will give it a smooth motion.

I have allready written the script, with NMesh, but since i dont have my data yet, i am animating simple harmonic waves just to make sure the shape keys insertion works. It look good but the code need some tweaking.

That is very nice script. Is your script available somewhere?

Here it is. I left out some stuff though, but it needs some tweaking. I am going to create more functions (for cosmetics), and see if i can set some more attributes for the object.


import Blender as B
import math
import random


def create_nmesh(Xres, Yres, Xrange, Yrange, name):
	mesh = B.NMesh.New(name)
	for i in range(Yres+1):
		y = i*float(Yrange)/Yres
		for j in range(Xres+1):
			x = j*float(Xrange)/Xres
			mesh.verts.append( B.NMesh.Vert(x,y,0) )
	aface = range(4) # Just to have a length 4 list
	for i in range(Yres):
		for j in range(Xres):
			aface[0] = mesh.verts[ i*(Xres+1)+j ]
			aface[1] = mesh.verts[ i*(Xres+1)+j+1 ]
			aface[2] = mesh.verts[ (i+1)*(Xres+1)+j+1 ]
			aface[3] = mesh.verts[ (i+1)*(Xres+1)+j ]
			mesh.faces.append(B.NMesh.Face(aface))
	mesh.update()
	return mesh

def update_waves(mesh, time, k, theta, w, amp):
	# test version
	# Using n = len(k) harmonic waves
	for v in mesh.verts:
		x = v.co[0]
		y = v.co[1]
		z = 0
		for i in range(len(k)):
			z += amp[i]*math.sin( k[i]*math.cos(theta[i])*x + k[i]*math.sin(theta[i])*y - w[i]*time )
		v.co[2] = z
	mesh.update()



############
##  MAIN  ##
############

# "Delete" redundant objects
scn = B.Scene.GetCurrent()
#delete_objects(scn)
for o in scn.getChildren():
	type = o.getType()
	if type != 'Camera' and type != 'Lamp':
		#o.removeProperty('Name')
		scn.unlink(o)

frame = 1  # Start frame

# Create nmesh, object, and links
Xres = 50
Yres = 50
Xrange = 10
Yrange = 10
name = 'myNMesh'
mesh = create_nmesh(Xres, Yres, Xrange, Yrange, name)

ob = B.Object.New('Mesh', 'bolge')
ob.link(mesh)
scn.link(ob)

B.Window.EditMode(1)  # Unneccesary?
# Create shape key basis
ob.insertShapeKey()

#####################
## Animation data  ##
#####################

fps = 25        # frames per second
frame = 1       # start frame
samplerate = 5  # Number of frames per samples
numsamples = 30 # Total number of samples

############################
##  Wave Characteristics  ##
############################

n = 3 # Number of waves
amp = [float(1)/(i+3) for i in range(n)]
mean_angle = math.radians(30)
theta = [mean_angle + (random.random()-0.5)/1.0 for i in range(n)]
k = [2 - 0.5**(n-i) for i in range(n)]
g = 9.81
h = 10 # Sea depth
w = [math.sqrt(g*k[i]*math.tanh(k[i]*h)) for i in range(n)]

#####################################
##  Set animation and create keys  ##
#####################################

key = mesh.key  # Create a key
# if not key, bla bla
key.ipo = B.Ipo.New("Key", "minIPO") # An ipo-curve for the keys
ipo = key.ipo

# Create shape keys and set their ipo-curve
for i in range(numsamples):
	frame += samplerate
	time = float(frame-1)/fps 
	# Update nmesh
	update_waves(mesh, time, k, theta, w, amp)
	ob.insertShapeKey()
	curve = ipo.addCurve(ipo.curveConsts[-1])
	curve.append( (frame, 0.5) )
	curve.append( (frame-samplerate, 0) )
	curve.append( (frame+samplerate, 0) )
	curve.setInterpolation("Linear")
	curve.recalc()


ob.sel = 1
B.Window.RedrawAll()

Thank you very much for the script. I will play around with it this weekend.

Is there really no way to do this without going to the CVS version? I want to do something very similar on a commercial project, and we don’t like to use unstable software on a production, so we’re sticking to the release versions.

You do not need the cvs version for the script above.

Oh, I managed to get it working in my script the same way you did it! Thanks :slight_smile: