void BKE_modifier_init(void) { ModifierData *md; /* Initialize modifier types */ modifier_type_init(modifier_types); /* MOD_utils.c */ /* Initialize global cmmon storage used for virtual modifier list */ md = modifier_new(eModifierType_Armature); virtualModifierCommonData.amd = *((ArmatureModifierData *) md); modifier_free(md); md = modifier_new(eModifierType_Curve); virtualModifierCommonData.cmd = *((CurveModifierData *) md); modifier_free(md); md = modifier_new(eModifierType_Lattice); virtualModifierCommonData.lmd = *((LatticeModifierData *) md); modifier_free(md); md = modifier_new(eModifierType_ShapeKey); virtualModifierCommonData.smd = *((ShapeKeyModifierData *) md); modifier_free(md); virtualModifierCommonData.amd.modifier.mode |= eModifierMode_Virtual; virtualModifierCommonData.cmd.modifier.mode |= eModifierMode_Virtual; virtualModifierCommonData.lmd.modifier.mode |= eModifierMode_Virtual; virtualModifierCommonData.smd.modifier.mode |= eModifierMode_Virtual; }
void multiresModifier_join(Object *ob) { Base *base = NULL; int highest_lvl = 0; /* First find the highest level of subdivision */ base = FIRSTBASE; while(base) { if(TESTBASELIB_BGMODE(v3d, base) && base->object->type==OB_MESH) { ModifierData *md; for(md = base->object->modifiers.first; md; md = md->next) { if(md->type == eModifierType_Multires) { int totlvl = ((MultiresModifierData*)md)->totlvl; if(totlvl > highest_lvl) highest_lvl = totlvl; /* Ensure that all updates are processed */ multires_force_update(base->object); } } } base = base->next; } /* No multires meshes selected */ if(highest_lvl == 0) return; /* Subdivide all the displacements to the highest level */ base = FIRSTBASE; while(base) { if(TESTBASELIB_BGMODE(v3d, base) && base->object->type==OB_MESH) { ModifierData *md = NULL; MultiresModifierData *mmd = NULL; for(md = base->object->modifiers.first; md; md = md->next) { if(md->type == eModifierType_Multires) mmd = (MultiresModifierData*)md; } /* If the object didn't have multires enabled, give it a new modifier */ if(!mmd) { md = base->object->modifiers.first; while(md && modifierType_getInfo(md->type)->type == eModifierTypeType_OnlyDeform) md = md->next; mmd = (MultiresModifierData*)modifier_new(eModifierType_Multires); BLI_insertlinkbefore(&base->object->modifiers, md, mmd); modifier_unique_name(&base->object->modifiers, mmd); } if(mmd) multiresModifier_subdivide(mmd, base->object, highest_lvl - mmd->totlvl, 0, 0); } base = base->next; } }
static void rna_FluidSettings_update_type(Main *bmain, Scene *scene, PointerRNA *ptr) { Object *ob = (Object*)ptr->id.data; FluidsimModifierData *fluidmd; ParticleSystemModifierData *psmd; ParticleSystem *psys; ParticleSettings *part; fluidmd = (FluidsimModifierData*)modifiers_findByType(ob, eModifierType_Fluidsim); fluidmd->fss->flag &= ~OB_FLUIDSIM_REVERSE; /* clear flag */ /* remove fluidsim particle system */ if (fluidmd->fss->type & OB_FLUIDSIM_PARTICLE) { for (psys = ob->particlesystem.first; psys; psys = psys->next) if (psys->part->type == PART_FLUID) break; if (ob->type == OB_MESH && !psys) { /* add particle system */ part = psys_new_settings("ParticleSettings", bmain); psys = MEM_callocN(sizeof(ParticleSystem), "particle_system"); part->type = PART_FLUID; psys->part = part; psys->pointcache = BKE_ptcache_add(&psys->ptcaches); psys->flag |= PSYS_ENABLED; BLI_strncpy(psys->name, "FluidParticles", sizeof(psys->name)); BLI_addtail(&ob->particlesystem,psys); /* add modifier */ psmd = (ParticleSystemModifierData*)modifier_new(eModifierType_ParticleSystem); BLI_strncpy(psmd->modifier.name, "FluidParticleSystem", sizeof(psmd->modifier.name)); psmd->psys = psys; BLI_addtail(&ob->modifiers, psmd); modifier_unique_name(&ob->modifiers, (ModifierData *)psmd); } } else { for (psys = ob->particlesystem.first; psys; psys = psys->next) { if (psys->part->type == PART_FLUID) { /* clear modifier */ psmd = psys_get_modifier(ob, psys); BLI_remlink(&ob->modifiers, psmd); modifier_free((ModifierData *)psmd); /* clear particle system */ BLI_remlink(&ob->particlesystem, psys); psys_free(ob, psys); } } } rna_fluid_update(bmain, scene, ptr); }
int ED_object_modifier_copy(ReportList *UNUSED(reports), Object *ob, ModifierData *md) { ModifierData *nmd; nmd = modifier_new(md->type); modifier_copyData(md, nmd); BLI_insertlink(&ob->modifiers, md, nmd); modifier_unique_name(&ob->modifiers, nmd); return 1; }
static void add_hook_object(Main *bmain, Scene *scene, Object *obedit, Object *ob, int mode) { ModifierData *md=NULL; HookModifierData *hmd = NULL; float cent[3]; int tot, ok, *indexar; char name[32]; ok = object_hook_index_array(obedit, &tot, &indexar, name, cent); if (!ok) return; // XXX error("Requires selected vertices or active Vertex Group"); if (mode==OBJECT_ADDHOOK_NEWOB && !ob) { ob = add_hook_object_new(scene, obedit); /* transform cent to global coords for loc */ mul_v3_m4v3(ob->loc, obedit->obmat, cent); } md = obedit->modifiers.first; while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) { md = md->next; } hmd = (HookModifierData*) modifier_new(eModifierType_Hook); BLI_insertlinkbefore(&obedit->modifiers, md, hmd); BLI_snprintf(hmd->modifier.name, sizeof(hmd->modifier.name), "Hook-%s", ob->id.name+2); modifier_unique_name(&obedit->modifiers, (ModifierData*)hmd); hmd->object= ob; hmd->indexar= indexar; copy_v3_v3(hmd->cent, cent); hmd->totindex= tot; BLI_strncpy(hmd->name, name, sizeof(hmd->name)); /* matrix calculus */ /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */ /* (parentinv ) */ where_is_object(scene, ob); invert_m4_m4(ob->imat, ob->obmat); /* apparently this call goes from right to left... */ mul_serie_m4(hmd->parentinv, ob->imat, obedit->obmat, NULL, NULL, NULL, NULL, NULL, NULL); DAG_scene_sort(bmain, scene); }
static int bake( Render *re, Main *bmain, Scene *scene, Object *ob_low, ListBase *selected_objects, ReportList *reports, const ScenePassType pass_type, const int margin, const BakeSaveMode save_mode, const bool is_clear, const bool is_split_materials, const bool is_automatic_name, const bool is_selected_to_active, const bool is_cage, const float cage_extrusion, const int normal_space, const BakeNormalSwizzle normal_swizzle[], const char *custom_cage, const char *filepath, const int width, const int height, const char *identifier, ScrArea *sa, const char *uv_layer) { int op_result = OPERATOR_CANCELLED; bool ok = false; Object *ob_cage = NULL; BakeHighPolyData *highpoly = NULL; int tot_highpoly = 0; char restrict_flag_low = ob_low->restrictflag; char restrict_flag_cage = 0; Mesh *me_low = NULL; Mesh *me_cage = NULL; float *result = NULL; BakePixel *pixel_array_low = NULL; BakePixel *pixel_array_high = NULL; const bool is_save_internal = (save_mode == R_BAKE_SAVE_INTERNAL); const bool is_noncolor = is_noncolor_pass(pass_type); const int depth = RE_pass_depth(pass_type); BakeImages bake_images = {NULL}; size_t num_pixels; int tot_materials; RE_bake_engine_set_engine_parameters(re, bmain, scene); if (!RE_bake_has_engine(re)) { BKE_report(reports, RPT_ERROR, "Current render engine does not support baking"); goto cleanup; } tot_materials = ob_low->totcol; if (uv_layer && uv_layer[0] != '\0') { Mesh *me = (Mesh *)ob_low->data; if (CustomData_get_named_layer(&me->ldata, CD_MLOOPUV, uv_layer) == -1) { BKE_reportf(reports, RPT_ERROR, "No UV layer named \"%s\" found in the object \"%s\"", uv_layer, ob_low->id.name + 2); goto cleanup; } } if (tot_materials == 0) { if (is_save_internal) { BKE_report(reports, RPT_ERROR, "No active image found, add a material or bake to an external file"); goto cleanup; } else if (is_split_materials) { BKE_report(reports, RPT_ERROR, "No active image found, add a material or bake without the Split Materials option"); goto cleanup; } else { /* baking externally without splitting materials */ tot_materials = 1; } } /* we overallocate in case there is more materials than images */ bake_images.data = MEM_mallocN(sizeof(BakeImage) * tot_materials, "bake images dimensions (width, height, offset)"); bake_images.lookup = MEM_mallocN(sizeof(int) * tot_materials, "bake images lookup (from material to BakeImage)"); build_image_lookup(bmain, ob_low, &bake_images); if (is_save_internal) { num_pixels = initialize_internal_images(&bake_images, reports); if (num_pixels == 0) { goto cleanup; } } else { /* when saving extenally always use the size specified in the UI */ num_pixels = (size_t)width * (size_t)height * bake_images.size; for (int i = 0; i < bake_images.size; i++) { bake_images.data[i].width = width; bake_images.data[i].height = height; bake_images.data[i].offset = (is_split_materials ? num_pixels : 0); bake_images.data[i].image = NULL; } if (!is_split_materials) { /* saving a single image */ for (int i = 0; i < tot_materials; i++) { bake_images.lookup[i] = 0; } } } if (is_selected_to_active) { CollectionPointerLink *link; tot_highpoly = 0; for (link = selected_objects->first; link; link = link->next) { Object *ob_iter = link->ptr.data; if (ob_iter == ob_low) continue; tot_highpoly ++; } if (is_cage && custom_cage[0] != '\0') { ob_cage = BLI_findstring(&bmain->object, custom_cage, offsetof(ID, name) + 2); if (ob_cage == NULL || ob_cage->type != OB_MESH) { BKE_report(reports, RPT_ERROR, "No valid cage object"); goto cleanup; } else { restrict_flag_cage = ob_cage->restrictflag; ob_cage->restrictflag |= OB_RESTRICT_RENDER; } } } pixel_array_low = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels low poly"); pixel_array_high = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels high poly"); result = MEM_callocN(sizeof(float) * depth * num_pixels, "bake return pixels"); /* get the mesh as it arrives in the renderer */ me_low = BKE_mesh_new_from_object(bmain, scene, ob_low, 1, 2, 0, 0); BKE_mesh_split_faces(me_low); /* populate the pixel array with the face data */ if ((is_selected_to_active && (ob_cage == NULL) && is_cage) == false) RE_bake_pixels_populate(me_low, pixel_array_low, num_pixels, &bake_images, uv_layer); /* else populate the pixel array with the 'cage' mesh (the smooth version of the mesh) */ if (is_selected_to_active) { CollectionPointerLink *link; ModifierData *md, *nmd; ListBase modifiers_tmp, modifiers_original; int i = 0; /* prepare cage mesh */ if (ob_cage) { me_cage = BKE_mesh_new_from_object(bmain, scene, ob_cage, 1, 2, 0, 0); BKE_mesh_split_faces(me_cage); if ((me_low->totpoly != me_cage->totpoly) || (me_low->totloop != me_cage->totloop)) { BKE_report(reports, RPT_ERROR, "Invalid cage object, the cage mesh must have the same number " "of faces as the active object"); goto cleanup; } } else if (is_cage) { modifiers_original = ob_low->modifiers; BLI_listbase_clear(&modifiers_tmp); for (md = ob_low->modifiers.first; md; md = md->next) { /* Edge Split cannot be applied in the cage, * the cage is supposed to have interpolated normals * between the faces unless the geometry is physically * split. So we create a copy of the low poly mesh without * the eventual edge split.*/ if (md->type == eModifierType_EdgeSplit) continue; nmd = modifier_new(md->type); BLI_strncpy(nmd->name, md->name, sizeof(nmd->name)); modifier_copyData(md, nmd); BLI_addtail(&modifiers_tmp, nmd); } /* temporarily replace the modifiers */ ob_low->modifiers = modifiers_tmp; /* get the cage mesh as it arrives in the renderer */ me_cage = BKE_mesh_new_from_object(bmain, scene, ob_low, 1, 2, 0, 0); BKE_mesh_split_faces(me_cage); RE_bake_pixels_populate(me_cage, pixel_array_low, num_pixels, &bake_images, uv_layer); } highpoly = MEM_callocN(sizeof(BakeHighPolyData) * tot_highpoly, "bake high poly objects"); /* populate highpoly array */ for (link = selected_objects->first; link; link = link->next) { TriangulateModifierData *tmd; Object *ob_iter = link->ptr.data; if (ob_iter == ob_low) continue; /* initialize highpoly_data */ highpoly[i].ob = ob_iter; highpoly[i].restrict_flag = ob_iter->restrictflag; /* triangulating so BVH returns the primitive_id that will be used for rendering */ highpoly[i].tri_mod = ED_object_modifier_add( reports, bmain, scene, highpoly[i].ob, "TmpTriangulate", eModifierType_Triangulate); tmd = (TriangulateModifierData *)highpoly[i].tri_mod; tmd->quad_method = MOD_TRIANGULATE_QUAD_FIXED; tmd->ngon_method = MOD_TRIANGULATE_NGON_EARCLIP; highpoly[i].me = BKE_mesh_new_from_object(bmain, scene, highpoly[i].ob, 1, 2, 0, 0); highpoly[i].ob->restrictflag &= ~OB_RESTRICT_RENDER; BKE_mesh_split_faces(highpoly[i].me); /* lowpoly to highpoly transformation matrix */ copy_m4_m4(highpoly[i].obmat, highpoly[i].ob->obmat); invert_m4_m4(highpoly[i].imat, highpoly[i].obmat); highpoly[i].is_flip_object = is_negative_m4(highpoly[i].ob->obmat); i++; } BLI_assert(i == tot_highpoly); ob_low->restrictflag |= OB_RESTRICT_RENDER; /* populate the pixel arrays with the corresponding face data for each high poly object */ if (!RE_bake_pixels_populate_from_objects( me_low, pixel_array_low, pixel_array_high, highpoly, tot_highpoly, num_pixels, ob_cage != NULL, cage_extrusion, ob_low->obmat, (ob_cage ? ob_cage->obmat : ob_low->obmat), me_cage)) { BKE_report(reports, RPT_ERROR, "Error handling selected objects"); goto cage_cleanup; } /* the baking itself */ for (i = 0; i < tot_highpoly; i++) { ok = RE_bake_engine(re, highpoly[i].ob, i, pixel_array_high, num_pixels, depth, pass_type, result); if (!ok) { BKE_reportf(reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2); goto cage_cleanup; } } cage_cleanup: /* reverting data back */ if ((ob_cage == NULL) && is_cage) { ob_low->modifiers = modifiers_original; while ((md = BLI_pophead(&modifiers_tmp))) { modifier_free(md); } } if (!ok) { goto cleanup; } } else { /* make sure low poly renders */ ob_low->restrictflag &= ~OB_RESTRICT_RENDER; if (RE_bake_has_engine(re)) { ok = RE_bake_engine(re, ob_low, 0, pixel_array_low, num_pixels, depth, pass_type, result); } else { BKE_report(reports, RPT_ERROR, "Current render engine does not support baking"); goto cleanup; } } /* normal space conversion * the normals are expected to be in world space, +X +Y +Z */ if (ok && pass_type == SCE_PASS_NORMAL) { switch (normal_space) { case R_BAKE_SPACE_WORLD: { /* Cycles internal format */ if ((normal_swizzle[0] == R_BAKE_POSX) && (normal_swizzle[1] == R_BAKE_POSY) && (normal_swizzle[2] == R_BAKE_POSZ)) { break; } else { RE_bake_normal_world_to_world(pixel_array_low, num_pixels, depth, result, normal_swizzle); } break; } case R_BAKE_SPACE_OBJECT: { RE_bake_normal_world_to_object(pixel_array_low, num_pixels, depth, result, ob_low, normal_swizzle); break; } case R_BAKE_SPACE_TANGENT: { if (is_selected_to_active) { RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_low, normal_swizzle, ob_low->obmat); } else { /* from multiresolution */ Mesh *me_nores = NULL; ModifierData *md = NULL; int mode; md = modifiers_findByType(ob_low, eModifierType_Multires); if (md) { mode = md->mode; md->mode &= ~eModifierMode_Render; } me_nores = BKE_mesh_new_from_object(bmain, scene, ob_low, 1, 2, 0, 0); BKE_mesh_split_faces(me_nores); RE_bake_pixels_populate(me_nores, pixel_array_low, num_pixels, &bake_images, uv_layer); RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_nores, normal_swizzle, ob_low->obmat); BKE_libblock_free(bmain, me_nores); if (md) md->mode = mode; } break; } default: break; } } if (!ok) { BKE_reportf(reports, RPT_ERROR, "Problem baking object \"%s\"", ob_low->id.name + 2); op_result = OPERATOR_CANCELLED; } else { /* save the results */ for (int i = 0; i < bake_images.size; i++) { BakeImage *bk_image = &bake_images.data[i]; if (is_save_internal) { ok = write_internal_bake_pixels( bk_image->image, pixel_array_low + bk_image->offset, result + bk_image->offset * depth, bk_image->width, bk_image->height, margin, is_clear, is_noncolor); /* might be read by UI to set active image for display */ bake_update_image(sa, bk_image->image); if (!ok) { BKE_reportf(reports, RPT_ERROR, "Problem saving the bake map internally for object \"%s\"", ob_low->id.name + 2); op_result = OPERATOR_CANCELLED; } else { BKE_report(reports, RPT_INFO, "Baking map saved to internal image, save it externally or pack it"); op_result = OPERATOR_FINISHED; } } /* save externally */ else { BakeData *bake = &scene->r.bake; char name[FILE_MAX]; BKE_image_path_from_imtype(name, filepath, bmain->name, 0, bake->im_format.imtype, true, false, NULL); if (is_automatic_name) { BLI_path_suffix(name, FILE_MAX, ob_low->id.name + 2, "_"); BLI_path_suffix(name, FILE_MAX, identifier, "_"); } if (is_split_materials) { if (bk_image->image) { BLI_path_suffix(name, FILE_MAX, bk_image->image->id.name + 2, "_"); } else { if (ob_low->mat[i]) { BLI_path_suffix(name, FILE_MAX, ob_low->mat[i]->id.name + 2, "_"); } else if (me_low->mat[i]) { BLI_path_suffix(name, FILE_MAX, me_low->mat[i]->id.name + 2, "_"); } else { /* if everything else fails, use the material index */ char tmp[4]; sprintf(tmp, "%d", i % 1000); BLI_path_suffix(name, FILE_MAX, tmp, "_"); } } } /* save it externally */ ok = write_external_bake_pixels( name, pixel_array_low + bk_image->offset, result + bk_image->offset * depth, bk_image->width, bk_image->height, margin, &bake->im_format, is_noncolor); if (!ok) { BKE_reportf(reports, RPT_ERROR, "Problem saving baked map in \"%s\"", name); op_result = OPERATOR_CANCELLED; } else { BKE_reportf(reports, RPT_INFO, "Baking map written to \"%s\"", name); op_result = OPERATOR_FINISHED; } if (!is_split_materials) { break; } } } } if (is_save_internal) refresh_images(&bake_images); cleanup: if (highpoly) { int i; for (i = 0; i < tot_highpoly; i++) { highpoly[i].ob->restrictflag = highpoly[i].restrict_flag; if (highpoly[i].tri_mod) ED_object_modifier_remove(reports, bmain, highpoly[i].ob, highpoly[i].tri_mod); if (highpoly[i].me) BKE_libblock_free(bmain, highpoly[i].me); } MEM_freeN(highpoly); } ob_low->restrictflag = restrict_flag_low; if (ob_cage) ob_cage->restrictflag = restrict_flag_cage; if (pixel_array_low) MEM_freeN(pixel_array_low); if (pixel_array_high) MEM_freeN(pixel_array_high); if (bake_images.data) MEM_freeN(bake_images.data); if (bake_images.lookup) MEM_freeN(bake_images.lookup); if (result) MEM_freeN(result); if (me_low) BKE_libblock_free(bmain, me_low); if (me_cage) BKE_libblock_free(bmain, me_cage); return op_result; }
ModifierData *modifiers_getVirtualModifierList(Object *ob) { /* Kinda hacky, but should be fine since we are never * reentrant and avoid free hassles. */ static ArmatureModifierData amd; static CurveModifierData cmd; static LatticeModifierData lmd; static ShapeKeyModifierData smd; static int init = 1; ModifierData *md; if (init) { md = modifier_new(eModifierType_Armature); amd = *((ArmatureModifierData*) md); modifier_free(md); md = modifier_new(eModifierType_Curve); cmd = *((CurveModifierData*) md); modifier_free(md); md = modifier_new(eModifierType_Lattice); lmd = *((LatticeModifierData*) md); modifier_free(md); md = modifier_new(eModifierType_ShapeKey); smd = *((ShapeKeyModifierData*) md); modifier_free(md); amd.modifier.mode |= eModifierMode_Virtual; cmd.modifier.mode |= eModifierMode_Virtual; lmd.modifier.mode |= eModifierMode_Virtual; smd.modifier.mode |= eModifierMode_Virtual; init = 0; } md = ob->modifiers.first; if(ob->parent) { if(ob->parent->type==OB_ARMATURE && ob->partype==PARSKEL) { amd.object = ob->parent; amd.modifier.next = md; amd.deformflag= ((bArmature *)(ob->parent->data))->deformflag; md = &amd.modifier; } else if(ob->parent->type==OB_CURVE && ob->partype==PARSKEL) { cmd.object = ob->parent; cmd.defaxis = ob->trackflag + 1; cmd.modifier.next = md; md = &cmd.modifier; } else if(ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) { lmd.object = ob->parent; lmd.modifier.next = md; md = &lmd.modifier; } } /* shape key modifier, not yet for curves */ if(ELEM(ob->type, OB_MESH, OB_LATTICE) && ob_get_key(ob)) { if(ob->type == OB_MESH && (ob->shapeflag & OB_SHAPE_EDIT_MODE)) smd.modifier.mode |= eModifierMode_Editmode|eModifierMode_OnCage; else smd.modifier.mode &= ~eModifierMode_Editmode|eModifierMode_OnCage; smd.modifier.next = md; md = &smd.modifier; } return md; }
ModifierData *ED_object_modifier_add(ReportList *reports, Main *bmain, Scene *scene, Object *ob, const char *name, int type) { ModifierData *md=NULL, *new_md=NULL; ModifierTypeInfo *mti = modifierType_getInfo(type); /* only geometry objects should be able to get modifiers [#25291] */ if(!ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) { BKE_reportf(reports, RPT_WARNING, "Modifiers cannot be added to Object '%s'", ob->id.name+2); return NULL; } if(mti->flags&eModifierTypeFlag_Single) { if(modifiers_findByType(ob, type)) { BKE_report(reports, RPT_WARNING, "Only one modifier of this type allowed"); return NULL; } } if(type == eModifierType_ParticleSystem) { /* don't need to worry about the new modifier's name, since that is set to the number * of particle systems which shouldn't have too many duplicates */ new_md = object_add_particle_system(scene, ob, name); } else { /* get new modifier data to add */ new_md= modifier_new(type); if(mti->flags&eModifierTypeFlag_RequiresOriginalData) { md = ob->modifiers.first; while(md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) md = md->next; BLI_insertlinkbefore(&ob->modifiers, md, new_md); } else BLI_addtail(&ob->modifiers, new_md); if(name) BLI_strncpy(new_md->name, name, sizeof(new_md->name)); /* make sure modifier data has unique name */ modifier_unique_name(&ob->modifiers, new_md); /* special cases */ if(type == eModifierType_Softbody) { if(!ob->soft) { ob->soft= sbNew(scene); ob->softflag |= OB_SB_GOAL|OB_SB_EDGES; } } else if(type == eModifierType_Collision) { if(!ob->pd) ob->pd= object_add_collision_fields(0); ob->pd->deflect= 1; DAG_scene_sort(bmain, scene); } else if(type == eModifierType_Surface) DAG_scene_sort(bmain, scene); else if(type == eModifierType_Multires) /* set totlvl from existing MDISPS layer if object already had it */ multiresModifier_set_levels_from_disps((MultiresModifierData *)new_md, ob); } DAG_id_tag_update(&ob->id, OB_RECALC_DATA); return new_md; }
/* UNUSED, keep in case we want to copy functionality for use elsewhere */ static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event) { Object *ob; Base *base; Curve *cu, *cu1; Nurb *nu; int do_scene_sort= 0; if (scene->id.lib) return; if (!(ob=OBACT)) return; if (scene->obedit) { // XXX get from context /* obedit_copymenu(); */ return; } if (event==9) { copymenu_properties(scene, v3d, ob); return; } else if (event==10) { copymenu_logicbricks(scene, v3d, ob); return; } else if (event==24) { /* moved to object_link_modifiers */ /* copymenu_modifiers(bmain, scene, v3d, ob); */ return; } for (base= FIRSTBASE; base; base= base->next) { if (base != BASACT) { if (TESTBASELIB(v3d, base)) { base->object->recalc |= OB_RECALC_OB; if (event==1) { /* loc */ copy_v3_v3(base->object->loc, ob->loc); copy_v3_v3(base->object->dloc, ob->dloc); } else if (event==2) { /* rot */ copy_v3_v3(base->object->rot, ob->rot); copy_v3_v3(base->object->drot, ob->drot); copy_qt_qt(base->object->quat, ob->quat); copy_qt_qt(base->object->dquat, ob->dquat); } else if (event==3) { /* size */ copy_v3_v3(base->object->size, ob->size); copy_v3_v3(base->object->dscale, ob->dscale); } else if (event==4) { /* drawtype */ base->object->dt= ob->dt; base->object->dtx= ob->dtx; base->object->empty_drawtype= ob->empty_drawtype; base->object->empty_drawsize= ob->empty_drawsize; } else if (event==5) { /* time offs */ base->object->sf= ob->sf; } else if (event==6) { /* dupli */ base->object->dupon= ob->dupon; base->object->dupoff= ob->dupoff; base->object->dupsta= ob->dupsta; base->object->dupend= ob->dupend; base->object->transflag &= ~OB_DUPLI; base->object->transflag |= (ob->transflag & OB_DUPLI); base->object->dup_group= ob->dup_group; if (ob->dup_group) id_lib_extern(&ob->dup_group->id); } else if (event==7) { /* mass */ base->object->mass= ob->mass; } else if (event==8) { /* damping */ base->object->damping= ob->damping; base->object->rdamping= ob->rdamping; } else if (event==11) { /* all physical attributes */ base->object->gameflag = ob->gameflag; base->object->inertia = ob->inertia; base->object->formfactor = ob->formfactor; base->object->damping= ob->damping; base->object->rdamping= ob->rdamping; base->object->min_vel= ob->min_vel; base->object->max_vel= ob->max_vel; if (ob->gameflag & OB_BOUNDS) { base->object->collision_boundtype = ob->collision_boundtype; } base->object->margin= ob->margin; base->object->bsoft= copy_bulletsoftbody(ob->bsoft); } else if (event==17) { /* tex space */ copy_texture_space(base->object, ob); } else if (event==18) { /* font settings */ if (base->object->type==ob->type) { cu= ob->data; cu1= base->object->data; cu1->spacemode= cu->spacemode; cu1->spacing= cu->spacing; cu1->linedist= cu->linedist; cu1->shear= cu->shear; cu1->fsize= cu->fsize; cu1->xof= cu->xof; cu1->yof= cu->yof; cu1->textoncurve= cu->textoncurve; cu1->wordspace= cu->wordspace; cu1->ulpos= cu->ulpos; cu1->ulheight= cu->ulheight; if (cu1->vfont) cu1->vfont->id.us--; cu1->vfont= cu->vfont; id_us_plus((ID *)cu1->vfont); if (cu1->vfontb) cu1->vfontb->id.us--; cu1->vfontb= cu->vfontb; id_us_plus((ID *)cu1->vfontb); if (cu1->vfonti) cu1->vfonti->id.us--; cu1->vfonti= cu->vfonti; id_us_plus((ID *)cu1->vfonti); if (cu1->vfontbi) cu1->vfontbi->id.us--; cu1->vfontbi= cu->vfontbi; id_us_plus((ID *)cu1->vfontbi); BKE_text_to_curve(bmain, scene, base->object, 0); /* needed? */ BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family)); base->object->recalc |= OB_RECALC_DATA; } } else if (event==19) { /* bevel settings */ if (ELEM(base->object->type, OB_CURVE, OB_FONT)) { cu= ob->data; cu1= base->object->data; cu1->bevobj= cu->bevobj; cu1->taperobj= cu->taperobj; cu1->width= cu->width; cu1->bevresol= cu->bevresol; cu1->ext1= cu->ext1; cu1->ext2= cu->ext2; base->object->recalc |= OB_RECALC_DATA; } } else if (event==25) { /* curve resolution */ if (ELEM(base->object->type, OB_CURVE, OB_FONT)) { cu= ob->data; cu1= base->object->data; cu1->resolu= cu->resolu; cu1->resolu_ren= cu->resolu_ren; nu= cu1->nurb.first; while (nu) { nu->resolu= cu1->resolu; nu= nu->next; } base->object->recalc |= OB_RECALC_DATA; } } else if (event==21) { if (base->object->type==OB_MESH) { ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf); if (md) { ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf); if (!tmd) { tmd = modifier_new(eModifierType_Subsurf); BLI_addtail(&base->object->modifiers, tmd); } modifier_copyData(md, tmd); base->object->recalc |= OB_RECALC_DATA; } } } else if (event==22) { /* Copy the constraint channels over */ copy_constraints(&base->object->constraints, &ob->constraints, TRUE); do_scene_sort= 1; } else if (event==23) { base->object->softflag= ob->softflag; if (base->object->soft) sbFree(base->object->soft); base->object->soft= copy_softbody(ob->soft); if (!modifiers_findByType(base->object, eModifierType_Softbody)) { BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody)); } } else if (event==26) { #if 0 // XXX old animation system copy_nlastrips(&base->object->nlastrips, &ob->nlastrips); #endif // XXX old animation system } else if (event==27) { /* autosmooth */ if (base->object->type==OB_MESH) { Mesh *me= ob->data; Mesh *cme= base->object->data; cme->smoothresh= me->smoothresh; if (me->flag & ME_AUTOSMOOTH) cme->flag |= ME_AUTOSMOOTH; else cme->flag &= ~ME_AUTOSMOOTH; } } else if (event==28) { /* UV orco */ if (ELEM(base->object->type, OB_CURVE, OB_SURF)) { cu= ob->data; cu1= base->object->data; if (cu->flag & CU_UV_ORCO) cu1->flag |= CU_UV_ORCO; else cu1->flag &= ~CU_UV_ORCO; } } else if (event==29) { /* protected bits */ base->object->protectflag= ob->protectflag; } else if (event==30) { /* index object */ base->object->index= ob->index; } else if (event==31) { /* object color */ copy_v4_v4(base->object->col, ob->col); } } } } if (do_scene_sort) DAG_scene_sort(bmain, scene); DAG_ids_flush_update(bmain, 0); }
/* single_psys_from is optional, if NULL all psys of ob_from are copied */ static bool copy_particle_systems_to_object(Scene *scene, Object *ob_from, ParticleSystem *single_psys_from, Object *ob_to, int space) { ModifierData *md; ParticleSystem *psys_start = NULL, *psys, *psys_from; ParticleSystem **tmp_psys; DerivedMesh *final_dm; CustomDataMask cdmask; int i, totpsys; if (ob_to->type != OB_MESH) return false; if (!ob_to->data || ((ID *)ob_to->data)->lib) return false; /* For remapping we need a valid DM. * Because the modifiers are appended at the end it's safe to use * the final DM of the object without particles. * However, when evaluating the DM all the particle modifiers must be valid, * i.e. have the psys assigned already. * To break this hen/egg problem we create all psys separately first (to collect required customdata masks), * then create the DM, then add them to the object and make the psys modifiers ... */ #define PSYS_FROM_FIRST (single_psys_from ? single_psys_from : ob_from->particlesystem.first) #define PSYS_FROM_NEXT(cur) (single_psys_from ? NULL : (cur)->next) totpsys = single_psys_from ? 1 : BLI_listbase_count(&ob_from->particlesystem); tmp_psys = MEM_mallocN(sizeof(ParticleSystem*) * totpsys, "temporary particle system array"); cdmask = 0; for (psys_from = PSYS_FROM_FIRST, i = 0; psys_from; psys_from = PSYS_FROM_NEXT(psys_from), ++i) { psys = BKE_object_copy_particlesystem(psys_from); tmp_psys[i] = psys; if (psys_start == NULL) psys_start = psys; cdmask |= psys_emitter_customdata_mask(psys); } /* to iterate source and target psys in sync, * we need to know where the newly added psys start */ psys_start = totpsys > 0 ? tmp_psys[0] : NULL; /* get the DM (psys and their modifiers have not been appended yet) */ final_dm = mesh_get_derived_final(scene, ob_to, cdmask); /* now append psys to the object and make modifiers */ for (i = 0, psys_from = PSYS_FROM_FIRST; i < totpsys; ++i, psys_from = PSYS_FROM_NEXT(psys_from)) { ParticleSystemModifierData *psmd; psys = tmp_psys[i]; /* append to the object */ BLI_addtail(&ob_to->particlesystem, psys); /* add a particle system modifier for each system */ md = modifier_new(eModifierType_ParticleSystem); psmd = (ParticleSystemModifierData *)md; /* push on top of the stack, no use trying to reproduce old stack order */ BLI_addtail(&ob_to->modifiers, md); BLI_snprintf(md->name, sizeof(md->name), "ParticleSystem %i", i); modifier_unique_name(&ob_to->modifiers, (ModifierData *)psmd); psmd->psys = psys; psmd->dm = CDDM_copy(final_dm); CDDM_calc_normals(psmd->dm); DM_ensure_tessface(psmd->dm); if (psys_from->edit) copy_particle_edit(scene, ob_to, psys, psys_from); } MEM_freeN(tmp_psys); /* note: do this after creating DM copies for all the particle system modifiers, * the remapping otherwise makes final_dm invalid! */ for (psys = psys_start, psys_from = PSYS_FROM_FIRST, i = 0; psys; psys = psys->next, psys_from = PSYS_FROM_NEXT(psys_from), ++i) { float (*from_mat)[4], (*to_mat)[4]; switch (space) { case PAR_COPY_SPACE_OBJECT: from_mat = I; to_mat = I; break; case PAR_COPY_SPACE_WORLD: from_mat = ob_from->obmat; to_mat = ob_to->obmat; break; default: /* should not happen */ from_mat = to_mat = NULL; BLI_assert(false); break; } remap_hair_emitter(scene, ob_from, psys_from, ob_to, psys, psys->edit, from_mat, to_mat, psys_from->flag & PSYS_GLOBAL_HAIR, psys->flag & PSYS_GLOBAL_HAIR); /* tag for recalc */ // psys->recalc |= PSYS_RECALC_RESET; } #undef PSYS_FROM_FIRST #undef PSYS_FROM_NEXT DAG_id_tag_update(&ob_to->id, OB_RECALC_DATA); WM_main_add_notifier(NC_OBJECT | ND_PARTICLE | NA_EDITED, ob_to); return true; }