Problems in understanding Key and KeyBlock

Hi,
I`m about to write an export-script for a own file format. For this, I need to export animations. But I got big problems to understand the Key and KeyBlock modules in the Blender Python API Reference (perhaps because of my bad English :wink: ).
For example I have a simple scene with an animation. When I call

            keys= Blender.Key.Get()
				for foo in keys:
					bar= foo.KeyBlock.Get()
					print(bar.name)

the console shows nothing. I guess, that I got the whole thing wrong.
I should mention, that until now, I write all (!) Keys (usually 1-250) and all transformations to my file. That leads to way to big files, so I`m searching for a possibility to read out the user-set keys.

Thank you for your help!

Hi, La_Boda.

Here is a working script which prints the names of the selected object Vertex-Keys on the console.


import Blender
from Blender import Object, NMesh

# Get selected

obj = Object.GetSelected()[0]
mesh = obj.getData()

shapekeys = mesh.getKey().getBlocks()

for s in shapekeys:
	print s.name

Use 'Object’s to get their Keys. :wink:

You could get the Vertex-Keys via “NMesh.NMesh.getKey().getBlocks()”.

Mmmh. It’s confusing.
Do anyone know why Blender distinguish between ‘Key’ and ‘KeyBlock’?


Siebeneicher

— Edit 1 —

You could get the Vertex-Keys via “NMesh.NMesh.getKey().getBlocks()”.

Mmmh.
You’ll need an instance of ‘NMesh.NMesh’.
:expressionless:

Is there a missunderstanding?

What do you really want?

  1. To find Keys for an given Object …
  2. To find Objects, which have Keys …

Hmm I try to do the first thing. Thank you for your help, until now I couldn´t test it, but I will post, if I succeed!

It doesn`t work :frowning:
In line “shapekeys = mesh.getKey().getBlocks()” the console says “Attribute Error: ‘NoneType’ object has no attribute ‘getBlocks’”
But before I do tset, if the object is a mesh. And there are also keys set…

part 1 …

If you run it, you’ll find there are objects with keys and another objects without keys. (Please correct me if I am wrong.)


import Blender
from Blender import Scene, Object, NMesh

def main():
	# (1)
	scn = Scene.GetCurrent()
	print "Current scene:", scn
	

	# (2)
	obj_list = scn.getChildren()
	for o in obj_list:
		d = o.getData()
		if dir(d).__contains__('key'):
			
			print "Object '%s', containing data of type '%s': Data contains key of type '%s'" % (o.name, type(d), type(d.key))
		else:			
			print "Object '%s', Containing  data '%s': does the data have keys?" % (o.name, type(d))
			
main()

part 2 …

Mmmh. This Case has been covered by the program above, but it’s shows an special-case.

(a)
Create a Cube within the Blender-GUI.
Select it.

(b)
Run the script below
-> What says the console?

(a)
Select the cube. Create a Key with ‘Add Shape Key’-Button.

(b)
Run the script below
-> What says the console?


import Blender
from Blender import Object, NMesh

def Main():
	# Please select Mesh-Object ;)
	o = Object.GetSelected()[0]
	d = o.getData()
	print "Object '%s' with data '%s' has key type '%s'" % (o.name, d.name, type(d.getKey()))

Main()


Siebeneicher

But before I do tset, if the object is a mesh. And there are also keys set…

:o … %|

Wow, Vielen Dank! :Z

But it still doesn`t work correctly. I always set the keys by pressing “i” and because of this I do not know a “Add Shape Key”-Button :expressionless: Where can I find it? But anyway it should work with the “i”-Button, too, or are the keys created with this menu different fromt the “Add Shape Key”-keys???

€: Your test-scripts always say that the keys are of type “NoneType”, when I set them up by pressing “i” and the “Rot” (and slide the frames bar etc…) When I press “i” and select “Mesh”, then your scripts answer “Blender Key” - that seems to be right…

Hehe. Gern geschehen.

You mean some type of keys which are bound to objects.
Maybe, you are searching for ‘Ipo-Keys’.

Mmh. There are no real Ipo-Keys, only Ipo-Curves.
Ipo-Curves do control object-transformations (and other) within an animation.

(Probably, you already know)


Siebeneicher.

PS:

It was a missunderstanding. %|

I mean ‘Vertex-Keys’ or ‘Shape-Keys’.
These keys are bound to Object-Data, not to Objects.

Here is the “Add Shape Key”-Button.
A Mesh-Object must be selected.
http://img125.imageshack.us/img125/4823/rvk1ot.th.jpg

They may be activated with ‘i’-Button … choosing ‘Mesh’.

OK, thanks again for the help. Now I understand the difference between Ipo-Curves and Mesh-Keys.
For my animation-script, above all I need only those three things: scaling, rotating and shifting of objects. I already suceeded in reading out all Ipos. But how can I find out, which Ipo-Objects belong to which meshes? And then finally, how can I read out, in which frame what transformation to the mesh is done?

Sorry, many questions, but I`m completely new Blender-Python-scripting and I have only very basic knowledge in Blender itself…

Am learning Blender-Python, too.

There are many who know much more about things like animation and Ipo (within BPY).

But how can I find out, which Ipo-Objects belong to which meshes?

There is a way to get Ipo’s from Objects.

In this program I try to access the lpo of each object (of tht current scene). It seems to work.


# Ipo-Test
import Blender
from Blender import Scene, Object, Ipo

def Main():
	scn = Scene.GetCurrent()
	render_context = scn.getRenderingContext()
	fps = render_context.framesPerSec()
	obj_list = scn.getChildren()
	
	for o in obj_list: 
		print
		print
		print "--- --- --- ---"
		print "Current Object:", o.name
		ipo = o.getIpo()
		if ipo == None:
			print "No IPO for this object specified"
		else:
			for c in ipo.getCurves():
				print
				print "Current Ipo-Curve:", c.getName()
				print "It has", len(c.getPoints()),"controlpoints"
				# print "It has", len(c.getPoints()), "control points (Bezier-Triples)"
				print "After 1.0 secounds", c.evaluate(1), " --- The tenth 10 frame", c.evaluate(10.0/fps)

Main()

Good luck,
Leonard Siebeneicher

OK many thanks to you again.
I think I got enough information to do what I planned. When I do not forget it, I will inform you about the success of my project :wink:

Thanks,
Boda