Python: "Attach" mesh to bone

Hi!

I am trying to make an import-script for Half-Life 2 SMD files. There’s an old script from 2005 written by 3d2toy that can be found at http://www.3d2toy.com/downloader.php?ID=47, but it doesn’t work (because of the updates of the blender python API).

I have been able to:

  • Import the bones and create an armature.
  • Import the mesh (with faces).
  • Make a vertexgroup for EACH bone, and assign vertices to it (with the weight each vertex is deformed)But how do I get the Armature to deform the mesh? I have given the same name to each vertexgroup as the bones that shall deform it. The Armature has Vertex Groups enable under Deform Options.

When I move the bones around in pose mode, the mesh isn’t following. I have done some testing, and tried to search around in the documentation/tutorials, but haven’t found any solution.

What I need is a short explanation on how rig the mesh to the armature. (In python. I think I know how from a normal users side, it’s just been a while since I did it :p)

Thanks!

EDIT:
Figured it out, partly. I had to parent the one object (mesh) to the other (armature). I haven’t done it in python, and if anybody now how to, please post here. (Meanwhile I try figure it out.)

add an armature modifier to the mesh object. point the modifier to your armature object,

Thanks! I’ll try that right now.

ideasman42: it worked. I have currently been able to attach the mesh to the armature,BUT I have to save it as a .blend file and reopen it before it works.

Is there anyway I can make Blender aware of the change? (The modifier is drawn in the buttons window, and in the outliner)

you probably need to update the object data or scene

import bpy
bpy.data.scenes.active.update(1) # 1 is for a full update

and for your mesh object

ob.makeDisplayList()

I think only one of these is needed.

I had updated my meshobject, but not the scene.
After that little update it worked!
Thanks once again :slight_smile:

cool, this can be added to the docs. did .update(0) work or only .update(1)?

Have I missed the link to the script ?
Can you share your results ?

You can use this link to post:
http://projects.blender.org/snippet/

Thx :o

@ideasman42: bpy.data.scenes.active.update(0) is enough. When using it, my_mesh_object.makeDisplayList() is not needed.

@gabdab: I haven’t released it yet. There are two reasons for that:

  1. It’s not finished yet (TODO: resizing of model, importing texture coords, set normals correctly (I think blender does it more or less automatically), clean up the code)
  2. Some parts of the code is just copy/past of the old script. And I don’t want to release anything I haven’t made 100%. At least I think I should have a little chat with the creator, first.

Meanwhile, I can give you some images. These are the one model called ct_urban in Counter Strike: Source. It’s skin number 1 of the 4 CT’s. The SMD format does not contain information on how long the bones should be (just the head’s position, rotation ), so they are all at same length…

The rig:
http://jondrange.googlepages.com/ct_urban_rig.jpg

Posing:
http://jondrange.googlepages.com/ct_urban_moving.jpg

BTW: there’s a EXPORT script under developement at http://forum.moddb.com/thread/13334/ worth taking a look at.

I’ll come with updates later on. (In another thread, probably)

/JMD