Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1566 - trunk/SwigInterface


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1566 - trunk/SwigInterface
  • Date: Thu, 26 Jul 2007 15:26:05 -0600 (MDT)

Author: bigler
Date: Thu Jul 26 15:26:05 2007
New Revision: 1566

Modified:
   trunk/SwigInterface/pycallback.cc
   trunk/SwigInterface/pycallback.h
   trunk/SwigInterface/pycallback.i
   trunk/SwigInterface/wxManta.py
Log:

SwigInterface/pycallback.cc
SwigInterface/pycallback.h
SwigInterface/pycallback.i

  Adding support for animation callbacks.
  - callFromC_Animation()
  - createMantaAnimationCallback()

SwigInterface/wxManta.py

  Adding an example of how to create and use an animation callback.

  For some reason unregistering the test animation callback didn't
  work.


Modified: trunk/SwigInterface/pycallback.cc
==============================================================================
--- trunk/SwigInterface/pycallback.cc   (original)
+++ trunk/SwigInterface/pycallback.cc   Thu Jul 26 15:26:05 2007
@@ -167,6 +167,81 @@
   PyGILState_Release(gstate);
 }
 
+void
+callFromC_Animation(int a1, int a2, bool& b1,
+                    PyObjectContainer function, PyObjectContainer args)
+{
+  // Get ready to call python code
+  PyGILState_STATE gstate;
+  gstate = PyGILState_Ensure();
+
+  // Create a new Touple to accomodate the new args
+  int num_args = PyTuple_Size(args());
+  PyObject* new_args = PyTuple_New(num_args + 3);
+  // Pack the new args to the arguments
+  PyTuple_SET_ITEM(new_args, 0, PyInt_FromLong(a1));
+  PyTuple_SET_ITEM(new_args, 1, PyInt_FromLong(a2));
+  // Create a dictionary to pass in/out return parameters
+  PyObject* return_parameters = PyDict_New();
+  if (PyDict_SetItemString(return_parameters,
+                           "changed",
+                           b1 ? Py_True : Py_False)) {
+    // There was some kind of a problem
+    cerr << "Can't add item to dictionary.  Aborting callback.\n";
+    // Something went wrong
+    PyObject* error = PyErr_Occurred();
+    if (error) {
+      PyErr_Print();
+    }
+    Py_DECREF(return_parameters);
+    goto cleanup;
+  }
+  PyTuple_SET_ITEM(new_args, 2, return_parameters);
+  for(int i = 0; i < num_args; ++i) {
+    PyTuple_SET_ITEM(new_args, i+2, PyTuple_GET_ITEM(args(), i));
+  }
+  
+  makePyCall(function(), new_args);
+
+  // Now pull out the value
+  PyObject* changed = PyDict_GetItemString(return_parameters,
+                                           "changed");
+  if (changed != NULL) {
+    // Check to see if this is a bool object
+    bool new_b1;
+    if (PyBool_Check(changed)) {
+      // Get the value
+      if (changed == Py_True) new_b1 = true;
+      else if (changed == Py_False) new_b1 = false;
+      else {
+        // Shouldn't really need to check this, but I'm pedantic.
+        cerr << "changed is a Bool, but not Py_True or Py_False\n";
+        goto cleanup;
+      }
+    } else if (PyInt_Check(changed)) {
+      // Some goofy person could have specified an int.
+      
+      // Get the value.
+      long val = PyInt_AsLong(changed);
+      new_b1 = val != 0;
+    } else {
+      cerr << "changed value isn't a bool or int\n";
+      goto cleanup;
+    }
+    cerr << "b1 = "<<b1<<", new_b1 = "<<new_b1<<"\n";
+    if (new_b1 != b1) b1 = new_b1;
+  }
+ cleanup:
+#ifdef DECREMENT_PYTHON_REFERENCE_COUNTER
+  // You can't decrement this here, because it could lead to deleted
+  // SWIG proxy classes.
+  Py_DECREF(new_args);
+#endif
+
+  // Done with python code
+  PyGILState_Release(gstate);
+}
+
 CallbackBase_2Data<int, int>*
 createMantaOneShotCallback(PyObject* function, PyObject* args)
 {
@@ -175,6 +250,18 @@
   Py_XINCREF(args);
 #endif
   return Callback::create(&callFromC_OneShot,
+                          PyObjectContainer(function),
+                          PyObjectContainer(args));
+}
+
+CallbackBase_3Data<int, int, bool&>*
+createMantaAnimationCallback(PyObject* function, PyObject* args)
+{
+#ifndef INCREMENT_PYTHON_REFERENCE_COUNTER
+  Py_XINCREF(function);
+  Py_XINCREF(args);
+#endif
+  return Callback::create(&callFromC_Animation,
                           PyObjectContainer(function),
                           PyObjectContainer(args));
 }

Modified: trunk/SwigInterface/pycallback.h
==============================================================================
--- trunk/SwigInterface/pycallback.h    (original)
+++ trunk/SwigInterface/pycallback.h    Thu Jul 26 15:26:05 2007
@@ -78,9 +78,14 @@
 #ifndef SWIG
   void callFromC_OneShot(int a1, int a2,
                          PyObjectContainer function, PyObjectContainer args);
+  void callFromC_Animation(int a1, int a2, bool&,
+                           PyObjectContainer function, PyObjectContainer 
args);
 #endif
   Manta::CallbackBase_2Data<int, int>*
   createMantaOneShotCallback(PyObject* function, PyObject* args);
+
+  Manta::CallbackBase_3Data<int, int, bool&>*
+  createMantaAnimationCallback(PyObject* function, PyObject* args);
 
 } // end namespace Manta
 

Modified: trunk/SwigInterface/pycallback.i
==============================================================================
--- trunk/SwigInterface/pycallback.i    (original)
+++ trunk/SwigInterface/pycallback.i    Thu Jul 26 15:26:05 2007
@@ -37,6 +37,8 @@
 
 namespace Manta {
   %template(CallbackBase_2Data_Int_Int) CallbackBase_2Data<int, int>;
+  // For animation callbacks
+  %template(CallbackBase_3Data_Int_Int_BoolP) CallbackBase_3Data<int, int, 
bool&>;
 }
 
 

Modified: trunk/SwigInterface/wxManta.py
==============================================================================
--- trunk/SwigInterface/wxManta.py      (original)
+++ trunk/SwigInterface/wxManta.py      Thu Jul 26 15:26:05 2007
@@ -524,6 +524,7 @@
         self.left_mouse_is_down = False
         self.right_mouse_is_down = False
         self.timeView = None
+        self.animationCallbackHandle = None
         
 
     
###########################################################################
@@ -645,6 +646,14 @@
                 self.OnTKey(event)
             elif key == 'V':
                 self.OnAutoView(None)
+            elif key == 'A':
+                if (self.animationCallbackHandle == None):
+                    self.animationCallbackHandle = 
self.engine.registerSerialAnimationCallback(manta_new(createMantaAnimationCallback(self.animationCallback,
 ())))
+                    print self.animationCallbackHandle
+                else:
+                    print "Unregistering animation callback"
+                    
self.engine.unregisterCallback(self.animationCallbackHandle)
+                    self.animationCallbackHandle = None
             else:
                 print "Unknown key '%s'" % key
         else:
@@ -653,6 +662,13 @@
             
         event.Skip()
 
+    
###########################################################################
+    ## animationCallback
+    
###########################################################################
+    def animationCallback(self, proc, numProcs, return_parameters):
+        print "proc = %s, numProcs = %s, changed = %s" % (proc, numProcs, 
return_parameters["changed"])
+        return_parameters["changed"] = True
+        
     
###########################################################################
     ## OnShowDialog
     
###########################################################################




  • [MANTA] r1566 - trunk/SwigInterface, bigler, 07/26/2007

Archive powered by MHonArc 2.6.16.

Top of page