PDA

View Full Version : Python File Handling


jsells20
11-02-2010, 05:17 PM
Hello all. I have a python script I had done for an exercise a while back that I need help with. I got up to the point of taking a text file and doing an output in the script editor but as far as graphing it in Maya using nurbs curves I'm not sure how to go about it.

I heard it has something to do with longitudinal and latitudinal ranges or something like that but I never have seen a code example to go by. I don't mind posting the script and the .txt file but I would be grateful if someone could help me since this has been on my to do list for a while.

Thanks in advance,
Jon

import maya.cmds as mc

def movieGenre():

#import re

defaultDirectory = mc.workspace(query = True, rootDirectory = True)

if mc.optionVar(exists = "dialogDefaultDirectory"):
defaultDirectory = mc.optionVar(query = "dialogDefaultDirectory")

userFile = mc.fileDialog(mode = 0, directoryMask = ("%s/*.txt" % defaultDirectory))

if not len(userFile):
print "# No file selected."
return

print "# The user selected %s" % userFile

mc.optionVar(stringValue = ("dialogDefaultDirectory",
userFile[0:userFile.rfind("/")]) )

file = open(userFile, "r")

#pattern = re.compile('-*\d+\.\d+')

points = []
curves = []
genres = []
genreCounts = []

# converts data to set up for curves.
lineCount = 0
for line in file:
lineCount = lineCount + 1
line = line.strip()
lineSplit = line.split()
if len(line):
if lineCount == 1:
genres = line.split()[1:]
for genre in genres:
genreCounts.append([])
else:
i = 0
for count in lineSplit[1:]:
genreCounts[i].append(float(count))
i = i + 1

# make curves from the data stored in genreCounts



# make the curve, record its name and prepare for another curve
curve = mc.curve(name = "curveData", degree = 3, point = points)
curves.append(curve)
points = []

# curve = mc.curve(name = "curveData", degree = 1, point = points)
# curves.append(curve)

print "# Curves made from %s:" % (userFile[userFile.rfind("/") + 1:])
for curve in curves:
print "# %s" % curve

file.close()

NaughtyNathan
11-02-2010, 05:30 PM
I'm not entirely sure where you are struggling or stuck at Jon, but basically when you create a nurbs curve you give the command a list of points. each point is the x,y,z position you want the control vertex at. So, in your example I assume you want to create a series of curves using an X value that is the year(offset), a Y value that is the value from the genre column and a Z value of 0. (or whatever other way round you want)

The trick would probably be to read the whole text file in FIRST and build a dictionary or list of lists. It would then be far easier to generate your curves one by one from this data, rather than try to create them all simultaneously on the fly as you parse the file.

:nathaN

CGTalk Moderation
11-02-2010, 05:30 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.