Branch soc-2009-kazanbas
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 18 Jun 2009 19:59:20 +0000 (19:59 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 18 Jun 2009 19:59:20 +0000 (19:59 +0000)
Merge with trunk revision 20991, and update scripts for the changes too.
Reload Scripts and Export OBJ still work for me.

1  2 
release/io/export_obj.py
release/ui/space_script.py

index df10c3e908990e6470cd6b67774d16923df1441e,0000000000000000000000000000000000000000..4354f9f9bb94b984f4d3d711ab5047f2dfda1369
mode 100644,000000..100644
--- /dev/null
@@@ -1,69 -1,0 +1,71 @@@
-       mesh = bpy.data.add_mesh("tmpmesh")
-       # copy data with modifiers applied
-       mesh.copy_applied(scene, ob)
 +import bpy
 +
 +def write_obj(filepath, scene, ob):
 +      out = open(filepath, 'w')
 +
 +      # create a temporary mesh
-       # TODO: delete mesh here
++      mesh = ob.create_render_mesh(scene)
 +
 +      # for vert in mesh.verts:
 +      # ^ iterating that way doesn't work atm for some reason
 +
 +      for i in range(len(mesh.verts)):
 +              vert = mesh.verts[i]
 +              out.write('v {0} {1} {2}\n'.format(vert.co[0], vert.co[1], vert.co[2]))
 +      
 +      for i in range(len(mesh.faces)):
 +              face = mesh.faces[i]
 +              out.write('f')
 +
 +              # but this works
 +              for index in face.verts:
 +                      out.write(' {0}'.format(index + 1))
 +              out.write('\n')
 +
-               bpy.props["StringProperty"](attr="filename", name="filename")
++      # delete mesh gain
++      bpy.data.remove_mesh(mesh)
 +
 +      out.close()
 +      
 +class SCRIPT_OT_export_obj(bpy.types.Operator):
 +      '''A very basic OBJ exporter, writes only active object's mesh.'''
 +
 +      __label__ = 'Export OBJ'
 +      
 +      # List of operator properties, the attributes will be assigned
 +      # to the class instance from the operator settings before calling.
 +      __props__ = [
-       def exec(self, context):
++              bpy.props.StringProperty(attr="filename", name="filename")
 +              ]
 +
 +      def debug(self, message):
 +              print("{0}: {1}".format(self.__class__.__name__, message))
 +
-               context.add_fileselect(self.__operator__)
++      def execute(self, context):
 +              self.debug("exec")
 +              self.debug("filename = " + self.filename)
 +
 +              act = context.active_object
 +
 +              if act.type == 'MESH':
 +                      write_obj(self.filename, context.scene, act)
 +              else:
 +                      self.debug("Active object is not a MESH.")
 +
 +              # XXX errors are silenced for some reason
 +#             raise Exception("oops!")
 +
 +              return ('FINISHED',)
 +      
 +      def invoke(self, context, event):
 +              self.debug("invoke")
++              wm = context.manager
++              wm.add_fileselect(self.__operator__)
 +              return ('RUNNING_MODAL',)
 +      
 +      def poll(self, context): # poll isnt working yet
 +              self.debug("poll")
 +              return True
++
++bpy.ops.add(SCRIPT_OT_export_obj)
++
index d2c73adb00fc817c2ec236f70741d65c16d18060,0000000000000000000000000000000000000000..d35f2d389c8c5254353fc2f314f176a685275dea
mode 100644,000000..100644
--- /dev/null
@@@ -1,131 -1,0 +1,132 @@@
-     def exec(self, context):
-         print("SCRIPT_OT_reload_scripts: exec")
 +import sys
 +import os
 +import imp
 +# import glob
 +import bpy
 +
 +operators = []
 +
 +def register_op(opclass):
 +    if (hasattr(bpy.ops, opclass.__name__)):
 +        bpy.ops.remove(getattr(bpy.ops, opclass.__name__))
 +
 +    bpy.ops.add(opclass)
 +
 +    global operators
 +    if opclass.__name__ not in operators:
 +        operators.append(opclass.__name__)
 +
 +
 +# hint for myself: for interface api documentation, see source/blender/editors/interface/interface_api.c
 +# another hint: reloading ui scripts in scripts window is Shift + P
 +
 +class SCRIPT_HT_header(bpy.types.Header):
 +      __space_type__ = "SCRIPTS_WINDOW"
 +      __idname__ = "SCRIPT_HT_header"
 +
 +      def draw(self, context):
 +              st = context.space_data
 +              layout = self.layout
 +
 +              layout.template_header(context)
 +
 +              if context.area.show_menus:
 +                      row = layout.row(align=True)
 +                      row.itemM(context, "SCRIPT_MT_scripts")
 +
 +class SCRIPT_MT_scripts(bpy.types.Menu):
 +    __space_type__ = "SCRIPTS_WINDOW"
 +    __label__ = "Scripts"
 +
 +    def draw(self, context):
 +        layout = self.layout
 +        layout.column()
 +        layout.itemM(context, "SCRIPT_MT_export")
 +        layout.itemO("SCRIPT_OT_reload_scripts")
 +
 +class SCRIPT_MT_export(bpy.types.Menu):
 +    __space_type__ = "SCRIPTS_WINDOW"
 +    __label__ = "Export"
 +
 +    def draw(self, context):
 +        global operators
 +
 +        print("drawing {0} operators: {1}".format(len(operators), operators))
 +
 +        layout = self.layout
 +        layout.column()
 +        for opname in operators:
 +            layout.itemO(opname)
 +
 +class SCRIPT_OT_reload_scripts(bpy.types.Operator):
 +    __label__ = 'Reload Scripts'
 +
-         return self.exec(context)
++    def execute(self, context):
++        print("SCRIPT_OT_reload_scripts: execute")
 +
 +        # add ../io to sys.path
 +
 +        # this module's absolute path
 +        abspath = os.path.abspath(sys.modules[__name__].__file__)
 +        print("Current abspath: {0}".format(abspath))
 +
 +        # ../io
 +        io = os.path.normpath(os.path.dirname(abspath) + "/../io")
 +        print("abspath = " + io)
 +
 +        if io not in sys.path:
 +            sys.path.append(io)
 +
 +        # for each .py file in release/io,
 +        # import/reload module, in the module:
 +        # find subclasses of bpy.types.Operator,
 +        # for each subclass create menus under "Export"
 +        # with (row.)itemO
 +
 +        global operators
 +        operators = []
 +
 +        # glob unavailable :(
 +#         for path in glob.glob("../io/*.py"):
 +        for path in os.listdir(io):
 +            modname, ext = os.path.splitext(os.path.basename(path))
 +
 +            if ext != ".py":
 +                continue
 +
 +            print("Found module {0}.".format(modname))
 +
 +            if modname in sys.modules:
 +                mod = imp.reload(sys.modules[modname])
 +                print("Reloaded it.")
 +            else:
 +                mod = __import__(modname)
 +                print("Imported it.")
 +
 +            for attr in dir(mod):
 +                cls = getattr(mod, attr)
 +                
 +                # XXX is there a better way to check that cls is a class?
 +                if type(cls) == bpy.types.Operator.__class__ and issubclass(cls, bpy.types.Operator):
 +                    print("Found class {0}.".format(cls.__name__))
 +                    register_op(cls)
 +                    print("Registered it.")
 +
 +        return ('FINISHED',)
 +
 +    def invoke(self, context, event):
 +        print("SCRIPT_OT_reload_scripts: invoke")
++        return self.execute(context)
 +
 +    def poll(self, context):
 +        pass
 +
 +bpy.types.register(SCRIPT_HT_header)
 +bpy.types.register(SCRIPT_MT_scripts)
 +bpy.types.register(SCRIPT_MT_export)
 +
 +if (hasattr(bpy.ops, "SCRIPT_OT_reload_scripts")):
 +    bpy.ops.remove(bpy.ops.SCRIPT_OT_reload_scripts)
 +
 +bpy.ops.add(SCRIPT_OT_reload_scripts)
++