0
$\begingroup$

i'm working on a little script to get the right file output name automatically in the compositor, so far so good, my little script works, but i can't turn it into a custom operator to make it executable from a ui panel. Here's what i've got so far (thanks to several people in here :) )

import bpy
import os

# Get absolute render path:
filepath = bpy.context.scene.render.filepath
absolutepath = bpy.path.abspath(filepath)

# Split it
path = os.path.normpath(absolutepath)
parts = path.split(os.sep)

parentpath = os.sep.join(parts[:-2])

# Add rename operator
def rename (context):
    # set the path for all file output nodes:
    for scene in bpy.data.scenes:
        for node in scene.node_tree.nodes:


            if node.type == 'OUTPUT_FILE':
                #gets the link from the input
                link0 = node.inputs[0].links[0]


                #returns the name of the socket the link is from
                socket_name = link0.from_socket.name 

                node.base_path = parentpath
                node.label = socket_name
                node.file_slots[0].path = "\_" + "nom" + "_" + socket_name + "\_" + "nom" + "_" +  socket_name + "_"

    class RenameFO(bpy.types.Operator):
        """Tooltip"""
    bl_idname = "Rename.F_O"
    bl_label = "Rename File Outputs"

    def execute(self, context):
            rename(context)
            return {'FINISHED'}


# Create custom panel    
class FOM(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "File Output Manager"
    bl_idname = "File_output_manager"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "render"

    def draw(self, context):
        layout = self.layout

        row = layout.row()
        row.operator("Rename.F_O" , text = "Rename")

def register():
    bpy.utils.register_class(RenameFO)
    bpy.utils.register_class(FOM)


def unregister():
    bpy.utils.unregister_class(RenameFO)
    bpy.utils.unregister_class(FOM)



    register()

Does someone know what i'm doing wrong ? Thank you very much !

$\endgroup$
2
$\begingroup$

In case you haven't worked it out by now:

import bpy
import os
filepath=bpy.context.scene.render.filepath#Get absolute render path
absolutepath=bpy.path.abspath(filepath)
path=os.path.normpath(absolutepath)#Split it
parts=path.split(os.sep)
parentpath=os.sep.join(parts[:-2])
def rename(context):#Set the path for all file output nodes
    for scene in bpy.data.scenes:
        if scene.node_tree:
            for node in scene.node_tree.nodes:
                if node.type=='OUTPUT_FILE':
                    link0=node.inputs[0].links[0]#Gets the link from the input
                    socket_name=link0.from_socket.name#Returns the name of the socket the link is from
                    node.base_path=parentpath
                    node.label=socket_name
                    node.file_slots[0].path="\_"+"nom"+"_"+socket_name+"\_"+"nom"+"_"+socket_name+"_"
class FILE_OT_rename_file_outputs(bpy.types.Operator):
    """Renames file outputs"""
    bl_idname="render.rename_file_outputs"
    bl_label="Rename File Outputs"
    def execute(self,context):
        rename(context)
        return {'FINISHED'}
class FILE_PT_rename_file_outputs(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label="File Output Manager"
    bl_space_type='PROPERTIES'
    bl_region_type='WINDOW'
    bl_context="render"
    def draw(self,context):
        layout=self.layout
        row=layout.row()
        row.operator("render.rename_file_outputs",text="Rename")
classes=[FILE_OT_rename_file_outputs,FILE_PT_rename_file_outputs]
def register():
    for c in classes:bpy.utils.register_class(c)
def unregister():
    for c in classes:bpy.utils.unregister_class(c)
if __name__=="__main__":register()
| improve this answer | |
$\endgroup$
  • $\begingroup$ hey man thank you ! i already found a similar solution over the web , but thanks for the answer :) $\endgroup$ – tonton Apr 7 '17 at 15:02

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.