daily pastebin goal
56%
SHARE
TWEET

blender25 addon ctypes-gtk test1

a guest Jan 6th, 2011 247 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. ctypes-GTK in Blender Test1 - Jan6th 2010
  3.         tested with blender2.55 on ubuntu 8.10 and 10.10
  4.         (should work on any platform with gtk installed)
  5.  
  6. Installing:
  7.         1. download:
  8.                 http://rpythonic.googlecode.com/files/ctypes-gtk-test1.tar.bz2
  9.         2. copy _ctypes_gtk.py to blender/2.5x/scripts/addons/.
  10.         3. copy this file (addon_gtk_test.py) to blender/2.5x/scripts/addons/.
  11.         4. go to "User-Prefs->Addons->System" enable Gtk-test1
  12.         5. click "GTK" in upper right toolbar
  13.  
  14. '''
  15.  
  16. VERSION = 'gtk in blender test1'
  17.  
  18. bl_addon_info = {
  19.     "name": "GTK test1",
  20.     "author": "HartsAntler",
  21.     "version": (0,0,1),
  22.     "blender": (2, 5, 6),
  23.     "location": "Properties",
  24.     "description": "GTK test",
  25.     "warning": "",
  26.     "category": "System"}
  27.  
  28. import os, sys, time, hashlib, math, threading
  29.  
  30. try:
  31.         import bpy, mathutils
  32.         from bpy.props import *
  33. except ImportError:
  34.         sys.exit("This script is an addon for blender, you must install it from blender.")
  35.  
  36. from _ctypes_gtk import *
  37.  
  38. class MyGtkWidget(object):
  39.         def start(self):
  40.                 print('running gtk main')
  41.                 gtk_main()
  42.                 print('gtk exit')
  43.         def start_thread( self ):
  44.                 t = threading.Thread(target=self.start)
  45.                 print('starting thread')
  46.                 t.start()
  47.                 #print('joining thread')                # dont join the thread so that blender can continue running
  48.                 #t.join()
  49.                 #print('thread exit')
  50.  
  51.         def __init__(self):
  52.                 self.bobject = None
  53.  
  54.                 gtk_init(
  55.                         ctypes.byref(  ctypes.c_int(0) ),
  56.                         ctypes.byref(  ctypes.pointer( ctypes.c_char_p('')))
  57.                 )
  58.  
  59.                 self.win_widget = winwidget = gtk_window_new(GTK_WINDOW_TOPLEVEL)
  60.                 self.win = win = ctypes.cast( winwidget, ctypes.POINTER(GtkWindow) )
  61.                 gtk_window_set_title(win, "ctypes pygtk")
  62.                 gtk_window_set_default_size(win, 320, 60)
  63.                 #gdk_window_set_keep_above(     # not working TODO
  64.                 #       #ctypes.cast(win, ctypes.POINTER(GdkDrawable)),
  65.                 #       win.contents.frame,
  66.                 #       ctypes.c_int(1)
  67.                 #)
  68.  
  69.                 gtk_signal_connect_full(
  70.                         ctypes.cast(win,ctypes.POINTER(GtkObject)), "destroy", gtk_main_quit,
  71.                         None, None, None, ctypes.c_int(0), ctypes.c_int(0)
  72.                 )
  73.  
  74.  
  75.                 self.frame = gtk_frame_new('blender gtk test1')
  76.                 gtk_container_add(
  77.                         ctypes.cast(win, ctypes.POINTER(GtkContainer)),
  78.                         ctypes.cast(self.frame, ctypes.POINTER(GtkWidget))
  79.                 )
  80.  
  81.                 self.box = gtk_hbox_new( 0, 2 )         #ctypes.c_int(0), ctypes.c_int(10) )
  82.                 gtk_container_add(
  83.                         ctypes.cast(self.frame, ctypes.POINTER(GtkContainer)),
  84.                         ctypes.cast(self.box, ctypes.POINTER(GtkWidget))
  85.                 )
  86.  
  87.  
  88.                 button = gtk_button_new_with_label("update")
  89.                 self.myfunc = func = ctypes.CFUNCTYPE( c_void_p, c_void_p )( self.callback_update )
  90.                 gtk_signal_connect_full(
  91.                         ctypes.cast(button,ctypes.POINTER(GtkObject)), "clicked", func,
  92.                         None, None, None, ctypes.c_int(0), ctypes.c_int(0)
  93.                 )
  94.  
  95.                 #gtk_container_add(
  96.                 #       ctypes.cast(self.box, ctypes.POINTER(GtkContainer)),
  97.                 #       ctypes.cast(button, ctypes.POINTER(GtkWidget))
  98.                 #)
  99.                 gtk_box_pack_start(
  100.                         ctypes.cast( self.box, ctypes.POINTER(GtkBox) ),
  101.                         ctypes.cast( button, ctypes.POINTER(GtkWidget) ),
  102.                         0, 0, 2
  103.                 )
  104.  
  105.                 self.label= gtk_label_new('selected')
  106.                 gtk_box_pack_start(
  107.                         ctypes.cast( self.box, ctypes.POINTER(GtkBox) ),
  108.                         ctypes.cast( self.label, ctypes.POINTER(GtkWidget) ),
  109.                         0, 0, 2
  110.                 )
  111.  
  112.  
  113.  
  114.                 adj = gtk_adjustment_new( .0, -10, 10, 0.01, 1, 1 )
  115.                 self.adjust = ctypes.cast(adj, ctypes.POINTER(GtkAdjustment))
  116.                 self.myfunc2 = func = ctypes.CFUNCTYPE( c_void_p, c_void_p )( self.callback_adj )
  117.                 gtk_signal_connect_full(
  118.                         ctypes.cast(adj,ctypes.POINTER(GtkObject)), "value_changed", func,
  119.                         None, None, None, ctypes.c_int(0), ctypes.c_int(0)
  120.                 )
  121.  
  122.                 self.scale = gtk_hscale_new( self.adjust )
  123.                 gtk_box_pack_start(
  124.                         ctypes.cast( self.box, ctypes.POINTER(GtkBox) ),
  125.                         ctypes.cast( self.scale, ctypes.POINTER(GtkWidget) ),
  126.                         1, 1, 4
  127.                 )
  128.  
  129.                 gtk_widget_show_all( winwidget )
  130.  
  131.  
  132.         def callback_update(self,*args):
  133.                 for ob in bpy.data.objects:
  134.                         if ob.select:
  135.                                 self.bobject = ob
  136.                                 break
  137.                 if self.bobject:
  138.                         ob = self.bobject
  139.                         self.adjust.contents.value = ob.scale.x
  140.                         gtk_label_set_text(
  141.                                 ctypes.cast(self.label,ctypes.POINTER(GtkLabel)),
  142.                                 ob.name
  143.                         )
  144.  
  145.         def callback_adj(self,*args):
  146.                 if self.bobject:
  147.                         ob = self.bobject
  148.                         v = self.adjust.contents.value
  149.                         ob.scale.x = v
  150.  
  151.  
  152. _GUI_ = None
  153.  
  154. class gts_update_op(bpy.types.Operator):                
  155.         '''operator: open GTK test'''  
  156.         bl_idname = "gtk.start"  
  157.         bl_label = "opens the GTK test"                    
  158.         bl_options = {'REGISTER'}                              # Options for this panel type
  159.         @classmethod
  160.         def poll(cls, context): return True
  161.         def invoke(self, context, event):
  162.                 global _GUI_
  163.                 if not _GUI_:
  164.                         _GUI_ = MyGtkWidget()
  165.                         _GUI_.start_thread()
  166.  
  167.                 return {'FINISHED'}
  168.  
  169. class INFO_HT_myheader(bpy.types.Header):
  170.         bl_space_type = 'INFO'
  171.         def draw(self, context):
  172.                 layout = self.layout
  173.                 op = layout.operator('gtk.start', text='GTK')
  174.  
  175.  
  176. def register(): print( VERSION )
  177. def unregister(): pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top