控制我的输出

发布于 2024-11-29 12:57:15 字数 2999 浏览 1 评论 0原文

这是代码:

#!/usr/bin/env python
import json
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.
    print(len(points))
    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)

(p1, p2, p3, p4, p5) = points
print (p1)
#print (p2)
#print (p3)
#print (p4)
#print (p5)

file.close()

这是我的输出:

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

从这个输出我希望能够将(0.793803)分配给变量'point1x'(0.136326)到变量point1y(0)到变量point1z(0.5)到变量point1w和 (0) 到变量 point1s。

因此,我不只是输出 (0.793803,0.136326,0,0.5,0) 我希望它单独给我值,

这样:

point1x: 0.793803
point1y: 0.136326
point1z: 0
point1w: 0.5
point1s: 0

有人知道我该怎么做吗?

Here is the code:

#!/usr/bin/env python
import json
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.
    print(len(points))
    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)

(p1, p2, p3, p4, p5) = points
print (p1)
#print (p2)
#print (p3)
#print (p4)
#print (p5)

file.close()

Here is my output:

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

from this output I want to be able to assign (0.793803) to the variable 'point1x' (0.136326) to the variable point1y (0) to the variable point1z (0.5) to the variable point1w and (0) to the variable point1s.

So instead of just outputting (0.793803,0.136326,0,0.5,0) I want it to give me the values individually

so:

point1x: 0.793803
point1y: 0.136326
point1z: 0
point1w: 0.5
point1s: 0

Anyone know how I can do this?

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

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

发布评论

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

评论(2

思念满溢 2024-12-06 12:57:15

这看起来像一个元组,在这种情况下,您想要的 python 语句是:

point1x, point1y, point1z, point1w = (0.793803,0.136326,0,0.5,0)

That looks like a tuple, in which case, the python statement for what you want is:

point1x, point1y, point1z, point1w = (0.793803,0.136326,0,0.5,0)
乖乖公主 2024-12-06 12:57:15

一旦你有了,如果你知道它将始终是一个以逗号分隔的十进制数字列表,只需执行以下操作:

points = points.split(",")
# points = ["0.793803","0.136326","0","0.5","0"]
# Now you can use John Gaines suggestion
# to unpack your variables
point1x, point1y, point1z, point1w = points

如果你不知道有多少分任何给定的条目都需要解包,但您知道最大值,只需在拆分后检查点长度并将适当数量的无条目添加到列表中即可:

# Somewhere earlier
max_entries = 5
# ... snip ...
# points = ["0.793803","0.136326","0"]
cur_len = len(points)
if cur_len > max_entries:
    raise ValueError("%d points discovered in %s.  Max entries is %d" % (cur_len, points, max_entries)
 if cur_len != max_entries:
     points += [None] * (max_entries - cur_len)
 # Continue from here

Once you have points, if you know it will always be a comma-seperated list of decimal numbers, simply do something like this:

points = points.split(",")
# points = ["0.793803","0.136326","0","0.5","0"]
# Now you can use John Gaines suggestion
# to unpack your variables
point1x, point1y, point1z, point1w = points

If you don't know how many points there will be to unpack for any given entry, but you know the max, simply check points length after you split and add the appropriate number of None entries to the list:

# Somewhere earlier
max_entries = 5
# ... snip ...
# points = ["0.793803","0.136326","0"]
cur_len = len(points)
if cur_len > max_entries:
    raise ValueError("%d points discovered in %s.  Max entries is %d" % (cur_len, points, max_entries)
 if cur_len != max_entries:
     points += [None] * (max_entries - cur_len)
 # Continue from here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文