SHARE
TWEET

SendToMaya

a guest May 28th, 2017 9 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import bpy
  2. import sys
  3. import tempfile
  4. from telnetlib import Telnet
  5. from time import sleep
  6. import os
  7. bl_info = {
  8.     "name": "Send to Maya",
  9.     "author": "Sven Akelian - BlenderLounge",
  10.     "blender": (2, 7, 0),
  11.     "location": "VIEW3D -> UI panel",
  12.     "description": "Send selected Objects to Maya",
  13.     "warning": "Still in dev.",
  14.     "category": "Import-Export"
  15. }
  16.  
  17. # In maya you first need to open the port using this command:
  18. #
  19. host = 'localhost'
  20. port = 7002
  21.  
  22.  
  23. def export_defaults(file):
  24.     bpy.ops.wm.alembic_export(
  25.         filepath=file,
  26.         # check_existing=0,
  27.         selected=1,
  28.         normals=1,
  29.         uvs=1,
  30.         # vcolors=1,
  31.         global_scale=1,
  32.         )
  33.  
  34.  
  35. class OBJECT_OT_sendMaya(bpy.types.Operator):
  36.     bl_idname = 'object.send_to_maya'
  37.     bl_label = 'Send'
  38.     bl_options = {'REGISTER'}
  39.  
  40.     def invoke(self, context, event):
  41.         context.window_manager.invoke_props_dialog(self)
  42.         return {'RUNNING_MODAL'}
  43.  
  44.     def execute(self, context):
  45.         # Set temporary file and export
  46.         file = tempfile.NamedTemporaryFile(suffix=".abc",delete=False)
  47.         file.close()
  48.         export_defaults(file.name)
  49.  
  50.         # TODO: Here find an appropriate/pythonic way to check the state of the ABC File
  51.         checkfileStatus(file.name)
  52.  
  53.         try:
  54.  
  55.             c = Telnet(host, port, timeout=3)
  56.             message = "import pymel.core as pm;pm.importFile('" + file.name + "')"
  57.             c.write(message.encode("utf-8"))
  58.  
  59.  
  60.         except Exception:
  61.             e = sys.exc_info()[1]
  62.             err = str(e)
  63.             msg = "Failed to communicate with Maya (%(host)s:%(port)s)):\n%(err)s" % locals()
  64.             raise
  65.  
  66.         else:
  67.             sleep(.1)
  68.  
  69.         finally:
  70.             try:
  71.                 if c is not None:
  72.                     c.close()
  73.             except:
  74.                 print("ERROR")
  75.  
  76.         return {'FINISHED'}
  77.  
  78.  
  79. class VIEW3D_PT_sendMaya(bpy.types.Panel):
  80.     bl_idname = "OBJECT_PT_send_to_maya"
  81.     bl_label = "Send to Maya"
  82.     bl_space_type = "VIEW_3D"
  83.     bl_region_type = "UI"
  84.  
  85.     def draw(self, context):
  86.         self.layout.row().operator('object.send_to_maya')
  87.  
  88. def checkfileStatus(file):
  89.     """
  90.    Blocking function.
  91.    Hacky way to get the filestatus from Blender's abc save.
  92.    Checking the file size over time, break when there is no more difference...
  93.    """
  94.     while True:
  95.         fileInit = os.stat(file)
  96.         sleep(1)
  97.         fileNew = os.stat(file)
  98.  
  99.         difference = fileNew.st_size - fileInit.st_size
  100.  
  101.         if difference == 0:
  102.             break
  103.         else:
  104.             sleep(5)
  105.  
  106. def register():
  107.     bpy.utils.register_class(OBJECT_OT_sendMaya)
  108.     bpy.utils.register_class(VIEW3D_PT_sendMaya)
  109.  
  110.  
  111. def unregister():
  112.     bpy.utils.unregister_class(OBJECT_OT_sendMaya)
  113.     bpy.utils.unregister_class(VIEW3D_PT_sendMaya)
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