Posted by Christopher Diggins, 19 December 2011 10:38 am
Almost anything is possible in code, given enough time. However given the right technologies and libraries then coding can be quick and easy! Python is a great example of this. The Python standard libraries make many programming tasks that are time consuming and error prone in C++ very easy. For example creating a web-server and converting numerical data to JSON.
At Autodesk University I gave a demonstration of a Python API that served 3D content in JSON to a web-browser. After giving the demo I realized that just showing JSON data in the browser is a bit underwhelming, so decided to render the JSON data using WebGL and to extend the API to the FBX SDK as well.
The point of the demo I gave at AU was not the web-server, but rather to show how easy it is to build a cross-product Python API around the various M&E SDKs. For this blog post I'm going to skip the details of the cross-product Python API and focus on a particularly interesting part of the demo: serving 3D content over the web from a FBX file using nothing more than the FBX Python SDK.
What you need to know about the cross-product Python API I made is that it is in a Python package called "autodesk". When this package is imported it looks at an environment variable in order to load the correct abstraction layer. In this case the wrapper around the FBX Python API "fbx_common". Effectively it is a wrapper around the FBX SDK.
The following video and steps show how to run the 3D web-server scripts using the FBX Python SDK.
import fbx
dir(fbx)import init_autodesk_fbx
import osprint os.environ['ADSKPRODUCT’]”import autodesk
autodesk.app.write_line('hello world')autodesk.app.load_file('teapot.fbx')
import output_sceneoutput_scene.output_scene()import server3dserver3d.serve()”http://localhost:8000/test
http://localhost:8000/tree
http://localhost:8000/mesh
http://localhost:8000/demo
http:///localhost:8000/exit
If everything worked out for you at this point you can start having fun playing with scripts and adding new features. The files you probably want to look at in order are:
The server3d uses an instance of the default TCPServer class in the Python library and a custom HTTP request handler derived from BaseHTTPServer.BaseHTTPRequestHandler. The custom request handler looks at the URL of HTTP "GET" requests, and depending on the text chooses to serve different content. The HTTP response is handled by writing to a member variable of the base class called "wfile".
When the URL contains the text "demo.html" (e.g. http://localhost:8000/demo.html") the contents of the file "demo.html" is copied to the "wfile" member variable. When served to the web-browser, a JavaScript function in that file will send an XmlHTTPRequest back to the web-server for mesh data:
// Request the mesh data from the web server
req = new XMLHttpRequest();
req.open("get", "mesh" + qs, false);
req.send();
var json = JSON.parse(req.responseText);
When the server receives a URL containing the text "mesh" it will return a JSON representation of the mesh data.
# Return a JSON representation of the mesh of the first selected object or the named object.
elif '/mesh' == path:
try:
# Check is a node name has been specified
if 'name' in params:
name = params['name'][0]
node = node_by_name(name)
else:
node = autodesk.app.first_selected
if not node:
node = first_node_with_mesh()
jsondata = node_mesh_to_glge_json(node)
except:
jsondata = 'Exception: ' + exception_string()
content.write(jsondata)
I could go into more detail, but I think reading and experimenting with the code is the most effective thing you can do.
I hope this inspires you to start thinking about new and interesting ways to use the FBX Python SDK!
Note that the server3d.py module should work also from 3ds Max 2012 Subscription Advantage Pack using an update Python script launcher plug-in and from MotionBuilder 2012 and Maya 2012. In 3ds Max the environment variables needed by the "autodesk" pacakge are set by the Python plug-in, but for MotionBuilder you will have to call "import init_autodesk_mobu" and for Maya you will have to call "import init_autodesk_maya" before importing the server3d module.
Please only report comments that are spam or abusive.
Comments
There are currently no comments for this post. Be the first to comment!
Add Your Comment
You must be logged in to post a comment. Login or Register here