Input float numbers from CSV to Maya Hypershade utility via script

Input float numbers from CSV to Maya Hypershade utility via script

Anonymous
Not applicable
1,420 Views
6 Replies
Message 1 of 7

Input float numbers from CSV to Maya Hypershade utility via script

Anonymous
Not applicable

Hi,

 

my problem is that i want to take a float number from csv file. (i.e 40,02) This number i want to link it with hypershade floatConstant. In floatConstant utility exist a box called "float". in This box i want to put the number(s) from csv.

 

thank you in advance !

0 Likes
Accepted solutions (1)
1,421 Views
6 Replies
Replies (6)
Message 2 of 7

haggi_master
Advocate
Advocate

Could you give us a little bit more informations? Only reading a number from a csv and placing it into an attribute seems to be not really useful. The first thing I would recommend is to simply copy and paste the value, but I suppose that's not what you want.

 

Which language do you need? Do you really only need to read exactly one value? If so, you need to know the layout of the csv etc. Do you want to solve the problem yourself or do you simply need someone who does the work for you?

0 Likes
Message 3 of 7

Anonymous
Not applicable

First of all I want to do it for one value and after I will automate the input of many values. 

 

The script can be in Python or Mel. For example i found how i can read the csv file in MEl. I post the code :

 

string $fileName = "c:/temp/test.csv";
int $fileId = fopen $fileName "r";

 

Now i want to put one value at first in the "float" box in floatConstant utility in maya hypershade.

 

This script in python maybe is very simple, but my knowledge is python  is not so good yet.

0 Likes
Message 4 of 7

haggi_master
Advocate
Advocate

I recommend python in this case because you have to split your read lines into chunks what is simpler in python.

 

e.g. reading a csv file and splitting it into single values works like this:

 

fh = open("c:/daten/info.csv", "r")
content = fh.readlines()
fh.close()
for line in content:
    l = line.strip()
    values = l.split(",")

So you will need to know in which line and in which row your value can be found. If you know it, you can retrieve the value. e.g. let's say the needed value is in row 2 column 3 then can can do:

fh = open("c:/daten/info.csv", "r")
content = fh.readlines()
fh.close()
l = lines[2].strip()
value = l.split(",")[3]

Once you have the desired value, you can assign it to your node. Let's say your node is named "MyConstFloat" then you can try this:

import pymel.core as pm

fh = open("c:/daten/info.csv", "r")
content = fh.readlines()
fh.close()
l = lines[2].strip()
value = l.split(",")[3]

constFloatNode = pm.PyNode("MyConstFloat")
constFloatNode.inFloat.set(float(value))
0 Likes
Message 5 of 7

Anonymous
Not applicable

Thank you very muck at first.

 

Maya gives me back that the "lines" are not defined. I try to define in as an array that it's not the solution!

 

if i have a value i.e in first line in first row i have to put like this?

 

import pymel.core as pm

fh = open("C:/temp/test.csv", "r")
content = fh.readlines()
fh.close()
l = lines[1].strip() //change this
value = l.split(",")[1] //change this

constFloatNode = pm.PyNode("floatConstant1") // i put the name of my node
constFloatNode.inFloat.set(float(value))

0 Likes
Message 6 of 7

haggi_master
Advocate
Advocate
Accepted solution

Uppsss sorry, my fault. It has to be content, not lines. The indices are zero based, so if you want the very first entry in the very first row, it is 0,0.

 

import pymel.core as pm

fh = open("C:/temp/test.csv", "r")
content = fh.readlines()
fh.close()
l = content[1].strip() //change this
value = l.split(",")[1] //change this

constFloatNode = pm.PyNode("floatConstant1") // i put the name of my node
constFloatNode.inFloat.set(float(value))
0 Likes
Message 7 of 7

Anonymous
Not applicable

Thank you very much for your front responese.

it works!!

0 Likes