/* Temporary wrapper for driver operators for buttons to make it easier to create * such drivers by rerouting all paths through the active object instead so that * they will get picked up by the dependency system. * * < C: context pointer - for getting active data * <> ptr: RNA pointer for property's datablock. May be modified as result of path remapping. * < prop: RNA definition of property to add for * * > returns: MEM_alloc'd string representing the path to the property from the given PointerRNA */ static char *get_driver_path_hack(bContext *C, PointerRNA *ptr, PropertyRNA *prop) { ID *id = (ID *)ptr->id.data; ScrArea *sa = CTX_wm_area(C); /* get standard path which may be extended */ char *basepath = RNA_path_from_ID_to_property(ptr, prop); char *path = basepath; /* in case no remapping is needed */ /* Remapping will only be performed in the Properties Editor, as only this * restricts the subspace of options to the 'active' data (a manageable state) */ // TODO: watch out for pinned context? if ((sa) && (sa->spacetype == SPACE_BUTS)) { Object *ob = CTX_data_active_object(C); if (ob && id) { /* only id-types which can be remapped to go through objects should be considered */ switch (GS(id->name)) { case ID_TE: /* textures */ { Material *ma = give_current_material(ob, ob->actcol); Tex *tex = give_current_material_texture(ma); /* assumes: texture will only be shown if it is active material's active texture it's ok */ if ((ID *)tex == id) { char name_esc_ma[(sizeof(ma->id.name) - 2) * 2]; char name_esc_tex[(sizeof(tex->id.name) - 2) * 2]; BLI_strescape(name_esc_ma, ma->id.name + 2, sizeof(name_esc_ma)); BLI_strescape(name_esc_tex, tex->id.name + 2, sizeof(name_esc_tex)); /* create new path */ // TODO: use RNA path functions to construct step by step instead? // FIXME: maybe this isn't even needed anymore... path = BLI_sprintfN("material_slots[\"%s\"].material.texture_slots[\"%s\"].texture.%s", name_esc_ma, name_esc_tex, basepath); /* free old one */ MEM_freeN(basepath); } break; } } /* fix RNA pointer, as we've now changed the ID root by changing the paths */ if (basepath != path) { /* rebase provided pointer so that it starts from object... */ RNA_pointer_create(&ob->id, ptr->type, ptr->data, ptr); } } } /* the path should now have been corrected for use */ return path; }
Tex *give_current_object_texture(Object *ob) { Material *ma, *node_ma; Tex *tex= NULL; if(ob==NULL) return NULL; if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL; if(ob->type==OB_LAMP) { tex= give_current_lamp_texture(ob->data); } else { ma= give_current_material(ob, ob->actcol); if((node_ma=give_node_material(ma))) ma= node_ma; tex= give_current_material_texture(ma); } return tex; }
static int buttons_context_path_texture(ButsContextPath *path, ButsContextTexture *ct) { if (ct) { /* new shading system */ PointerRNA *ptr = &path->ptr[path->len - 1]; ID *id; /* if we already have a (pinned) texture, we're done */ if (RNA_struct_is_a(ptr->type, &RNA_Texture)) return 1; if (!ct->user) return 0; id = ct->user->id; if (id) { if (GS(id->name) == ID_BR) buttons_context_path_brush(path); else if (GS(id->name) == ID_MA) buttons_context_path_material(path, false, true); else if (GS(id->name) == ID_WO) buttons_context_path_world(path); else if (GS(id->name) == ID_LA) buttons_context_path_data(path, OB_LAMP); else if (GS(id->name) == ID_PA) buttons_context_path_particle(path); else if (GS(id->name) == ID_OB) buttons_context_path_object(path); else if (GS(id->name) == ID_LS) buttons_context_path_linestyle(path); } if (ct->texture) { RNA_id_pointer_create(&ct->texture->id, &path->ptr[path->len]); path->len++; } return 1; } else { /* old shading system */ Material *ma; Lamp *la; World *wo; ParticleSystem *psys; FreestyleLineStyle *ls; Tex *tex; PointerRNA *ptr = &path->ptr[path->len - 1]; /* if we already have a (pinned) texture, we're done */ if (RNA_struct_is_a(ptr->type, &RNA_Texture)) { return 1; } /* try world */ else if ((path->tex_ctx == SB_TEXC_WORLD) && buttons_context_path_world(path)) { wo = path->ptr[path->len - 1].data; if (wo && GS(wo->id.name) == ID_WO) { tex = give_current_world_texture(wo); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try particles */ else if ((path->tex_ctx == SB_TEXC_PARTICLES) && buttons_context_path_particle(path)) { if (path->ptr[path->len - 1].type == &RNA_ParticleSettings) { ParticleSettings *part = path->ptr[path->len - 1].data; tex = give_current_particle_texture(part); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } else { psys = path->ptr[path->len - 1].data; if (psys && psys->part && GS(psys->part->id.name) == ID_PA) { tex = give_current_particle_texture(psys->part); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } } /* try material */ else if ((path->tex_ctx == SB_TEXC_MATERIAL) && buttons_context_path_material(path, true, false)) { ma = path->ptr[path->len - 1].data; if (ma) { tex = give_current_material_texture(ma); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try lamp */ else if ((path->tex_ctx == SB_TEXC_LAMP) && buttons_context_path_data(path, OB_LAMP)) { la = path->ptr[path->len - 1].data; if (la) { tex = give_current_lamp_texture(la); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try linestyle */ else if ((path->tex_ctx == SB_TEXC_LINESTYLE) && buttons_context_path_linestyle(path)) { ls = path->ptr[path->len - 1].data; if (ls) { tex = give_current_linestyle_texture(ls); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } } /* no path to a texture possible */ return 0; }
static int buttons_context_path_texture(ButsContextPath *path) { Material *ma; Lamp *la; Brush *br; World *wo; ParticleSystem *psys; Tex *tex; PointerRNA *ptr= &path->ptr[path->len-1]; int orig_len = path->len; /* if we already have a (pinned) texture, we're done */ if(RNA_struct_is_a(ptr->type, &RNA_Texture)) { return 1; } /* try brush */ if((path->tex_ctx == SB_TEXC_BRUSH) && buttons_context_path_brush(path)) { br= path->ptr[path->len-1].data; if(br) { tex= give_current_brush_texture(br); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try world */ if((path->tex_ctx == SB_TEXC_WORLD) && buttons_context_path_world(path)) { wo= path->ptr[path->len-1].data; if(wo && GS(wo->id.name)==ID_WO) { tex= give_current_world_texture(wo); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try particles */ if((path->tex_ctx == SB_TEXC_PARTICLES) && buttons_context_path_particle(path)) { if(path->ptr[path->len-1].type == &RNA_ParticleSettings) { ParticleSettings *part = path->ptr[path->len-1].data; tex= give_current_particle_texture(part); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } else { psys= path->ptr[path->len-1].data; if(psys && psys->part && GS(psys->part->id.name)==ID_PA) { tex= give_current_particle_texture(psys->part); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } } /* try material */ if(buttons_context_path_material(path, 1)) { ma= path->ptr[path->len-1].data; if(ma) { tex= give_current_material_texture(ma); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try lamp */ if(buttons_context_path_data(path, OB_LAMP)) { la= path->ptr[path->len-1].data; if(la) { tex= give_current_lamp_texture(la); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* try brushes again in case of no material, lamp, etc */ path->len = orig_len; if(buttons_context_path_brush(path)) { br= path->ptr[path->len-1].data; if(br) { tex= give_current_brush_texture(br); RNA_id_pointer_create(&tex->id, &path->ptr[path->len]); path->len++; return 1; } } /* no path to a texture possible */ return 0; }