Merge branch 'master' into blender2.8
[blender.git] / source / blender / collada / DocumentImporter.cpp
index b77fdf0..aab5a4e 100644 (file)
@@ -57,12 +57,12 @@ extern "C" {
 #include "BLI_fileops.h"
 
 #include "BKE_camera.h"
+#include "BKE_collection.h"
 #include "BKE_main.h"
+#include "BKE_layer.h"
 #include "BKE_lamp.h"
 #include "BKE_library.h"
-#include "BKE_texture.h"
 #include "BKE_fcurve.h"
-#include "BKE_depsgraph.h"
 #include "BKE_scene.h"
 #include "BKE_global.h"
 #include "BKE_material.h"
@@ -82,6 +82,9 @@ extern "C" {
 
 }
 
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_build.h"
+
 #include "ExtraHandler.h"
 #include "ErrorHandler.h"
 #include "DocumentImporter.h"
@@ -104,8 +107,9 @@ DocumentImporter::DocumentImporter(bContext *C, const ImportSettings *import_set
        import_settings(import_settings),
        mImportStage(General),
        mContext(C),
-       armature_importer(&unit_converter, &mesh_importer, CTX_data_scene(C), import_settings),
-       mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)),
+       view_layer(CTX_data_view_layer(mContext)),
+       armature_importer(&unit_converter, &mesh_importer, CTX_data_scene(C), view_layer, import_settings),
+       mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C), view_layer),
        anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C))
 {
 }
@@ -130,7 +134,7 @@ bool DocumentImporter::import()
        loader.registerExtraDataCallbackHandler(ehandler);
 
        // deselect all to select new objects
-       BKE_scene_base_deselect_all(CTX_data_scene(mContext));
+       BKE_view_layer_base_deselect_all(view_layer);
 
        std::string mFilename = std::string(this->import_settings->filepath);
        const std::string encodedFilename = bc_url_encode(mFilename);
@@ -230,7 +234,7 @@ void DocumentImporter::finish()
                }
 
                // update scene
-               DAG_relations_tag_update(bmain);
+               DEG_relations_tag_update(bmain);
                WM_event_add_notifier(mContext, NC_OBJECT | ND_TRANSFORM, NULL);
 
        }
@@ -241,7 +245,7 @@ void DocumentImporter::finish()
        armature_importer.set_tags_map(this->uid_tags_map);
        armature_importer.make_armatures(mContext, *objects_to_scale);
        armature_importer.make_shape_keys();
-       DAG_relations_tag_update(bmain);
+       DEG_relations_tag_update(bmain);
 
 #if 0
        armature_importer.fix_animation();
@@ -259,22 +263,14 @@ void DocumentImporter::finish()
 
                fprintf(stderr, "got %d library nodes to free\n", (int)libnode_ob.size());
                // free all library_nodes
-               std::vector<Object *>::iterator lit;
-               for (lit = libnode_ob.begin(); lit != libnode_ob.end(); lit++) {
-                       Object *ob = *lit;
-
-                       Base *base = BKE_scene_base_find(sce, ob);
-                       if (base) {
-                               BLI_remlink(&sce->base, base);
-                               BKE_libblock_free_us(G.main, base->object);
-                               if (sce->basact == base)
-                                       sce->basact = NULL;
-                               MEM_freeN(base);
-                       }
+               std::vector<Object *>::iterator it;
+               for (it = libnode_ob.begin(); it != libnode_ob.end(); it++) {
+                       Object *ob = *it;
+                       BKE_scene_collections_object_remove(G.main, sce, ob, true);
                }
                libnode_ob.clear();
 
-               DAG_relations_tag_update(bmain);
+               DEG_relations_tag_update(bmain);
        }
 
        bc_match_scale(objects_to_scale, unit_converter, !this->import_settings->import_units);
@@ -383,7 +379,7 @@ Object *DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera
                return NULL;
        }
 
-       Object *ob = bc_add_object(sce, OB_CAMERA, NULL);
+       Object *ob = bc_add_object(sce, view_layer, OB_CAMERA, NULL);
        Camera *cam = uid_camera_map[cam_uid];
        Camera *old_cam = (Camera *)ob->data;
        ob->data = cam;
@@ -399,7 +395,7 @@ Object *DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Sce
                return NULL;
        }
 
-       Object *ob = bc_add_object(sce, OB_LAMP, NULL);
+       Object *ob = bc_add_object(sce, view_layer, OB_LAMP, NULL);
        Lamp *la = uid_lamp_map[lamp_uid];
        Lamp *old_lamp = (Lamp *)ob->data;
        ob->data = la;
@@ -412,8 +408,8 @@ Object *DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Nod
        fprintf(stderr, "create <instance_node> under node id=%s from node id=%s\n", instance_node ? instance_node->getOriginalId().c_str() : NULL, source_node ? source_node->getOriginalId().c_str() : NULL);
 
        Object *obn = BKE_object_copy(G.main, source_ob);
-       DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
-       BKE_scene_base_add(sce, obn);
+       DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
+       BKE_collection_object_add_from(G.main, sce, source_ob, obn);
 
        if (instance_node) {
                anim_importer.read_node_transform(instance_node, obn);
@@ -515,7 +511,7 @@ std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA
                if (parent_node == NULL && !is_library_node) {
                        // A Joint on root level is a skeleton without root node.
                        // Here we add the armature "on the fly":
-                       par = bc_add_object(sce, OB_ARMATURE, std::string("Armature").c_str());
+                       par = bc_add_object(sce, view_layer, OB_ARMATURE, std::string("Armature").c_str());
                        objects_done->push_back(par);
                        root_objects->push_back(par);
                        object_map.insert(std::pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), par));
@@ -547,8 +543,7 @@ std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA
                // maybe join multiple <instance_...> meshes into 1, and link object with it? not sure...
                // <instance_geometry>
                while (geom_done < geom.getCount()) {
-                       ob = mesh_importer.create_mesh_object(node, geom[geom_done], false, uid_material_map,
-                                                             material_texture_mapping_map);
+                       ob = mesh_importer.create_mesh_object(node, geom[geom_done], false, uid_material_map);
                        if (ob == NULL) {
                                report_unknown_reference(*node, "instance_mesh");
                        }
@@ -588,7 +583,7 @@ std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA
                }
                while (controller_done < controller.getCount()) {
                        COLLADAFW::InstanceGeometry *geometry = (COLLADAFW::InstanceGeometry *)controller[controller_done];
-                       ob = mesh_importer.create_mesh_object(node, geometry, true, uid_material_map, material_texture_mapping_map);
+                       ob = mesh_importer.create_mesh_object(node, geometry, true, uid_material_map);
                        if (ob == NULL) {
                                report_unknown_reference(*node, "instance_controller");
                        }
@@ -629,10 +624,10 @@ std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLA
                if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) {
                        //Check if Object is armature, by checking if immediate child is a JOINT node.
                        if (is_armature(node)) {
-                               ob = bc_add_object(sce, OB_ARMATURE, name.c_str());
+                               ob = bc_add_object(sce, view_layer, OB_ARMATURE, name.c_str());
                        }
                        else {
-                               ob = bc_add_object(sce, OB_EMPTY, NULL);
+                               ob = bc_add_object(sce, view_layer, OB_EMPTY, NULL);
                        }
                        objects_done->push_back(ob);
                        if (parent_node == NULL) {
@@ -768,102 +763,74 @@ bool DocumentImporter::writeMaterial(const COLLADAFW::Material *cmat)
        return true;
 }
 
-// create mtex, create texture, set texture image
-MTex *DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
-                                       int i, TexIndexTextureArrayMap &texindex_texarray_map)
-{
-       COLLADAFW::SamplerPointerArray& samp_array = ef->getSamplerPointerArray();
-       COLLADAFW::Sampler *sampler = samp_array[ctex.getSamplerId()];
-
-       const COLLADAFW::UniqueId& ima_uid = sampler->getSourceImage();
-
-       if (uid_image_map.find(ima_uid) == uid_image_map.end()) {
-               fprintf(stderr, "Couldn't find an image by UID.\n");
-               return NULL;
-       }
-
-       ma->mtex[i] = BKE_texture_mtex_add();
-       ma->mtex[i]->texco = TEXCO_UV;
-       ma->mtex[i]->tex = BKE_texture_add(G.main, "Texture");
-       ma->mtex[i]->tex->type = TEX_IMAGE;
-       ma->mtex[i]->tex->ima = uid_image_map[ima_uid];
-
-       texindex_texarray_map[ctex.getTextureMapId()].push_back(ma->mtex[i]);
-
-       return ma->mtex[i];
-}
-
 void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
 {
        COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
 
+       // TODO: add back texture and extended material parameter support
+
        // blinn
        if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
+#if 0
                ma->spec_shader = MA_SPEC_BLINN;
                ma->spec = ef->getShininess().getFloatValue();
+#endif
        }
        // phong
        else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
+#if 0
                ma->spec_shader = MA_SPEC_PHONG;
                ma->har = ef->getShininess().getFloatValue();
+#endif
        }
        // lambert
        else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
+#if 0
                ma->diff_shader = MA_DIFF_LAMBERT;
+#endif
        }
        // default - lambert
        else {
+#if 0
                ma->diff_shader = MA_DIFF_LAMBERT;
                fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
+#endif
        }
        // reflectivity
-       ma->ray_mirror = ef->getReflectivity().getFloatValue();
+       ma->metallic = ef->getReflectivity().getFloatValue();
        // index of refraction
+#if 0
        ma->ang = ef->getIndexOfRefraction().getFloatValue();
+#endif
 
-       int i = 0;
        COLLADAFW::Color col;
-       MTex *mtex = NULL;
-       TexIndexTextureArrayMap texindex_texarray_map;
 
        // DIFFUSE
        // color
        if (ef->getDiffuse().isColor()) {
-               /* too high intensity can create artefacts (fireflies)
-                  So here we take care that intensity is set to 0.8 wherever possible
-               */
                col = ef->getDiffuse().getColor();
-               ma->ref = max_ffff(col.getRed(), col.getGreen(), col.getBlue(), 0.8);
-               ma->r = col.getRed()   / ma->ref;
-               ma->g = col.getGreen() / ma->ref;
-               ma->b = col.getBlue()  / ma->ref;
+               ma->r = col.getRed();
+               ma->g = col.getGreen();
+               ma->b = col.getBlue();
        }
        // texture
        else if (ef->getDiffuse().isTexture()) {
+#if 0
                COLLADAFW::Texture ctex = ef->getDiffuse().getTexture();
-               mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
-               if (mtex != NULL) {
-                       mtex->mapto = MAP_COL;
-                       ma->texact = (int)i;
-                       i++;
-               }
+#endif
        }
        // AMBIENT
        // color
        if (ef->getAmbient().isColor()) {
+#if 0
                col = ef->getAmbient().getColor();
-               ma->ambr = col.getRed();
-               ma->ambg = col.getGreen();
-               ma->ambb = col.getBlue();
+#endif
        }
        // texture
        else if (ef->getAmbient().isTexture()) {
+#if 0
                COLLADAFW::Texture ctex = ef->getAmbient().getTexture();
-               mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
-               if (mtex != NULL) {
-                       mtex->mapto = MAP_AMB;
-                       i++;
-               }
+#endif
        }
        // SPECULAR
        // color
@@ -875,29 +842,22 @@ void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Materia
        }
        // texture
        else if (ef->getSpecular().isTexture()) {
+#if 0
                COLLADAFW::Texture ctex = ef->getSpecular().getTexture();
-               mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
-               if (mtex != NULL) {
-                       mtex->mapto = MAP_SPEC;
-                       i++;
-               }
+#endif
        }
        // REFLECTIVE
        // color
        if (ef->getReflective().isColor()) {
+#if 0
                col = ef->getReflective().getColor();
-               ma->mirr = col.getRed();
-               ma->mirg = col.getGreen();
-               ma->mirb = col.getBlue();
+#endif
        }
        // texture
        else if (ef->getReflective().isTexture()) {
+#if 0
                COLLADAFW::Texture ctex = ef->getReflective().getTexture();
-               mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
-               if (mtex != NULL) {
-                       mtex->mapto = MAP_REF;
-                       i++;
-               }
+#endif
        }
 
        // EMISSION
@@ -908,40 +868,31 @@ void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Materia
        }
        // texture
        else if (ef->getEmission().isTexture()) {
+#if 0
                COLLADAFW::Texture ctex = ef->getEmission().getTexture();
-               mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
-               if (mtex != NULL) {
-                       mtex->mapto = MAP_EMIT;
-                       i++;
-               }
+#endif
        }
 
        // TRANSPARENT
        // color
        if (ef->getOpacity().isColor()) {
+#if 0
                col = ef->getTransparent().getColor();
                float alpha = ef->getTransparency().getFloatValue();
                if (col.isValid()) {
                        alpha *= col.getAlpha(); // Assuming A_ONE opaque mode
                }
                if (col.isValid() || alpha < 1.0) {
-                       ma->alpha = alpha;
-                       ma->mode |= MA_ZTRANSP | MA_TRANSP;
+                       ...
                }
+#endif
        }
        // texture
        else if (ef->getOpacity().isTexture()) {
+#if 0
                COLLADAFW::Texture ctex = ef->getOpacity().getTexture();
-               mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
-               if (mtex != NULL) {
-                       mtex->mapto = MAP_ALPHA;
-                       i++;
-                       ma->spectra = ma->alpha = 0;
-                       ma->mode |= MA_ZTRANSP | MA_TRANSP;
-               }
+#endif
        }
-
-       material_texture_mapping_map[ma] = texindex_texarray_map;
 }
 
 /** When this method is called, the writer must write the effect.
@@ -1188,7 +1139,6 @@ bool DocumentImporter::writeLight(const COLLADAFW::Light *light)
                et->setData("spotsize", &(lamp->spotsize));
                lamp->spotsize = DEG2RADF(lamp->spotsize);
                et->setData("spotblend", &(lamp->spotblend));
-               et->setData("halo_intensity", &(lamp->haint));
                et->setData("att1", &(lamp->att1));
                et->setData("att2", &(lamp->att2));
                et->setData("falloff_type", &(lamp->falloff_type));
@@ -1196,38 +1146,12 @@ bool DocumentImporter::writeLight(const COLLADAFW::Light *light)
                et->setData("clipend", &(lamp->clipend));
                et->setData("bias", &(lamp->bias));
                et->setData("soft", &(lamp->soft));
-               et->setData("compressthresh", &(lamp->compressthresh));
                et->setData("bufsize", &(lamp->bufsize));
-               et->setData("samp", &(lamp->samp));
                et->setData("buffers", &(lamp->buffers));
-               et->setData("filtertype", &(lamp->filtertype));
-               et->setData("bufflag", &(lamp->bufflag));
-               et->setData("buftype", &(lamp->buftype));
-               et->setData("ray_samp", &(lamp->ray_samp));
-               et->setData("ray_sampy", &(lamp->ray_sampy));
-               et->setData("ray_sampz", &(lamp->ray_sampz));
-               et->setData("ray_samp_type", &(lamp->ray_samp_type));
                et->setData("area_shape", &(lamp->area_shape));
                et->setData("area_size", &(lamp->area_size));
                et->setData("area_sizey", &(lamp->area_sizey));
                et->setData("area_sizez", &(lamp->area_sizez));
-               et->setData("adapt_thresh", &(lamp->adapt_thresh));
-               et->setData("ray_samp_method", &(lamp->ray_samp_method));
-               et->setData("shadhalostep", &(lamp->shadhalostep));
-               et->setData("sun_effect_type", &(lamp->shadhalostep));
-               et->setData("skyblendtype", &(lamp->skyblendtype));
-               et->setData("horizon_brightness", &(lamp->horizon_brightness));
-               et->setData("spread", &(lamp->spread));
-               et->setData("sun_brightness", &(lamp->sun_brightness));
-               et->setData("sun_size", &(lamp->sun_size));
-               et->setData("backscattered_light", &(lamp->backscattered_light));
-               et->setData("sun_intensity", &(lamp->sun_intensity));
-               et->setData("atm_turbidity", &(lamp->atm_turbidity));
-               et->setData("atm_extinction_factor", &(lamp->atm_extinction_factor));
-               et->setData("atm_distance_factor", &(lamp->atm_distance_factor));
-               et->setData("skyblendfac", &(lamp->skyblendfac));
-               et->setData("sky_exposure", &(lamp->sky_exposure));
-               et->setData("sky_colorspace", &(lamp->sky_colorspace));
        }
        else {
                float constatt = light->getConstantAttenuation().getValue();
@@ -1291,7 +1215,6 @@ bool DocumentImporter::writeLight(const COLLADAFW::Light *light)
                        {
                                /* our sun is very strong, so pick a smaller energy level */
                                lamp->type = LA_SUN;
-                               lamp->mode |= LA_NO_SPEC;
                        }
                        break;
                        case COLLADAFW::Light::POINT_LIGHT: