Fri, Nov 29, 6:42 PM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: .sorry to bug pythonese about visual basic again ... but ... how to make multidi


elenorcoli ( ) posted Sat, 09 June 2007 at 4:18 PM · edited Sat, 09 November 2024 at 3:27 AM

visual basic forums don't seem to jumpy.  i figure a pythonesque take on this would be ok for me too, cause i'm sure the solution lies in the nesting of the code.  (don't know python yet though). 

ok here is the code i have so far

this code opens 2 files, file a which is

v -0.0002 0.0020 -0.0200
v -0.2002 0.2020 -0.2200

and file b which is

v -0.0001 0.0010 -0.0100
v -0.1001 0.1010 -0.1100

so i break down the files into arrays, "separatelines" first for each and then "separateelements" for each separate line creating a multidimensional array.  how do i code to get these array elements to compare to each other?  or how do i code to perform a math function, say a's (0,2) plus b's (0,2)? 
i have only been at programming for about 2 1/2 months, so i sure could use and explicit explanation of how to accomplish this.  thanks very much.

also you may notice the k and o because i am planning on further breaking down the elements into chars ( in a second version of this program) so that i can compare the elements char by char to discover changes that occur only in the lowest decimal, not the highest.  in the finished program, the numbers will run about 6 or 8 places behind the decimal ( like 0.11443355 )  and if the first 4 or so are identical, but the last few are not, the element from the a array will overwrite the element from the b array.  if any of the first 4 are not identical, the element will be preserved.  but i figure i wll check char by char, preserving the original, until a change is seen, at which point the overwriting will take place. (but only so many digits in, not all the way)
at this point we can ignore the k and o though.  i think figuring out this one problem will answer the second one.
again, thanks a lot.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' opens two files of verts and reads them

        Dim file1 As String
        Dim file2 As String
        Dim textbox2holder As String
        Dim textbox4holder As String

        file1 = file1inputbox.Text
        file2 = file2inputbox.Text

        Dim objReader1 As New System.IO.StreamReader(file1)
        Dim objreader2 As New System.IO.StreamReader(file2)

        'shows the contents of the 2 files in textboxes

        textbox2holder = objReader1.ReadToEnd
        objReader1.Close()
        textbox4holder = objreader2.ReadToEnd
        objreader2.Close()

        TextBox2.Text = textbox2holder
        TextBox4.Text = textbox4holder

        'variables for looping in file1

        Dim i As Integer
        Dim j As Integer
        Dim k As Integer

        Dim separatelines1() As String
        Dim separatelines1holder As String
        Dim separateelements1() As String
        Dim separateelements1holder As String
        Dim separatechars1() As String
        Dim separatechars1holder As String
        Dim separatechars1holdera As String

        'variables for looping in file2

        Dim m As Integer
        Dim n As Integer
        Dim o As Integer

        Dim separatelines2() As String
        Dim separatelines2holder As String
        Dim separateelements2() As String
        Dim separateelements2holder As String
        Dim separatechars2() As String
        Dim separatechars2holder As String

        Dim separatechars1holderdou As Double
        Dim separatechars2holderdou As Double

        'start the mad ass looping (or should i say arraying) set up 3d array
        separatelines1 = textbox2holder.Split("vbnewline")
        For i = 1 To UBound(separatelines1)
            MsgBox(separatelines1(i))
            separateelements1holder = separatelines1(i)

            separateelements1 = separateelements1holder.Split(" ")
            For j = 1 To UBound(separateelements1)
                MsgBox(separateelements1(j))
                separatechars1holder = separateelements1(j)
                separatechars1holderdou = CInt(separatechars1holder)
            
            Next
        Next

        separatelines2 = textbox4holder.Split("vbnewline")
        For i = 1 To UBound(separatelines2)
            MsgBox(separatelines2(i))
            separateelements2holder = separatelines2(i)

            separateelements2 = separateelements2holder.Split(" ")
            For j = 1 To UBound(separateelements2)
                MsgBox(separateelements2(j))
                separatechars2holder = separateelements2(j)
                separatechars2holderdou = CInt(separatechars2holder)

            Next

        Next

End Sub
End Class


adp001 ( ) posted Sat, 09 June 2007 at 5:13 PM

Perhaps things would be simpler if you read your file line by line with "ReadLine".
Simpler and faster.
You can split each line into parts (see Split() in your manual) directly and fill your array with what you really need. 

In python it may look like this:

<pre style="font-family:'courier new';">
def readIntoArray(filehandle, leading=False) :<br></br>    """<br></br>    Read data from a file (wavefront object format) int an array.<br></br>    leading: the character preceeding a line of data ("v 0 0 0").<br></br>            Lines starting with another characters than leading are ignored<br></br>            if leading is set.<br></br>    Returns a filled array.<br></br>    """<br></br><br></br>    ar = []                     # define an empty array<br></br><br></br>    for line in filehandle:     # read line by line -<br></br>        if not line :           # skip empty lines<br></br>            continue<br></br>        first,x,y,z=line.split()   # we assume that each line has 4 elements<br></br>                                # seperated by whitespace(s).<br></br><br></br>        if leading and (leading != first):<br></br>            continue<br></br><br></br>        ar.append([x,y,z])      # append row of data to array<br></br><br></br>    return ar                   # return created array<br></br><br></br><br></br>filehandle = open("my1FileWithData.obj")<br></br>array1 = readIntoArray(filehandle)<br></br>filehandle.close()<br></br><br></br>filehandle = open("my2FileWithData.obj")<br></br>array2 = readIntoArray(filehandle)<br></br>filehandle.close()




elenorcoli ( ) posted Sat, 09 June 2007 at 7:57 PM

ok wow that does look fast.  when i first started i went with read line and then to split, but had some kinda problem with variable type (probably some newbie mistake somewhere) so i went with split all the way. 

does this code not just open the two files, right?  so how do we compare the arrays?  like morphing outside of poser, just perform the math operations on each element? that is where i am stuck on my code.  i didn't write the futile attempts i made to get there.  but i am seeking to add, subtract, or compare, the x, y and z's of each obj.

thanks for your help,
ec

by the way working on picking up python now...hearing a lot of good about it


adp001 ( ) posted Sat, 09 June 2007 at 10:38 PM

Do we talk about VB or Python?

For Python there is NumPy (or numarray for older Python versions), specialiced on ultrafast math with arrays (use Google and look for a version compatible with your Python version).

Don't know if something similar is available for .NET (By the way: I'm using C# on windows from time to time, but for most things Python is simpler; C# is faster sometimes if no specialized library for Python is available).

You'll run into variable type trouble as soon as you try to do arithmetic with a string (what is read from a file is string or byte-string). But Python has a lot of fast conversion functions. One way to go is this:

...
first,data = line.split(None,2)

with this the line is splitted into 2 parts. The first character (a "v" for example) and the rest of the line (into data, the 3 remaining values as one string).

If the first parameter for split is , Python assumes whitespaces (tabs, blanks, lineend-characters).

To split the data into numeric types we can use:

x,y,z = map(float, data.split() )

map() is a function that "maps" the second sequence-parameter (the members of an array as a result from split()) to the function named as first parameter (float() is a function). So x,y,z is filled with the (float) values contained in data.

Because you know what kind of data will come in and that all data has to be computed in some way, you may use another function insteed of float(). Python knows round(value,dezimalplaces) for example. This function returns shortened the way you need it. But the value has to be a float allready. So you can't use it as a parameter for map(). One way out is to write a user-defined function:

def strRound(value,decimalplaces):
    return round(float(value),decimalplaces)

After this you may write:

x,y,z = map(strRound, data.split())

To compare 2 array-members with each other, you can go the simple way:

ar0 = [1,2,3]
ar1 = [1,2,3]
ar2 = [2,3,4]

ar0==ar1 is True
ar0==ar2 is False

For more complicated comparisons and arraymanipulations you may consider to write user-defined functions.

To walk through an array (to call one of your functions, for example), simply write:

for member in myarray: print myFunction(member)

That's all.




adp001 ( ) posted Sat, 09 June 2007 at 10:49 PM

Sorry, a mistake:

def strRound(value,decimalplaces=4):
    return round(float(value),decimalplaces)

map() can't get a constant parameter (decimalplaces), so the default value of 4 is used in strRound (use a number that fits your needs).

In the previous example map(strRound, data.split()) will result in an error.




Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.