Esempio n. 1
0
def createMaterialDictAndMeshList(scene,object,applyTransform=True):
    materialDict = {}
    mesh_objects = []

    for ob in [object]:

      free, derived = create_derived_objects(scene, ob)

      if derived is None:
        continue
    
      for ob_derived, mat in derived:
        if ob.type not in ('MESH', 'CURVE', 'SURFACE', 'FONT', 'META'):
          continue

        data = ob_derived.create_mesh(scene, True, 'PREVIEW')
        if data:
          if applyTransform:
            data.transform(mat)

          mesh_objects.append((ob_derived, data))
          mat_ls = data.materials
          mat_ls_len = len(mat_ls)

          # get material/image tuples.
          if len(data.uv_textures):
            if not mat_ls:
              mat = mat_name = None

            for f, uf in zip(data.faces, data.uv_textures.active.data):
              if mat_ls:
                mat_index = f.material_index
                if mat_index >= mat_ls_len:
                  mat_index = f.mat = 0
                mat = mat_ls[mat_index]
                if mat:	mat_name = mat.name
                else:	mat_name = None
              # else there already set to none

              img = uf.image
              if img:	img_name = img.name
              else:	img_name = None

              materialDict.setdefault((mat_name, img_name), (mat, img) )

          else:
            for mat in mat_ls:
              if mat: # material may be None so check its not.
                materialDict.setdefault((mat.name, None), (mat, None) )

            for f in data.faces:
              if f.material_index >= mat_ls_len:
                f.material_index = 0

        if free:
            free_derived_objects(ob)
    return materialDict, mesh_objects
Esempio n. 2
0
    def export(self, scene, world, alltextures,
                use_apply_modifiers=False,
                use_selection=True,
                EXPORT_TRI=False,
                ):

        # tag un-exported IDs
        bpy.data.meshes.tag(False)
        bpy.data.materials.tag(False)
        bpy.data.images.tag(False)

        print("Info: starting X3D export to %r..." % self.filepath)
        self.writeHeader()
        # self.writeScript()
        self.writeNavigationInfo(scene)
        self.writeBackground(world, alltextures)
        self.writeFog(world)
        self.proto = 0

        if use_selection:
            objects = (o for o in scene.objects if o.is_visible(scene) and o.select)
        else:
            objects = (o for o in scene.objects if o.is_visible(scene))

        for ob_main in objects:

            free, derived = create_derived_objects(scene, ob_main)

            if derived is None:
                continue

            for ob, ob_mat in derived:
                objType = ob.type
                objName = ob.name
                ob_mat = self.global_matrix * ob_mat

                if objType == 'CAMERA':
                    self.writeViewpoint(ob, ob_mat, scene)
                elif objType in ('MESH', 'CURVE', 'SURF', 'FONT'):
                    if (objType != 'MESH') or (use_apply_modifiers and ob.is_modified(scene, 'PREVIEW')):
                        try:
                            me = ob.to_mesh(scene, use_apply_modifiers, 'PREVIEW')
                        except:
                            me = None
                    else:
                        me = ob.data

                    if me is not None:
                        self.writeIndexedFaceSet(ob, me, ob_mat, world, EXPORT_TRI=EXPORT_TRI)

                        # free mesh created with create_mesh()
                        if me != ob.data:
                            bpy.data.meshes.remove(me)

                elif objType == 'LAMP':
                    data = ob.data
                    datatype = data.type
                    if datatype == 'POINT':
                        self.writePointLight(ob, ob_mat, data, world)
                    elif datatype == 'SPOT':
                        self.writeSpotLight(ob, ob_mat, data, world)
                    elif datatype == 'SUN':
                        self.writeDirectionalLight(ob, ob_mat, data, world)
                    else:
                        self.writeDirectionalLight(ob, ob_mat, data, world)
                else:
                    #print "Info: Ignoring [%s], object type [%s] not handle yet" % (object.name,object.getType)
                    pass

            if free:
                free_derived_objects(ob_main)

        self.file.write("\n</Scene>\n</X3D>")

        # if use_apply_modifiers:
        # 	if containerMesh:
        # 		containerMesh.vertices = None

        self.cleanup()
Esempio n. 3
0
def save(operator, context, filepath="",
          use_selection=True,
          ):

    import bpy
    import time
    from io_utils import create_derived_objects, free_derived_objects

    '''Save the Blender scene to a 3ds file.'''

    # Time the export
    time1 = time.clock()
#	Blender.Window.WaitCursor(1)

    sce = context.scene

    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')

    # Initialize the main chunk (primary):
    primary = _3ds_chunk(PRIMARY)
    # Add version chunk:
    version_chunk = _3ds_chunk(VERSION)
    version_chunk.add_variable("version", _3ds_int(3))
    primary.add_subchunk(version_chunk)

    # init main object info chunk:
    object_info = _3ds_chunk(OBJECTINFO)

    ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX
    # init main key frame data chunk:
    kfdata = make_kfdata()
    '''

    # Get all the supported objects selected in this scene:
    # ob_sel= list(sce.objects.context)
    # mesh_objects = [ (ob, me) for ob in ob_sel   for me in (BPyMesh.getMeshFromObject(ob, None, True, False, sce),) if me ]
    # empty_objects = [ ob for ob in ob_sel if ob.type == 'Empty' ]

    # Make a list of all materials used in the selected meshes (use a dictionary,
    # each material is added once):
    materialDict = {}
    mesh_objects = []
    scene = context.scene


    if use_selection:
        objects = (ob for ob in scene.objects if ob.is_visible(scene) and ob.select)
    else:
        objects = (ob for ob in scene.objects if ob.is_visible(scene))

    for ob in objects:
        # get derived objects
        free, derived = create_derived_objects(scene, ob)

        if derived is None:
            continue

        for ob_derived, mat in derived:
# 		for ob_derived, mat in getDerivedObjects(ob, False):

            if ob.type not in ('MESH', 'CURVE', 'SURFACE', 'FONT', 'META'):
                continue

            try:
                data = ob_derived.create_mesh(scene, True, 'PREVIEW')
            except:
                data = None

            if data:
                data.transform(mat)
# 				data.transform(mat, recalc_normals=False)
                mesh_objects.append((ob_derived, data))
                mat_ls = data.materials
                mat_ls_len = len(mat_ls)

                # get material/image tuples.
                if len(data.uv_textures):
# 				if data.faceUV:
                    if not mat_ls:
                        mat = mat_name = None

                    for f, uf in zip(data.faces, data.uv_textures.active.data):
                        if mat_ls:
                            mat_index = f.material_index
# 							mat_index = f.mat
                            if mat_index >= mat_ls_len:
                                mat_index = f.mat = 0
                            mat = mat_ls[mat_index]
                            if mat:	mat_name = mat.name
                            else:	mat_name = None
                        # else there already set to none

                        img = uf.image
# 						img = f.image
                        if img:	img_name = img.name
                        else:	img_name = None

                        materialDict.setdefault((mat_name, img_name), (mat, img) )


                else:
                    for mat in mat_ls:
                        if mat: # material may be None so check its not.
                            materialDict.setdefault((mat.name, None), (mat, None) )

                    # Why 0 Why!
                    for f in data.faces:
                        if f.material_index >= mat_ls_len:
# 						if f.mat >= mat_ls_len:
                            f.material_index = 0
                            # f.mat = 0

        if free:
            free_derived_objects(ob)


    # Make material chunks for all materials used in the meshes:
    for mat_and_image in materialDict.values():
        object_info.add_subchunk(make_material_chunk(mat_and_image[0], mat_and_image[1]))

    # Give all objects a unique ID and build a dictionary from object name to object id:
    """
    name_to_id = {}
    for ob, data in mesh_objects:
        name_to_id[ob.name]= len(name_to_id)
    #for ob in empty_objects:
    #	name_to_id[ob.name]= len(name_to_id)
    """

    # Create object chunks for all meshes:
    i = 0
    for ob, blender_mesh in mesh_objects:
        # create a new object chunk
        object_chunk = _3ds_chunk(OBJECT)

        # set the object name
        object_chunk.add_variable("name", _3ds_string(sane_name(ob.name)))

        # make a mesh chunk out of the mesh:
        object_chunk.add_subchunk(make_mesh_chunk(blender_mesh, materialDict))
        object_info.add_subchunk(object_chunk)

        ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX
        # make a kf object node for the object:
        kfdata.add_subchunk(make_kf_obj_node(ob, name_to_id))
        '''
        if not blender_mesh.users:
            bpy.data.meshes.remove(blender_mesh)
# 		blender_mesh.vertices = None

        i+=i

    # Create chunks for all empties:
    ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX
    for ob in empty_objects:
        # Empties only require a kf object node:
        kfdata.add_subchunk(make_kf_obj_node(ob, name_to_id))
        pass
    '''

    # Add main object info chunk to primary chunk:
    primary.add_subchunk(object_info)

    ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX
    # Add main keyframe data chunk to primary chunk:
    primary.add_subchunk(kfdata)
    '''

    # At this point, the chunk hierarchy is completely built.

    # Check the size:
    primary.get_size()
    # Open the file for writing:
    file = open(filepath, 'wb')

    # Recursively write the chunks to file:
    primary.write(file)

    # Close the file:
    file.close()

    # Clear name mapping vars, could make locals too
    name_unique[:] = []
    name_mapping.clear()

    # Debugging only: report the exporting time:
# 	Blender.Window.WaitCursor(0)
    print("3ds export time: %.2f" % (time.clock() - time1))

    # Debugging only: dump the chunk hierarchy:
    #primary.dump()

    return {'FINISHED'}
Esempio n. 4
0
    def export(
        self,
        scene,
        world,
        alltextures,
        use_apply_modifiers=False,
        use_selection=True,
        EXPORT_TRI=False,
    ):

        # tag un-exported IDs
        bpy.data.meshes.tag(False)
        bpy.data.materials.tag(False)
        bpy.data.images.tag(False)

        print("Info: starting X3D export to %r..." % self.filepath)
        self.writeHeader()
        # self.writeScript()
        self.writeNavigationInfo(scene)
        self.writeBackground(world, alltextures)
        self.writeFog(world)
        self.proto = 0

        if use_selection:
            objects = (o for o in scene.objects
                       if o.is_visible(scene) and o.select)
        else:
            objects = (o for o in scene.objects if o.is_visible(scene))

        for ob_main in objects:

            free, derived = create_derived_objects(scene, ob_main)

            if derived is None:
                continue

            for ob, ob_mat in derived:
                objType = ob.type
                objName = ob.name
                ob_mat = self.global_matrix * ob_mat

                if objType == 'CAMERA':
                    self.writeViewpoint(ob, ob_mat, scene)
                elif objType in ('MESH', 'CURVE', 'SURF', 'FONT'):
                    if use_apply_modifiers or objType != 'MESH':
                        try:
                            me = ob.create_mesh(scene, use_apply_modifiers,
                                                'PREVIEW')
                        except:
                            me = None
                    else:
                        me = ob.data

                    if me is not None:
                        self.writeIndexedFaceSet(ob,
                                                 me,
                                                 ob_mat,
                                                 world,
                                                 EXPORT_TRI=EXPORT_TRI)

                        # free mesh created with create_mesh()
                        if me != ob.data:
                            bpy.data.meshes.remove(me)

                elif objType == 'LAMP':
                    data = ob.data
                    datatype = data.type
                    if datatype == 'POINT':
                        self.writePointLight(ob, ob_mat, data, world)
                    elif datatype == 'SPOT':
                        self.writeSpotLight(ob, ob_mat, data, world)
                    elif datatype == 'SUN':
                        self.writeDirectionalLight(ob, ob_mat, data, world)
                    else:
                        self.writeDirectionalLight(ob, ob_mat, data, world)
                else:
                    #print "Info: Ignoring [%s], object type [%s] not handle yet" % (object.name,object.getType)
                    pass

            if free:
                free_derived_objects(ob_main)

        self.file.write("\n</Scene>\n</X3D>")

        # if use_apply_modifiers:
        # 	if containerMesh:
        # 		containerMesh.vertices = None

        self.cleanup()