0
$\begingroup$
import bpy
from bpy.types import (Panel, Operator)
from bpy.utils import register_class, unregister_class
from bpy.props import (StringProperty,BoolProperty,IntProperty,FloatProperty,EnumProperty)

class RANOperator(bpy.types.Operator):
    """Replaces the Texture"""
    bl_idname = "cobject.cs1"
    bl_label = "Converter"
        
    def execute(self, context):
        for i in bpy.data.images:
            i.filepath = i.filepath.replace('.dds', '.png')
        return {'FINISHED'}

class COS(bpy.types.Panel):
    bl_label = "Replace Texture(s)"
    bl_idname = "cobject_PT_"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Tool"
    
    def draw(self, context):
        layout = self.layout 
        obj = context.object 
        row = layout.row()
        row.operator(RANOperator.bl_idname, text = "Replace", icon = "UV_SYNC_SELECT")

This is my code. And this is the resultant panel. However, whenever I click "Replace" Nothing happens

p.s. I have registered both classes. enter image description here

additionally, I want to have an input text field or Checkboxes so that I can replace hard-coded values, to input strings like .png .dds .tga

$\endgroup$
1
  • $\begingroup$ Is there a reason you are hiding how you registered them? $\endgroup$ Commented Sep 2, 2022 at 13:58

1 Answer 1

0
$\begingroup$

It is working fine. You might be mislead by the fact that it changes the path, not the name: enter image description here

You can pass strings to your operator that can be adjusted in the Adjust last operation panel:

import bpy
from bpy.types import (Panel, Operator)
from bpy.utils import register_class, unregister_class
from bpy.props import (StringProperty,BoolProperty,IntProperty,FloatProperty,EnumProperty)

class RANOperator(bpy.types.Operator):
    """Replaces the Texture"""
    bl_idname = "cobject.cs1"
    bl_label = "Converter"
    bl_options = {'REGISTER', 'UNDO'} # You need this for Adjust Last Operation panel
    extension1: StringProperty(
        name="Some extention",
        default=".dds",
        description="...",
    ) # you could add more of these
    
    def execute(self, context):
        for i in bpy.data.images:
            i.filepath = i.filepath.replace(self.extension1, '.png') 
            i.name = i.name.replace(self.extension1, '.png') 
            # Let's change the image name as well to avoid confusion
        return {'FINISHED'}

class COS(bpy.types.Panel):
    bl_label = "Replace Texture(s)"
    bl_idname = "cobject_PT_"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Tool"
    
    def draw(self, context):
        layout = self.layout 
        obj = context.object 
        row = layout.row()
        row.operator(RANOperator.bl_idname, text = "Replace", icon = "UV_SYNC_SELECT")
        
def register():
    bpy.utils.register_class(RANOperator)


if __name__ == "__main__":
    register()

And here is the Adjust Last Operation panel in the corner of 3d Viewport after the operation:

enter image description here

$\endgroup$
2
  • $\begingroup$ It is really working?! When used this code, it asked me for dds textures and not png when tried to pack resources. I will test it tomorrow because it's late night rn $\endgroup$
    – Satyam
    Commented Sep 2, 2022 at 16:38
  • $\begingroup$ Thanks It's working absolutely good. I was wondering if there is a way we can input the extension before and then click the Replace button to replace it. Like I want it to be there together with the Replace button $\endgroup$
    – Satyam
    Commented Sep 2, 2022 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .