6
$\begingroup$

Changing texture data properties via python seem impossible because the properties are inherited from the texture Type

for example texture.noise_scale don't exists yet if texture.type is currently "IMAGE" because texture.noise_scale is a child property of some subclass types

so if you are starting from an image type the following code won't be able to work

texture = bpy.data.textures["MyImgTexture"]
texture.type = "MARBLE" #changing type
texture.noise_scale = 5.5 #changing noise scale from this new type 

What can we do to refresh the child properties? bpy.context.view_layer.update() won't help here

$\endgroup$
7
$\begingroup$

Recast the type

For objects that can have their types changed can recast the types using type_recast

>>> tex.type_recast(
type_recast()
.. method:: type_recast()
Return a new instance, this is needed because types
such as textures can be changed at runtime.
:return: a new instance of this object with the type initialized again.
:rtype: subclass of :class:`bpy.types.bpy_struct`

try it out in console.

>>> tex = D.textures.new("Tex", 'STUCCI')
>>> tex.name
'Tex'

>>> tex.type
'STUCCI'

>>> tex.image
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'StucciTexture' object has no attribute 'image'

>>> tex.type = 'IMAGE'
>>> foo = tex.type_recast()
>>> foo.image is None
True

>>> foo.name
'Tex'

>>> foo
bpy.data.textures['Tex']

Notice too the two instances keep there type

>>> str(tex)
'<bpy_struct, StucciTexture("Tex") at 0x7f89ff16b448>'

>>> str(foo)
'<bpy_struct, ImageTexture("Tex") at 0x7f89ff16b448>'

btw, if while checking this out in console the properties area is open on same screen, it is being done automatically for us.

$\endgroup$
3
$\begingroup$

It's a bit unintuitive but refetching the Texture after changing the type works for me :

import bpy

texture = bpy.data.textures["Texture"]
texture.type = "MARBLE" #changing type
texture = bpy.data.textures["Texture"] # This one has noise_scale attribute
texture.noise_scale = 2  #changing noise scale from this new type 
$\endgroup$
1
  • 1
    $\begingroup$ speculate this is what is happening too if running py console code with properties editor open in same screen, when it sets context.texture making the prop behave as tho it has an update method when changed. $\endgroup$ – batFINGER Mar 22 at 14:15

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.