0

Can someone please help me with this simple script.

I just want to apply the material color that is in an array.

Here is what I have for now. How can I link so that my material two("colors2") looks for values $dp_matColor[3],$dp_matColor[4],$dp_matColor[5]?

It looks like I need an if statement but not sure what condition I need to add.

BTW I will have more than two materials

My final goal is to create button based on $list_of_names[]and get the background color from $dp_matColor[]. When I click, it will create my material based on below for loop by using same arrays.

Currently, I can achieve this by having a procedure for every color, but there is a lot of repeated script.

string $list_of_names[] = {"color1", "color2"};
float $dp_matColor[] = { 1.0, 0.5, 0.5,0.5, 0.5, 1.0};

for ($eachName in $list_of_names){
    shadingNode -asShader VRayMtl -n $eachName;
    setAttr ($eachName + ".color") -type double3 $dp_matColor[] $dp_matColor[] $dp_matColor[];   
} 
Andrea Rastelli
  • 617
  • 2
  • 11
  • 26
skb
  • 1
  • 1

1 Answers1

0

if you use python, you could construct your data to more easily loop through it like this:

import maya.cmds as mc

data = [('color1', (1.0, 0.5, 0.5)), ('color2', (0.5, 0.5, 1.0))]

for name, values in data:
    shader = mc.shadingNode(asShader='VRayMtl', n=name)
    mc.setAttr('%s.color' %shader, values, type='double3')

I haven't tested that to ensure it works... you may need to put the values in separately ie: values[0], values[1], values[3]. You could also use a dictionary to store the data, or create 2 lists that are in sync with each other... and on and on and on (so many options...)

But to try and do it with mel, this might work (it's been a while since I've used mel... so forgive me if it doesn't work... but the concept is there...):

string $list_of_names[] = {"color1", "color2"};
float $dp_matColor[] = { 1.0, 0.5, 0.5,0.5, 0.5, 1.0};

for (i=0;i<size($list_of_names);i++){
    shadingNode -asShader VRayMtl -n $list_of_names[i];
    int $index = i * 3;
    setAttr ($list_of_names[i] + ".color") -type double3 $dp_matColor[$index] $dp_matColor[($index+1)] $dp_matColor[($index+2)];   
} 
silent_sight
  • 492
  • 1
  • 8
  • 16
  • Thank you such, I was able to achieve the result with your help.I made couple changes.Now, I am not sure how to make the procedure run for each button.I made a different post. https://stackoverflow.com/questions/46530691/link-procedure-to-each-button-command-maya – skb Oct 02 '17 at 17:36