Jump to content

Export animation data to JSON


greyday

Recommended Posts

I'm working on a project that was previously using Cinema4D to export to Unity, then we were exporting to JSON from Unity.  Very laborious and too many pieces.  A friend of mine suggested we might be able to by pass the middle man... use Houdini through and through and export to JSON. The animation is quite simple.. we'll actually be using the data to move an object with a motor in it.  

We animate in 3D and export the data to a physical object.

Is there a way to do this with Houdini using some sort of JSON export capabilities or could we use Python somehow?

Thanks for any advice.  I'm looking for options really.

Link to comment
Share on other sites

I've just been working on an exporter to write out geometry descriptions and animation data for a realtime renderer and found it was quite manageable using python, the thing that can get tricky to get right is the formatting - square brackets, curly brackets, commas etc.

This site might come in very handy for checking your output :

jsonlint.com

Link to comment
Share on other sites

Thanks everyone.  I'm pretty new to the whole thing.

Basically what I want to be able to do is animate a 3D object in Houdini (basic things like rotations and translation) export those values to JSON.  Then the JSON file is read in to an external program that will move the physical robot in the same way I've animated it in Houdini.

What should I read to be able to understand what f1480187 or even j00ey mentions?

Link to comment
Share on other sites

It depends on what the software you're reading it back in with wants.

I was writing it out for a custom renderer so the developer gave me a template to use as a guide and I matched that, filling in the data from geometry attributes and transforms etc.

I guess if you just want translation, rotation etc you can use worldTransformAtTime() to get the transformation matrix at a given frame, then use extractTranslates(), extractRotates() to get the values for each frame [or time step] that you want to sample.

Check how the reader expects your JSON to be formatted and match that - eg [if my memory serves] I had to write the point positions as one long array of comma separated values that the reader parses in groups of 3 [x, y, and z], so that meant looping through the points, getting the positions, stripping off the brackets, appending to the array then putting square brackets around the array. The site I posted last time was very useful for checking I hadn't missed a comma or bracket somewhere.

If you have experience with python I think it should be fairly straightforward, if not [like me] use the docs...

Link to comment
Share on other sites

I made you a basic file to start you off - I'm not very advanced in python myself so don't take this as the proper way to do something, it's just a way...

If you open a python shell and click on the 'Print JSON' button, you should get some JSON printed out. Look in the python module of the asset to see how it's set up.

basic_json.hip

Link to comment
Share on other sites

@greyday, show a simple example JSON readable by external program. The task is simple, but requires to know Python and HOM. The problem of different coordinate systems will also be raised, probably.

If you are ready to spend couple of weeks, you can learn Python tutorial (at least, parts 3 through 6), then learn basics of json module (dumping simple dicts and lists on disk). I linked Python 3 pages and Houdini uses Python 2. There won't be any difference in this case, and it is reasonable to learn Python by most recent docs, IMO. Feel free to switch versions at the top bar of the Python site.

Then read HOM introduction, find way to access animation values you need. Finally, combine everything in the final project. HOM will be the most tricky: docs are unfriendly. But you can ask here.

 

@j00ey, try json module instead of manual formatting:

data = {
    'time': time,
    'objects': []
}

for obj in objs:
    t, r = get_wt(obj, time)
    obj_data = {
        'name': hou.node(obj).name(),
        'translate': list(t),
        'rotate': list(r)
    }
    data['objects'].append(obj_data)

print json.dumps(data, indent=4, sort_keys=True)

 

Edited by f1480187
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...