Grasshopper

algorithmic modeling for Rhino

I have to drag lists thoruh the panel in order for python to accept it, WHY?!

I'm currently working on a script in GH that will export a karamba model to a txt file that I later will analyze in an other program.

When I'm trying to extract information about the supports for example I have to drag it through a panel before I can use it as an input to my python script. The supports comes as a list of strings (see figure) and I want to extract some of that information (e.g. what nodes are fixed) and write that to my txt file.

I extract the info with these lines:

for row in Support:
     node = row[8:row.find(' DOF')]
     file.write(" %s,\n" % node)

print node

>> 95

If I however don't drag it via a panel i get the following output:

for row in Support:
     node = row[8:row.find(' DOF')]
     file.write(" %s,\n" % node)

print node

>> Supports.Suppor

It's like the script doesn't get that each row is a string.

I have the input set to "list access" and type hint to "str" and I've tried to simplifying and flatten the list.

Greatful for help

Views: 1202

Replies to This Discussion

I think all Panel does is to cast the objects to string. Changing the Hint type should do the same for you but just to make sure you can try to map them to string explicitly in your code.

# map objects inside support to string

support = map(str, support)

# do your stuff

It's still not working...

This is what I get:

You should print support and not x. When you map values python creates a new list from your original list.

Yeah obviously, my mistake! 

I tried it again and I get the same result. 

The panel shows in what format I want it.

I don't have karamba to check it myself but here is what I would do.

1. Make sure Type hint for x input is set to No type hint (and not string) and try again.

2. If that didn't solve the problem then change str to repr in mapping function.

support = map(repr, x)

print support

what Mostapha says is correct. the reason it's not working is that karamba is NOT outputting a string - it's outputting a custom object type that has the string representation that you see.

Okey, so you are saying that even though if I set the type hint to "str" it won't help? Can I somehow change the output type from karamba so that my script understands that it is a string and not a custom object? 

David,

I get that you are trying to parse the string, but my question would be why? 

karamba outputs a custom object with all of its properties, which I am guessing are easier to access than it is to convert the object to a string and then parse that string. I would just take advantage of the format that karamba already implements and operate on object properties directly. However if you really want a string representation of the object itself, then Karamba actually offers a utility method that does that:


import clr
karambaPath = r'C:\Program Files\Rhinoceros 5 (64-bit)\Plug-ins\karamba.gha'
clr.AddReferenceToFileAndPath(karambaPath)

import Karamba.Supports as Supports

for i in x:
print i.toString()

I'm writing a .mgt file that I want to import to Midas Gen so I figured the easiest way was to work with strings? 

Using only "i.toString" solved my problem. Thanks!!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service