2011-08-11 29 views
0

這裏是我的代碼:如何單獨的輸出數據

#------------------------------------------------------------------------------- 
# 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:/Blender_Roto') 
filename = 'diving_board.shape4ae' 
infile = 'Z:/Blender_Roto/' 
#import bpy 
#from mathutils import Vector 

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

import re 

data_directory = 'Z:/Blender_Roto/' # windows 
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) 

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,等等等等

附錄

所以我落得這樣做增加這個

(point1, point2, point3, point4, point5) = points 
print (point1) 
#print (point2) 
#print (point3) 
#print (point4) 
#print (point5) 

這產生:

(0.793803,0.136326,0,0.5,0) 

但是,當我試圖再次解析和我寫

(x,y,z,w,s) = p 

它想出的誤差P沒有定義

所以然後我試圖 (X,Y,Z,W,S)=點1

其產生的誤差值過多到解壓。

我想另一件事是

for point1 in points 
    p1x = (x,) 
print (p1x) 

剛剛取得第一(而不是整個X值...

任何解決方案?

附錄PT 2

那麼會發生什麼呢:

====init===== 
Frames Per Second: 24.0 
Source Width: 2048 
Source Height: 778 
Source Pixel Aspect Ratio: 1 
Comp Pixel Aspect Ratio: 1 
5 
frame: 20 
(0.793803,0.136326,0,0.5,0) 
x: (, y: y0 
x: (, y: y0 
x: (, y: y0 
x: (, y: y0 
x: (, y: y0 
當我輸入這個

(point1, point2, point3, point4, point5) = points 
print (point1) 
#print (point2) 
#print (point3) 
#print (point4) 
#print (point5) 

for point in points: 
    x, y, *data = point 
    print(str.format("x: {0}, y: y{1}", x, y)) 

file.close() 

我不知道爲什麼很拉(從(0.793803,0.136326,0,0.5,0)

我可以告訴你,程序認爲

如果我把x,y,z,w,s

我想要x:'('y:'0'z:'。'w:'7's:'9' , 我想: x:0.793803 y:0.136326 z:0 瓦特:0.5 S:0

對不起,所有的問題,我真的感謝您的幫助

+0

http://stackoverflow.com/questions/17967426/elegant-unpacking-variable-length-tuples是類似的 – n611x007

+0

http://stackoverflow.com/questions/10299682/how-to-unpack-tuple-of-length -n-to-mn-variables可能是最有用的信息 – n611x007

回答

3

的點是元組,所以這是非常簡單的。

你可以先解包(其中p是一個點):

>>> x, y, *data = p 
>>> x 
0.793803 
>>> y 
0.136326 
>>> data 
[0, 0.5, 0] 

或者你可以索引到他們:

>>> p[0] 
0.793803 

或者你可以去瘋狂的字符串格式化:

>>> str.format("x{0[0]} y{0[1]}", p) 
'x0.793803 y0.136326' 

好的,也許不是最後一個。爲了便於閱讀,我會先解壓縮它們。


我在上下文例如:

for point in points: 
    x, y, *data = point 
    print(str.format("x: {0}, y: y{1}", x, y)) 

points是一個元組的「迭代」,每個元組代表在花鍵的點,取入可變point

+0

我嘗試了幾個不同的事情,取得了一些進展,但是你能否回頭看原始帖子,我發佈了附錄,其中包含我試過的東西 – Jeff

+0

@Jeff:我看到,我的建議是,你可以在for循環中執行上面的任何例子。增加了一個例子。也許我誤解了你。 – Skurmedel

+0

@Jeff:關於解壓縮的值太多。當你解開一個列表或元組時,它需要與列表或元組中的值一樣多的「目標變量」。當然,如果你做了'a,* rest =(...)'之類的事情,第一個元素之後的所有東西都會以'rest'結尾。 – Skurmedel

相關問題