如何分离输出数据

发布于 11-28 21:14 字数 4579 浏览 4 评论 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 等等

ADDENDUM

所以我最终做的是添加这个

(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) = point1

产生了错误太多值无法解包。

我尝试的另一件事是,

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

它只产生了第一个(而不是整个 X 值...

有解决方案吗?

ADDENDUM 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()

我不知道为什么会这样正在拉 (, from (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 坐标:0 宽:0.5 s: 0

抱歉提出所有问题,我非常感谢您的帮助

Here is my code:

#-------------------------------------------------------------------------------
# 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()

here is the output:

====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)

I want to figure out how to call out individual points given. For example, how would I have the code just spit out 0.793803, or just spit out 0.136326, etc etc

ADDENDUM

So what I ended up doing was adding this

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

which yielded:

(0.793803,0.136326,0,0.5,0)

But when I tried to parse again and I wrote

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

it came up with the error p is not defined

so then I tried
(x,y,z,w,s) = point1

which yielded the error too many values to unpack.

Another thing I tried was

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

which just yielded the first ( rather than the whole X value...

Any solutions?

ADDENDUM PT 2

So what happens is this:

====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

when I input this

(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()

I don't know why it is pulling (, from (0.793803,0.136326,0,0.5,0)

I can tell you that the program thinks that if I put x, y, z, w, s

I want x: '(' y: '0' z: '.' w: '7' s: '9'
which I don't,
I want:
x: 0.793803
y: 0.136326
z: 0
w: 0.5
s: 0

Sorry for all the questions, I really appreciate your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见2024-12-05 21:14:18

点是元组,所以相当简单。

您可以先解压它们(其中 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))

点是元组的“可迭代”,每个元组代表样条线上的一个点,捕获到变量点中。

The points are tuples, so it's rather simple.

You can unpack them first (where p is a point):

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

Or you can index into them:

>>> p[0]
0.793803

Or you can go crazy with string formatting:

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

Ok, maybe not that last one. I would unpack them first for readability.


My example in context:

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

points is an "iterable" of tuples, each tuple representing a point on the spline, captured into the variable point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文