0

だから私は出力を与えるコードを持っています.私がする必要があるのは、コンマの間の情報を引き出し、呼び出されたときに動的に変化する変数にそれらを割り当てることです...これが私のコードです:

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) などで、どの数字がそれらの位置を占めていても、その値を取ります。

これらの数字を入れるために必要なコードは次のとおりです。

#-------------------------------------------------------------------------------
# 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) などを配置できるようにしたいと考えています。与えられた番号ごとに変更

どんな助けでも素晴らしいでしょう..事前に感謝します!

-ジェフ

4

2 に答える 2

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 座標が変更されます。変数名を修正した場合、これはほとんど不可能です。それが役立つことを願っています! (そして、あなたの説明が理解できることを願っています!そうでない場合はお知らせください....)

于 2011-08-09T17:07:46.543 に答える
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

于 2011-08-09T17:01:31.193 に答える