2011-08-09 158 views
0

所以我有一個給出輸出的代碼,我需要做的是在逗號之間拉出信息,將它們分配給一個變量,當被調用時動態變化。 ..這裏是我的代碼:如何從Python中的輸出中提取特定信息

import re 

data_directory = 'Z:/Blender_Roto/' 
data_file = 'diving_board.shape4ae' 
fullpath = data_directory + data_file 


print("====init=====") 

file = open(fullpath) 
for line in file: 
current_line = line 

# massive room for optimized code here. 

# this assumes the last element of the line containing the words 
# "Units Per Second" is the number we are looking for. 
# this is a non float number, generally. 
if current_line.find("Units Per Second") != -1: 
    fps = line_split = float(current_line.split()[-1]) 
    print("Frames Per Second:", fps) 

# source dimensions 
if current_line.find("Source Width") != -1: 
    source_width = line_split = int(current_line.split()[-1]) 
    print("Source Width:", source_width) 

if current_line.find("Source Height") != -1: 
    source_height = line_split = int(current_line.split()[-1]) 
    print("Source Height:", source_height) 

# aspect ratios 
if current_line.find("Source Pixel Aspect Ratio") != -1: 
    source_px_aspect = line_split = int(current_line.split()[-1]) 
    print("Source Pixel Aspect Ratio:", source_px_aspect) 

if current_line.find("Comp Pixel Aspect Ratio") != -1: 
    comp_aspect = line_split = int(current_line.split()[-1]) 
    print("Comp Pixel Aspect Ratio:", comp_aspect) 


# assumption, ae file can contain multiple mocha shapes. 
# without knowing the exact format i will limit the script 
# to deal with one mocha shape being animated N frames. 

# this gathers the shape details, and frame number but does not 
# include error checking yet. 
if current_line.find("XSpline") != -1: 

    # record the frame number. 

    frame = re.search("\s*(\d*)\s*XSpline", current_line) 
    if frame.group(1) != None: 
     frame = frame.group(1) 
     print("frame:", frame) 


    # pick part the part of the line that deals with geometry 
    match = re.search("XSpline\((.+)\)\n", current_line) 

    line_to_strip = match.group(1) 
    points = re.findall('(\(.*?\))', line_to_strip) 

    print(len(points)) 
    for point in points: 
     print(point) 
    print("="*40) 

file.close() 

這使我的輸出:

====init===== 
Frames Per Second: 24.0 
Source Width: 2048 
Source Height: 778 
Source Pixel Aspect Ratio: 1 
Comp Pixel Aspect Ratio: 1 
frame: 20 
5 
(0.793803,0.136326,0,0.5,0) 
(0.772345,0.642332,0,0.5,0) 
(0.6436,0.597615,0,0.5,0) 
(0.70082,0.143387,0,0.5,0.25) 
(0.70082,0.112791,0,0.5,0) 
======================================== 

所以我需要的例子是能夠分配(0.793803,0.136326,0,0.5,0)到(1x,1y,1z,1w,1s),(0.772345,0.642332,0,0.5,0)到(2x,2y,2z,2w,2s)等等,這樣無論填入那些p他們將承擔這個價值。

這裏是我需要把這些數字爲代碼:

#------------------------------------------------------------------------------- 
# Name:  Mocha Rotoscoping Via Blender 
# Purpose:  Make rotoscoping more efficient 
# 
# Author:  Jeff Owens 
# 
# Created:  11/07/2011 
# Copyright: (c) jeff.owens 2011 
# Licence:  Grasshorse 
#------------------------------------------------------------------------------- 
#!/usr/bin/env python 

import sys 
import os 
import parser 
sys.path.append('Z:\_protomotion\Prog\HelperScripts') 
import GetDir 
sys.path.append('Z:\_tutorials\01\tut01_001\prod\Blender_Test') 
filename = 'diving_board.shape4ae' 
infile = 'Z:\_tutorials\01\tut01_001\prod\Blender_Test' 
import bpy 
from mathutils import Vector 

#below are taken from mocha export 
x_width =2048 
y_height = 778 
z_depth = 0 
frame = 20 

def readText(): 
text_file = open('diving_board.shape4ae', 'r') 
lines = text_file.readlines() 
print (lines) 
print (len.lines) 
for line in lines: 
    print (line) 

##sets points final x,y,z value taken from mocha export for blender interface 

point1x = (0.642706 * x_width) 
point1y = (0.597615 * y_height) 
point1z = (0 * z_depth) 

point2x = (0.770557 * x_width) 
point2y = (0.647039 * y_height) 
point2z = (0 * z_depth) 

point3x = (0.794697 * x_width) 
point3y = (0.0869024 * y_height) 
point3z = (0 * z_depth) 


point4x = (0.707973* x_width) 
point4y = (0.0751348 * y_height) 
point4z = (0 * z_depth) 


w = 1 # weight 
listOfVectors = [Vector((point1x,point1y,point1z)),Vector((point2x,point2y,point2z)),Vector((point3x,point3 y,point3z)),Vector((point4x,point4y,point4z)), Vector((point1x,point1y,point1z))] 

def MakePolyLine(objname, curvename, cList): 
curvedata = bpy.data.curves.new(name=curvename, type='CURVE') 
curvedata.dimensions = '3D' 

objectdata = bpy.data.objects.new(objname, curvedata) 
objectdata.location = (0,0,0) #object origin 
bpy.context.scene.objects.link(objectdata) 

polyline = curvedata.splines.new('POLY') 
polyline.points.add(len(cList)-1) 
for num in range(len(cList)): 
    x, y, z = cList[num] 
    polyline.points[num].co = (x, y, z, w) 

MakePolyLine("NameOfMyCurveObject", "NameOfMyCurve", listOfVectors) 

那麼,我有我的載體,我想能夠把(PX,PY,0.z,PW,PS)然後(p2.x,p2.y,p2.zp2.wp2.s)等,以便它可以改變每個給定的數字

任何幫助將是偉大的..先謝謝你!

-Jeff

+0

解析Python中的文本其實很簡單,但你必須自己想出一個方法。從頭開始,我會在新行上分割字符串(使用[str.split](http://docs.python.org/library/stdtypes.html#str.split))來解決這個問題,然後解析每條線分別使用組合str.split和簡單檢查。只要文本的格式沒有改變,你就可以爲這個特定的情況編寫解析器。 –

回答

3

而不是印刷每個輸出,您可以創建點對象和索引它們的名稱。例如:

>>> class Point: 
...  def __init__(self, t): 
...   (self.x,self.y,self.z,self.w,self.s) = t 
... 
>>> p = Point((3,4,5,3,1)) 
>>> p.w 
3 

您可以在陣列中放置這些點對象,然後通過

myPoints[3].x 

附錄

訪問組件若拉是你重要從一個數組中取點,而是用實際變量名,你可以做到以下幾點,其中points是你的陣列元組的:

(p0x,p0y,p0z,p0w,p0s) = points[0] 
(p1x,p1y,p1z,p1w,p1s) = points[1] 
(p2x,p2y,p2z,p2w,p2s) = points[2] 
... 

等。

不過要考慮這是否是一種合適的方法。有一個積分類可以讓你有任意數量的積分。使用已定義的變量名稱,即時創建無限數量的這些東西是可能的,但幾乎總是一個壞主意。這裏有一個警告:http://mail.python.org/pipermail/tutor/2005-January/035232.html

當你有一個點對象的數組,你做你想要的更好!例如,您可以執行以下操作:

myPoints[i].y = 12 

從而更改第i個點的y座標。當你修改變量名時,這幾乎是不可能的。希望有所幫助! (並希望我明白你的澄清!讓我知道如果不是....)

+0

太棒了,非常感謝你,應該做的伎倆..我會讓你知道,雖然 – Jeff

+0

我應該也在我的問題中注意到我可以在這裏找到的其他代碼:http:// www。 pasteall.org/23850/python,所以我的目標是獲取從該文件給出的數據點,並能夠動態地將它們命名爲向量,以便每幀更改 – Jeff

+0

我將更新答案 –

0

如果我在讀你的代碼的權利,相關部分是在產生的元組結束循環。

data = [] 
for point in points: 
    data.append(point)  
    print(point) 

這將創建一個新列表並將每個元組添加到列表中。因此,data[0]擁有(0.793803,0.136326,0,0.5,0)data[0][0]擁有0.793803

+0

數據[0] [0]保持值0,數據[0] [1]成立。 ,數據[0] [2]成立7等 – Jeff

+0

我應該在我的問題中注意到我可以在這裏找到的其他代碼:pasteall.org/23850/python,所以我的目標是獲取給定的數據點從那個文件,並能夠動態地命名爲矢量,所以它改變每一幀 – Jeff

+0

如果點是元組,則不應該發生。他們是字符串嗎?如果是這樣,那麼追加步驟將需要某種類型的解析。 –

相關問題