Coder Social home page Coder Social logo

Comments (2)

theoldben avatar theoldben commented on June 11, 2024

Could be tricky. Those lists are hard to access. I'll see what I can do.

from blendernormalgroups.

Gotfind avatar Gotfind commented on June 11, 2024

Recently I took this script and modified it for my personal needs and with it I added the UV preservation. Keep in mind I am not an expert programmer and I made it so it uses my GroupNode I have in the .blend files already created just so it doesn't create a new one. That GroupNode is basically what you have but instead of having a UV node in itself, it take it from Group Input. I made it few days back so I don't even remember what exactly I added or removed in the script but I think I modified mainly the get_custom and set_custom. Anyway I am just gonna paste here the whole Operator Class in case you might want to get inspired by it or something.

class GOT_OT_OptimizedNormalMaps(Operator):
    bl_idname = 'got.optimizednormalmaps'
    bl_label = "Normal Map nodes to Custom"
    bl_options = {'UNDO'}
    bl_description = "Switch normal map nodes to a faster custom node"

    @classmethod
    def poll(self, context):
        return (bpy.data.materials or bpy.data.node_groups)

    def execute(self, context):
        def mirror(new, old):
            # Copy attributes of the old node to the new node
            new.parent = old.parent
            new.label = old.label
            new.mute = old.mute
            new.hide = old.hide
            new.select = old.select
            new.location = old.location
            
            # Inputs
            for (name, point) in old.inputs.items():
                input = new.inputs.get(name)
                if input:
                    input.default_value = point.default_value
                    for link in point.links:
                        new.id_data.links.new(link.from_socket, input)

            # Outputs
            for (name, point) in old.outputs.items():
                output = new.outputs.get(name)
                if output:
                    output.default_value = point.default_value
                    for link in point.links:
                        new.id_data.links.new(output, link.to_socket)

        def get_custom():
            name = 'Normal Map Optimized'
            group = bpy.data.node_groups.get(name)
            
            return group

        def set_custom(nodes):
            group = get_custom()
            if not group:
                return

            for node in nodes:
                new = None
                toGroup = False
                if self.custom:# Setting up the GROUP NormalMap GroupNode
                    if isinstance(node, bpy.types.ShaderNodeNormalMap):
                        toGroup = True
                        new = nodes.new(type='ShaderNodeGroup')
                        new.node_tree = group
                else:# Setting up the NORMAL NormalMap Node
                    if isinstance(node, bpy.types.ShaderNodeGroup):
                        if node.node_tree == group:
                            new = nodes.new(type='ShaderNodeNormalMap')

                if new:
                    name = node.name
                    mirror(new, node)
                    
                    if toGroup:# Setting up the GROUP NormalMap GroupNode
                        uvNode = nodes.new('ShaderNodeUVMap')
                        uvNode.uv_map = node.uv_map
                        uvNode.name = node.name+" -UV-"
                        uvNode.label = ''
                        uvNode.parent = new.parent
                        uvNode.label = node.label+" -UV-"
                        uvNode.mute = new.mute
                        uvNode.hide = new.hide
                        uvNode.select = new.select
                        uvNode.color = Color((0.6079999804496765, 0.6079999804496765, 0.6079999804496765))
                        uvNode.location = Vector((new.location.x-216.0011, new.location.y-9.06744))
                        uvNode.id_data.links.new(uvNode.outputs['UV'], new.inputs[0])
                    else:# Setting up the NORMAL NormalMap Node
                        uvNode = nodes[node.name+" -UV-"]
                        new.uv_map = uvNode.uv_map
                        nodes.remove(uvNode)
                    
                    nodes.remove(node)
                    new.name = name

        for mat in bpy.data.materials:
            set_custom(getattr(mat.node_tree, 'nodes', []))
        for group in bpy.data.node_groups:
            set_custom(group.nodes)
        
        # Finish
        return {'FINISHED'}
    custom: bpy.props.BoolProperty(
        name="To Custom",
        description="Set all normals to custom group, or revert back to normal",
        default=True,
    )

from blendernormalgroups.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.