0
$\begingroup$

I would like to use an Empty object as a Driver (Location, Rotation, Scale) to drive a "Mapping Node" or any other Shader nodes using Scripting. I know how to do this manually with drivers, but i want the drivers to be added with Scripting.

So to be clear, the idea is that when you click on "Run Script": drivers need to be added automatically to location, rotation, scale of the Mapping node in Shader Editor. And the Driver have to be an empty or any other Object.

$\endgroup$
1
  • $\begingroup$ Hello and welcome. Please don't write in all caps in the title or body of any posts, it is the written equivalent of shouting, is harder to read and may be considered rude. Please use the edit button below the post to change your text into regular case. $\endgroup$ Commented Nov 2 at 0:15

1 Answer 1

0
$\begingroup$

Although i'd recommend using drivers for that, transfering values can also easily be done through scripts - the important part is correctly identifying the node to access.

#read values from the empty
source_empty_name = "SourceEmpty"
source_empty = bpy.data.objects[source_empty_name]

location = source_empty.location
rotation = source_empty.rotation_euler  
scale = source_empty.scale  

# identify the mapping node of your material
target_material_name = "DynamicMapping"
target_material = bpy.data.materials.get(target_material_name)
    
target_node_name = "MappingPython"
target_node_type = "MAPPING"

for node in target_material.node_tree.nodes:
    if node.type == target_node_type and node.label == target_node_name:
        # set values
        node.inputs["Location"].default_value = location
        node.inputs["Rotation"].default_value = rotation
        node.inputs["Scale"].default_value = scale

This requires assigning the right names to your empty, the material, and the mapping node.

a mapping node driven by the empty transform The downside of using a script is that you have to manually trigger it if you want the node to update. There are ways to trigger it on updates, but this is not a "clean" way.

$\endgroup$
5
  • $\begingroup$ Hi Ismir Egal, thank you so much for helping. It seems to work for you, so i must did something wrong. I named the Empty: "SourceEmpty". The material name: "DynamicMapping". The mapping node: "MappingPython" and the mapping node label: "MappingPython". Please tell me what i'm doing wrong. I will update my question with a picture of what i have done. $\endgroup$
    – Paul Twinz
    Commented Nov 4 at 18:58
  • $\begingroup$ Also, when i a run the script, there was no drivers added to the location, rotation, scale of the Mapping node(MappingPython). $\endgroup$
    – Paul Twinz
    Commented Nov 4 at 19:37
  • $\begingroup$ Finally your Script works, but it doesn't update when i move the empty. I was expecting drivers to be added automatically in the mapping node in location, rotation, scale. But i learned something new from your script that i didn't know was possible. Do you know how to add drivers to the mapping node using an empty as a driver? The reason why it is so important to what i'm trying to do is simply because in Shader Editor there's no node that can do what "Object info" in Geometry node can do; meaning bring location, rotation, scale of any object to the Shader Editor. Thank you again. $\endgroup$
    – Paul Twinz
    Commented Nov 4 at 21:40
  • $\begingroup$ What you describe is the right way to do it: You can just right-click the fields inside the mapping node and hit 'add driver'. You need to set the 'type' field to match the field you're editing, then you can use the eyedropper to select the empty as a source. This has to be done for all 9 fields, but gives much better results than using a script: tiny example file $\endgroup$
    – Ismir Egal
    Commented Nov 7 at 3:48
  • $\begingroup$ Thank you Ismir Egal for your answer. $\endgroup$
    – Paul Twinz
    Commented Nov 15 at 23:03

You must log in to answer this question.

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