1
$\begingroup$

I understand that I can get the paint_active_slot for the active object, and that returns a number that corresponds to the position of the texture in the slot panel and in the texture panel.

bpy.context.active_object.active_material.paint_active_slot

What I don't seem to understand is how to get the names of the textures that correspond to the list of positions. If I want to force a 2d editor popup and have it in paint mode and also have the active paint slot, I am stumped on how to use the [0], [1], etc. that is returned by getting the active paint slot to correspond to the name of the texture.

This isn't just about making a new texture through python and forcing it to be loaded in the editor, but what I mean to do is to be owrking in the 3d view on an object with one or more image texture slots and be able to select and image slot and press a button to pop up a floating 2d editor that shows that image slot so that I can make use of the differences in the 2d paint tools while still focusing on the 3d paint scene.

edit:

I tried to look at a few things.

bpy.data.materials['my_image'].paint_active_slot

gets me the index of the current active paint slot.

bpy.context.active_object.active_material.active_texture_index

gets me the index of the active texture slot. These can be active and not be the same as the active paint slot, but they do share the same sequence in index so that if you move a texture slot up or down, the paint slot moves up or down to match.

bpy.context.active_object.active_material.active_texture

gets me bpy.data.textures['Diffuse_Color'] and appears to be usable for getting the name of the texture to match to the index of the texture slot index, but only the active one.

Somehow I need to connect the index of the material texture to the active paint slot index, and pull the name from a list I don't know how to get other than one active texture at a time. Is this a typical use of the for loop then? Do I iterate through the textures making them active and getting the names, and populate somehow to a list that will match the index numbers?

edit: I worked on a version that at least does change the active texture to match the active paint slot, and in the process I noticed that the paint slot image was opening in the main uv imge editor, but I suppose my problem is getting it to work on the floating editor that was formerly the user prefs window :D

import bpy

# Call user prefs window
bpy.ops.screen.userpref_show('INVOKE_DEFAULT')

# Change area type
area = bpy.context.window_manager.windows[-1].screen.areas[0]
area.type = 'IMAGE_EDITOR'

bpy.context.space_data.mode = 'PAINT'


#get active material

activemat = bpy.context.active_object.active_material

#get active paint slot for material

indexslot = activemat.paint_active_slot

#get corresponding index for texture/make it active in texture index
activemat.active_texture_index = indexslot


#get name of active texture
texname = activemat.active_texture

#set active texture to active image in 2d editor space

for area in bpy.context.screen.areas :
    if area.type == 'IMAGE_EDITOR' :
            area.spaces.active.image == texname

Thanks for any help you might have to give.

$\endgroup$
1
$\begingroup$

Material.texture_paint_images contains all image references used for painting.

import bpy

mat = bpy.context.object.active_material

for idx, img in enumerate(mat.texture_paint_images):
    print (idx, img)
    if idx == mat.paint_active_slot:
        print ("Index:", idx)
        print ("Name:", img.name)

Even shorter (without the iteration):

>>> mat = bpy.context.object.active_material
>>> print (mat.texture_paint_images[mat.paint_active_slot].name)
'Material Diffuse Intensity'
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.