# How to create an operator for the mapping node (scale)

**URL:** <https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969>\
**Category:** Python Support\
**Created:** [January 22, 2021, 4:27pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969 "2021-01-22T16:27:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon72338821](https://blenderartists.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@anon72338821](https://blenderartists.org/u/anon72338821)\
**Post date:** [January 22, 2021, 4:27pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/1 "2021-01-22T16:27:23Z")

</div>

![Scale](https://blenderartists.org/uploads/default/original/4X/7/e/8/7e8da9a2dd210bb1e48efd7800a756dd1d5cf1ae.jpeg)  
I made an addon (well, just two buttons for now) that adds and set nodes for tri-planar mapping textures. This, since Quixel Bridge drops only a bunch of textures and connect them to the principled BSDF. Got that, and works. So there is a button: add box mapping. Yay! But now I want the users to be able to adjust the scale (mapping node) in the UI.  
`The data path is: node_tree.nodes["Mapping"].inputs[3].default_value`

So I guess I have to use, in the Operator Class, something with:  
`bpy.context.object.active_material.node_tree.nodes["Mapping"].inputs[3].default_value`

And probably I need to set up a FloatVector property in the operator class so I get the scale x,y,z together in the UI?.

So far I got only this:

```auto
class QXH_OT_scaletex(bpy.types.Operator): 
    bl_idname ="qhx.scaletex"
    bl_label = "Scale Texture"
    bl_description = "Scale the texture"
    
    @classmethod
    def poll(cls, context):
        return context.active_object is not None
    
    def execute(self, context):
        # Make some references to material and it's nodes
        activemat = context.object.active_material
        treenode = activemat.node_tree.nodes
        
        #scale the texture
        # treenode["Mapping"].inputs[3].default_value
        # ............
        
        return {FINISHED}

```

---

<div class="post-metadata">

**Author:** ![Blutag](https://blenderartists.org/user_avatar/blenderartists.org/blutag/32/740870_2.png) [@Blutag](https://blenderartists.org/u/Blutag)\
**Post date:** [January 22, 2021, 5:17pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/2 "2021-01-22T17:17:36Z")

</div>

Try this:

```auto
bpy.context.object.active_material.node_tree.nodes["Mapping"].inputs["Scale"].default_value = (0.5,0.5,0.5)

```

---

<div class="post-metadata">

**Author:** ![anon72338821](https://blenderartists.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@anon72338821](https://blenderartists.org/u/anon72338821)\
**Post date:** [January 22, 2021, 5:33pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/3 "2021-01-22T17:33:53Z")

</div>

It works, but gives a button and sets the scaling to 0.5:  
 ![image](https://blenderartists.org/uploads/default/original/4X/4/0/9/4098084de37525eb486351b8e753fa53590e64fd.png)  
I’ll do some tests, and see if I can sliders

Update: not sure where I am heading to, so far I got:

```auto

class QXH_OT_scaletex(bpy.types.Operator): 
    bl_idname ="qxh.scaletex"
    bl_label = "Scale Texture"
    bl_description = "Scale the texture"
    
    #create properties
    scale_prop : bpy.props.FloatVectorProperty(
        subtype="XYZ", 
        min=0.0, 
        max=1.0,
        size=4) # size? 
    
    @classmethod
    def poll(cls, context):
        return context.active_object is not None # todo: and if material has a mapping node
    
    def execute(self, context):
        # Make some references to material and it's nodes
        activemat = context.object.active_material
        treenode = activemat.node_tree.nodes
        
        #scale the texture
        # treenode["Mapping"].inputs["Scale"].default_value = (0.5,0.5,0.5) ...
        # ... that gave a button in the UI panel, and set the mapping to 0.5 
        # ....checking on how to get sliders instead
    
    def draw(self, context):
        row.prop(self, "scale_prop", text="Scale Texture")
        
        return {FINISHED}

```

---

<div class="post-metadata">

**Author:** ![Blutag](https://blenderartists.org/user_avatar/blenderartists.org/blutag/32/740870_2.png) [@Blutag](https://blenderartists.org/u/Blutag)\
**Post date:** [January 22, 2021, 6:16pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/4 "2021-01-22T18:16:15Z")

</div>

Sliders are cool, but you should use a textbox instead, it’s much more practical.

---

<div class="post-metadata">

**Author:** ![anon72338821](https://blenderartists.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@anon72338821](https://blenderartists.org/u/anon72338821)\
**Post date:** [January 22, 2021, 6:44pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/5 "2021-01-22T18:44:24Z")

</div>

Textbox looks to cumbersome to me; you think 0.2 is nice, press ok. No, 0.3 maybe. No, 0.23… Ahw yes, 0.236. So that is typing 4 times, and 4 times confirming? Why you favour texbox for scaling a texture?

---

<div class="post-metadata">

**Author:** ![pvn31](https://blenderartists.org/user_avatar/blenderartists.org/pvn31/32/732716_2.png) [@pvn31](https://blenderartists.org/u/pvn31)\
**Post date:** [January 22, 2021, 7:28pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/6 "2021-01-22T19:28:01Z")

</div>

```auto
def invoke(self,context,event):
        return context.window_manager.invoke_props_dialog(self)

```

You need this. Draw function is used to customize how this popup/dialog\_box would look like. It is not necessary.

---

<div class="post-metadata">

**Author:** ![Blutag](https://blenderartists.org/user_avatar/blenderartists.org/blutag/32/740870_2.png) [@Blutag](https://blenderartists.org/u/Blutag)\
**Post date:** [January 22, 2021, 7:33pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/7 "2021-01-22T19:33:10Z")

</div>

What if you want 0.24 but the slider jumps from 0.23 to 0.25? Or if you want a scale of 100 but the slider only allows [-5, +5]? Besides numeric textboxes can also slide if you click and drag the number.

---

<div class="post-metadata">

**Author:** ![pvn31](https://blenderartists.org/user_avatar/blenderartists.org/pvn31/32/732716_2.png) [@pvn31](https://blenderartists.org/u/pvn31)\
**Post date:** [January 22, 2021, 7:35pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/8 "2021-01-22T19:35:08Z")

</div>

Also if you want slider you can use `slider = True` for property. But i suggest to define min and max if you are going to use sliders.

```auto
row.(context.object, "prop", slider=True)

```

---

<div class="post-metadata">

**Author:** ![anon72338821](https://blenderartists.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@anon72338821](https://blenderartists.org/u/anon72338821)\
**Post date:** [January 22, 2021, 7:37pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/9 "2021-01-22T19:37:00Z")

</div>

I got it to work with a function as described here: [How to create panel for setting operator properties? - #2 by Shawn\_Irwin](https://blenderartists.org/t/how-to-create-panel-for-setting-operator-properties/1279812/2) by @Shawn_Irwin  
And then you need to register the function.  
The function is like this:

```auto
def do_update( self, context ):
    if context.active_object:
        context.active_object.location.x = self.someValue
    print( 'update', self.someValue )

```

And registration like this:

```auto
def register():
    bpy.types.Scene.someValue = bpy.props.FloatProperty(name = "Float", 
        description = "Enter a float", min = -100, max = 100, update=do_update )
    bpy.utils.register_class(SomePanel)

def unregister():
    bpy.utils.unregister_class(SomePanel)
    del bpy.types.Scene.someValue

```

And in the panel like this:

```auto
    def draw(self, context):
        layout = self.layout
        scn = context.scene
        layout.prop( scn, 'someValue' )

```

The only thing , the values show up in my panel like this:  
 ![vectorScale](https://blenderartists.org/uploads/default/original/4X/1/c/e/1ce6a2d211cc382c822ffb9e19112d066bc8dbbd.jpeg)

The only thing is I need to have those vertical so the user can adjust the 3 values in one go.

---

<div class="post-metadata">

**Author:** ![pvn31](https://blenderartists.org/user_avatar/blenderartists.org/pvn31/32/732716_2.png) [@pvn31](https://blenderartists.org/u/pvn31)\
**Post date:** [January 22, 2021, 7:38pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/10 "2021-01-22T19:38:35Z")

</div>

> [@anon72338821](#):
>
> The only thing is I need to have those vertical so the user can adjust the 3 values in one go

Use `bpy.props.FloatVectorProperty`

---

<div class="post-metadata">

**Author:** ![anon72338821](https://blenderartists.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@anon72338821](https://blenderartists.org/u/anon72338821)\
**Post date:** [January 22, 2021, 7:39pm UTC](https://blenderartists.org/t/how-to-create-an-operator-for-the-mapping-node-scale/1279969/11 "2021-01-22T19:39:00Z")

</div>

Ah, I didn’t know you can slide numbers in texboxes. I know a lot already, but there is way more to learn still for me. 🙃
