Browse Source

added blender texture support

Johann Woelper 9 years ago
parent
commit
04fc94ddb8
3 changed files with 209 additions and 2 deletions
  1. 184 0
      m2b/b2m.py
  2. 20 0
      m2b/generic2m.py
  3. 5 2
      m2b/m2b.py

+ 184 - 0
m2b/b2m.py

@@ -0,0 +1,184 @@
+
+import bpy
+import socket
+import os
+import sys
+import json
+from bpy.app.handlers import persistent
+
+
+print('\n\n\n\n\n======================== INIT BLENDER SESSION ============================')
+
+home = os.path.expanduser('~') 
+includepath = os.path.join(home, 'Documents', 'lab', 'm2b')
+
+
+# /// SETTINGS
+OBJ_FILE = ''
+TEXTURE = '/Users/jwoelper/diggr.png'
+
+# disable splash
+bpy.context.user_preferences.view.show_splash = False
+
+
+def get_sceneinfo():
+    """Check if last argument is a file
+    """
+    arg = sys.argv[-1]
+    print('> CMD LINE ARG:', arg)
+    if 'blend' in arg:
+        basepath = os.path.dirname(arg)
+        uuid = os.path.basename(arg)
+        uuid = os.path.splitext(uuid)[0]
+        jsonfile = os.path.join(basepath, uuid + '.json')
+        with open(jsonfile) as conffile:
+            conf_data = json.load(conffile)
+        print(conf_data)
+        return conf_data
+    
+    elif 'json' in arg:
+        with open(arg) as conffile:
+            conf_data = json.load(conffile)
+        return conf_data
+
+
+
+
+def import_obj(fp):
+    old_state = list(bpy.context.scene.objects)
+    bpy.ops.import_scene.obj(filepath=fp)
+    new_state = list(bpy.context.scene.objects)
+    return set(new_state) - set(old_state)
+
+
+def setup_scene():
+    print('> SETTING UP SCENE')
+    if os.path.isfile(BLEND):
+        print('> loading file')
+        #Dbpy.ops.wm.open_mainfile(filepath=BLEND)
+        
+    else:
+        print('> texture:', TEXTURE)
+        for object in bpy.data.objects:
+            if object.name == 'Cube':
+                object.select = True
+                # remove it
+                bpy.ops.object.delete()
+    
+        new_items = import_obj(OBJ_FILE)
+        for object in bpy.context.scene.objects:
+            object.select = False
+
+        scn = bpy.context.scene
+        scn.render.engine = 'CYCLES'
+        
+        mat = bpy.data.materials.new('MayaTexture')
+        mat.use_nodes = True
+        texnode = mat.node_tree.nodes.new(type="ShaderNodeTexImage")
+        mat.node_tree.links.new(texnode.outputs['Color'], mat.node_tree.nodes['Diffuse BSDF'].inputs['Color'])
+        texnode.image = bpy.data.images.load(TEXTURE)
+
+        for item in new_items:
+            if item.type == 'MESH':
+                ob = item
+                mesh = ob.data
+                mesh.materials.append(mat)
+                item.select = True
+                bpy.context.scene.objects.active = item
+
+    
+    preview_texture(TEXTURE)
+
+    print('SAVING AS', BLEND)
+    bpy.ops.wm.save_as_mainfile(filepath=BLEND, check_existing=False)
+
+
+def update_maya():
+    print('MSG > MAYA', PORT)
+    host = '127.0.0.1'
+    port = PORT
+    message = 'import sys;sys.path.append("' + includepath + '");import m2b;m2b.update("' + UUID + '")'
+
+    maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    maya.connect((host, port))
+    msg = bytes(message, 'UTF-8')
+    try:
+        print('sending')
+        maya.send(msg)
+    except:
+        print('failed')
+        print(msg)
+    finally:
+        print('closed')
+        maya.close()
+
+def deselect():
+    for object in bpy.context.scene.objects:
+        object.select = False
+
+def preview_texture(image):
+    print('> PREVIEW TEXTURE GENERATION', TEXTURE)
+    img = bpy.data.images.load(image)
+
+
+
+
+    for area in bpy.data.screens['Default'].areas:
+        if area.type == 'VIEW_3D':
+            for space in area.spaces:
+                if space.type == 'VIEW_3D':
+                    space.viewport_shade = 'TEXTURED'
+                    space.show_textured_solid = True
+    deselect()
+    mat = bpy.data.materials.new('TexMat')
+
+
+def export():
+    selection = bpy.context.selected_objects
+
+    deselect()
+
+    for object in bpy.context.scene.objects:
+        if not object.hide_render:
+            object.select = True
+    bpy.ops.export_scene.obj(filepath=OBJ_FILE,
+                             use_materials=False,
+                             use_blen_objects=False,
+                             use_selection=True)
+
+    # restore selection
+    for object in bpy.context.scene.objects:
+        object.select = False
+        if object in selection:
+            object.select = True
+
+    update_maya()
+
+
+@persistent
+def save_handler(dummy):
+    export()
+
+
+class BlenderBridge(object):
+    def __init__(self):
+        self.handler = save_handler
+
+
+info = get_sceneinfo()
+if info is not None:
+    OBJ_FILE = info['obj']
+    TEXTURE = info['tex']
+    BLEND = info['blend']
+    UUID = info['uuid']
+    PORT = info['port']
+
+
+setup_scene()
+
+
+
+
+
+if len(bpy.app.handlers.save_post) < 1:
+    bpy.app.handlers.save_post.append(save_handler)

+ 20 - 0
m2b/generic2m.py

@@ -0,0 +1,20 @@
+
+
+import socket
+import os
+import sys
+
+home = os.path.expanduser('~') 
+importpath = os.path.join(home, 'Documents', 'lab', 'm2b')
+#sys.path.append(importpath)
+
+host = '127.0.0.1'
+port = 6006
+
+UUID = 'F2FCB409-9E40-CC3B-ED2B-A8BD80D9D1B3'
+message = 'import sys;sys.path.append("/Users/jwoelper/Documents/lab/m2b");import m2b;m2b.update("' + UUID + '")'
+
+maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+maya.connect( (host,port) )
+print maya.send(message)
+maya.close()

+ 5 - 2
m2b/m2b.py

@@ -117,7 +117,7 @@ class AtOrigin(object):
 
 class KeepSelection(object):
     def __enter__(self):
-        print '> save sel'
+        print '> store sel'
         self.selection = pm.selected()
         
     def __exit__(self, type, value, traceback):
@@ -144,7 +144,7 @@ class BridgedNode(object):
         #print [ (key, self.__dict__[key]) for key in self.__dict__.keys()]
     
         if not any([self.__dict__[key] is None for key in self.__dict__.keys()]):
-            print '> all values set'
+            print '> valid'
             return True
         
     def has_history(self):
@@ -228,6 +228,9 @@ def update(uuid):
     bn.ingest()
     if bn.is_valid():
         with KeepSelection():
+            print 'GEO', bn.geofile
             imported_node = load_geofile(bn.geofile)
             replace_mesh(bn.node, imported_node)
+    else:
+        print '> UUID invalid.'