Skip to content
Snippets Groups Projects
export_x3d.py 54.3 KiB
Newer Older
  • Learn to ignore specific revisions
  •             filepath = image.filepath
                filepath_full = bpy.path.abspath(filepath)
                # collect image paths, can load multiple
    
                if bpy.path.is_subdir(filepath_full, dirname):
                    images.append(os.path.relpath(filepath_full, dirname))
    
    
                images.append(os.path.basename(filepath_full))
    
                
                images = [f.replace('\\', '/') for f in images]
                images = [f for i, f in enumerate(images) if f not in images[:i]]
    
                fw(ident_step + "url='%s' " % ' '.join(['"%s"' % f for f in images]))
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + '/>\n')
    
        def writeBackground(ident, world):
    
            # note, not re-used
            world_id = unique_name(world, 'WO_' + world.name, uuid_cache_world, clean_func=quoteattr)
    
    
            blending = world.use_sky_blend, world.use_sky_paper, world.use_sky_real
    
    
            grd_triple = clamp_color(world.horizon_color)
            sky_triple = clamp_color(world.zenith_color)
            mix_triple = clamp_color((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3))
    
    Campbell Barton's avatar
    Campbell Barton committed
            ident_step = ident + (' ' * (-len(ident) + \
            fw('%s<Background ' % ident)))
    
            # No Skytype - just Hor color
            if blending == (False, False, False):
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + 'groundColor="%.3g %.3g %.3g"\n' % grd_triple)
                fw(ident_step + 'skyColor="%.3g %.3g %.3g"\n' % grd_triple)
    
            # Blend Gradient
            elif blending == (True, False, False):
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + 'groundColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (grd_triple + mix_triple))
                fw(ident_step + 'groundAngle="1.57, 1.57"\n')
                fw(ident_step + 'skyColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + mix_triple))
                fw(ident_step + 'skyAngle="1.57, 1.57"\n')
    
            # Blend+Real Gradient Inverse
            elif blending == (True, False, True):
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + 'groundColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple))
                fw(ident_step + 'groundAngle="1.57"\n')
                fw(ident_step + 'skyColor="%.3g %.3g %.3g, %.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple + sky_triple))
                fw(ident_step + 'skyAngle="1.57, 3.14159"\n')
    
            # Paper - just Zen Color
            elif blending == (False, False, True):
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + 'groundColor="%.3g %.3g %.3g"\n' % sky_triple)
                fw(ident_step + 'skyColor="%.3g %.3g %.3g"\n' % sky_triple)
    
            # Blend+Real+Paper - komplex gradient
            elif blending == (True, True, True):
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + 'groundColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple))
                fw(ident_step + 'groundAngle="1.57, 1.57"\n')
                fw(ident_step + 'skyColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple))
                fw(ident_step + 'skyAngle="1.57, 1.57"\n')
    
    Campbell Barton's avatar
    Campbell Barton committed
                fw(ident_step + 'groundColor="%.3g %.3g %.3g"\n' % grd_triple)
                fw(ident_step + 'skyColor="%.3g %.3g %.3g"\n' % sky_triple)
    
            for tex in bpy.data.textures:
    
    Campbell Barton's avatar
    Campbell Barton committed
                if tex.type == 'IMAGE' and tex.image:
                    namemat = tex.name
                    pic = tex.image
                    basename = os.path.basename(bpy.path.abspath(pic.filepath))
    
                    if namemat == 'back':
    
    Campbell Barton's avatar
    Campbell Barton committed
                        fw(ident_step + 'backUrl="%s"\n' % basename)
    
                    elif namemat == 'bottom':
    
    Campbell Barton's avatar
    Campbell Barton committed
                        fw(ident_step + 'bottomUrl="%s"\n' % basename)
    
                    elif namemat == 'front':
    
    Campbell Barton's avatar
    Campbell Barton committed
                        fw(ident_step + 'frontUrl="%s"\n' % basename)
    
                    elif namemat == 'left':
    
    Campbell Barton's avatar
    Campbell Barton committed
                        fw(ident_step + 'leftUrl="%s"\n' % basename)
    
                    elif namemat == 'right':
    
    Campbell Barton's avatar
    Campbell Barton committed
                        fw(ident_step + 'rightUrl="%s"\n' % basename)
    
                    elif namemat == 'top':
    
    Campbell Barton's avatar
    Campbell Barton committed
                        fw(ident_step + 'topUrl="%s"\n' % basename)
    
    Campbell Barton's avatar
    Campbell Barton committed
            fw(ident_step + '/>\n')
    
        # -------------------------------------------------------------------------
        # Export Object Hierarchy (recursively called)
        # -------------------------------------------------------------------------
        def export_object(ident, obj_main_parent, obj_main, obj_children):
            world = scene.world
            free, derived = create_derived_objects(scene, obj_main)
    
            if derived is None:
                return
    
            if use_hierarchy:
                obj_main_matrix_world = obj_main.matrix_world
                if obj_main_parent:
                    obj_main_matrix = obj_main_parent.matrix_world.inverted() * obj_main_matrix_world
                else:
                    obj_main_matrix = obj_main_matrix_world
                obj_main_matrix_world_invert = obj_main_matrix_world.inverted()
    
                obj_main_id = unique_name(obj_main, obj_main.name, uuid_cache_object, clean_func=quoteattr)
                    
                ident = writeTransform_begin(ident, obj_main_matrix if obj_main_parent else global_matrix * obj_main_matrix, obj_main_id)
    
            for obj, obj_matrix in derived:
                obj_type = obj.type
                
                if use_hierarchy:
                    # make transform node relative
                    obj_matrix = obj_main_matrix_world_invert * obj_matrix
    
                if obj_type == 'CAMERA':
                    writeViewpoint(ident, obj, obj_matrix, scene)
                elif obj_type in ('MESH', 'CURVE', 'SURF', 'FONT'):
                    if (obj_type != 'MESH') or (use_apply_modifiers and obj.is_modified(scene, 'PREVIEW')):
                        try:
                            me = obj.to_mesh(scene, use_apply_modifiers, 'PREVIEW')
                        except:
                            me = None
                    else:
                        me = obj.data
    
                    if me is not None:
                        writeIndexedFaceSet(ident, obj, me, obj_matrix, world)
    
                        # free mesh created with create_mesh()
                        if me != obj.data:
                            bpy.data.meshes.remove(me)
    
                elif obj_type == 'LAMP':
                    data = obj.data
                    datatype = data.type
                    if datatype == 'POINT':
                        writePointLight(ident, obj, obj_matrix, data, world)
                    elif datatype == 'SPOT':
                        writeSpotLight(ident, obj, obj_matrix, data, world)
                    elif datatype == 'SUN':
                        writeDirectionalLight(ident, obj, obj_matrix, data, world)
                    else:
                        writeDirectionalLight(ident, obj, obj_matrix, data, world)
                else:
                    #print "Info: Ignoring [%s], object type [%s] not handle yet" % (object.name,object.getType)
                    pass
    
            if free:
                free_derived_objects(obj_main)
    
            # ---------------------------------------------------------------------
            # write out children recursively
            # ---------------------------------------------------------------------
            for obj_child, obj_child_children in obj_children:
                export_object(ident, obj_main, obj_child, obj_child_children)
    
            if use_hierarchy:
                ident = writeTransform_end(ident)
    
        # -------------------------------------------------------------------------
        # Main Export Function
        # -------------------------------------------------------------------------
    
            world = scene.world
    
    
            # 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...' % file.name)
            ident = ''
    
            ident = writeHeader(ident)
    
            writeNavigationInfo(ident, scene)
            writeBackground(ident, world)
            writeFog(ident, world)
    
            ident = '\t\t'
    
                objects = [obj for obj in scene.objects if obj.is_visible(scene) and o.select]
    
                objects = [obj for obj in scene.objects if obj.is_visible(scene)]
    
            if use_hierarchy:
                objects_hierarchy = build_hierarchy(objects)
            else:
                objects_hierarchy = ((obj, []) for obj in objects)
    
            for obj_main, obj_main_children in objects_hierarchy:
                export_object(ident, None, obj_main, obj_main_children)
    
            ident = writeFooter(ident)
    
    Campbell Barton's avatar
    Campbell Barton committed
    
        # -------------------------------------------------------------------------
        # global cleanup
        # -------------------------------------------------------------------------
    
    Campbell Barton's avatar
    Campbell Barton committed
        if use_h3d:
            bpy.data.materials.remove(gpu_shader_dummy_mat)
    
    
        print('Info: finished X3D export to %r' % file.name)
    
    
    
    ##########################################################
    # Callbacks, needed before Main
    ##########################################################
    
    
    def save(operator, context, filepath="",
    
             use_selection=True,
             use_apply_modifiers=False,
             use_triangulate=False,
    
             use_normals=False,
    
             use_compress=False,
    
    Campbell Barton's avatar
    Campbell Barton committed
             use_h3d=False,
    
             global_matrix=None,
             ):
    
        bpy.path.ensure_ext(filepath, '.x3dz' if use_compress else '.x3d')
    
    
        if bpy.ops.object.mode_set.poll():
            bpy.ops.object.mode_set(mode='OBJECT')
    
    
        if use_compress:
    
                file = gzip.open(filepath, 'w')
    
                print('failed to import compression modules, exporting uncompressed')
    
                filepath = filepath[:-1]  # remove trailing z
    
        if file is None:
    
            file = open(filepath, 'w')
    
        if global_matrix is None:
            global_matrix = mathutils.Matrix()
    
    
        export(file,
               global_matrix,
               context.scene,
               use_apply_modifiers=use_apply_modifiers,
               use_selection=use_selection,
    
               use_triangulate=use_triangulate,
               use_normals=use_normals,
    
    Campbell Barton's avatar
    Campbell Barton committed
               use_h3d=use_h3d,