Exemplo n.º 1
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;
}
static void texture_get_from_context(const bContext *C, bNodeTreeType *UNUSED(treetype), bNodeTree **r_ntree, ID **r_id, ID **r_from)
{
	SpaceNode *snode = CTX_wm_space_node(C);
	Scene *scene = CTX_data_scene(C);
	Object *ob = OBACT;
	Tex *tx = NULL;

	if (snode->texfrom == SNODE_TEX_OBJECT) {
		if (ob) {
			tx = give_current_object_texture(ob);
			if (tx) {
				if (ob->type == OB_LAMP)
					*r_from = (ID *)ob->data;
				else
					*r_from = (ID *)give_current_material(ob, ob->actcol);
				
				/* from is not set fully for material nodes, should be ID + Node then */
				*r_id = &tx->id;
				*r_ntree = tx->nodetree;
			}
		}
	}
	else if (snode->texfrom == SNODE_TEX_WORLD) {
		if (scene->world) {
			*r_from = (ID *)scene->world;
			tx = give_current_world_texture(scene->world);
			if (tx) {
				*r_id = &tx->id;
				*r_ntree = tx->nodetree;
			}
		}
	}
	else if (snode->texfrom == SNODE_TEX_BRUSH) {
		struct Brush *brush = NULL;
		
		if (ob && (ob->mode & OB_MODE_SCULPT))
			brush = BKE_paint_brush(&scene->toolsettings->sculpt->paint);
		else
			brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);

		if (brush) {
			*r_from = (ID *)brush;
			tx = give_current_brush_texture(brush);
			if (tx) {
				*r_id = &tx->id;
				*r_ntree = tx->nodetree;
			}
		}
	}
	else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
		FreestyleLineStyle *linestyle = BKE_linestyle_active_from_scene(scene);
		if (linestyle) {
			*r_from = (ID *)linestyle;
			tx = give_current_linestyle_texture(linestyle);
			if (tx) {
				*r_id = &tx->id;
				*r_ntree = tx->nodetree;
			}
		}
	}
}