problem creating objects from text file

Discussions concerning programming of SOFTIMAGE©
Post Reply
clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

problem creating objects from text file

Post by clankill3r » 04 Oct 2013, 16:30

edit:

I spotted my error but i don't know how to fix.
With this i can set the length of cube15
#Application.SetValue("cube15.cube.length", 12.586, "")

but what if i have my cube as an object?

cube = Application.CreatePrim("Cube", "MeshSurface", "", "")
#this doesnt work:
Application.SetValue(cube.cube.length, 1, "")

what is the correct syntax?

You can skip reading the things below, but feel free to do


------------------

I have a textfile as tsv:
https://dl.dropboxusercontent.com/u/17630770/temp/output.tsv

It contains values to create boxes, the order is x y z width height depth
(where x, y and z are the center of the box)

I tested it in processing (i'm not so familiar with softimage and coding since it's more a hobby)
This is the result:
Screen Shot 2013-10-04 at 4.16.14 PM.png
Here is the code in case it helps:

Code: Select all

import peasy.*;

PeasyCam cam;

String[] lines;

PVector[] pos;
PVector[] scale;

void setup() {
  size(1024, 768, OPENGL);
  
  cam = new PeasyCam(this, 400);
  
  lines = loadStrings("output.tsv");
  
  pos = new PVector[lines.length];
  scale = new PVector[lines.length];
  

  String[] tokens;
  for(int i = 0; i < lines.length; i++) {
      tokens = split(lines[i], "\t");
      
      float x = float(tokens[0]);
      float y = float(tokens[1]);
      float z = float(tokens[2]);
      float w = float(tokens[3]);
      float h = float(tokens[4]);
      float d = float(tokens[5]);
      
      pos[i] = new PVector(x, y, z);
      scale[i] = new PVector(w, h, d);
      
  }
  
}


void draw() {
  background(0);
  
  fill(255);
  
  PVector p, s;
  for(int i = 0; i < pos.length; i++) {
    p = pos[i];
    s = scale[i];
    
    pushMatrix();
    translate(p.x, p.y, p.z);
    box(s.x, s.y, s.z);
    popMatrix();
     
  }
  
  
}
I try to get the same result in softimage, here it is:
Screen Shot 2013-10-04 at 4.18.40 PM.png
The scale is going wrong. In processing i use pixels. In softimage i try that a box of 1x1x1 mathes up with 1 unit of the grid.
But it's way bigger. First i try to make the box 1x1x1 unit and after that scale but i think the process of making it 1x1x1 goes wrong.
This is the line:

Application.SetValue(cube, 1, "")


(o yeah the count > 10 in my code is for if i want to load less lines, then i switch it to count < 10)

Code: Select all

import csv

count = 0

with open("C:\Users\Doeke\Desktop\output.tsv") as tsv:
    for line in csv.reader(tsv, delimiter="\t"): #You can also use delimiter="\t" rather than giving a dialect.
		
		x = float(line[0])
		y = float(line[1])
		z = float(line[2])
		w = float(line[3])
		h = float(line[4])
		d = float(line[5])
		
		if count > 10:
			LogMessage(line)
			cube = Application.CreatePrim("Cube", "MeshSurface", "", "")
			Application.SetValue(cube, 1, "")
			Application.Scale("", w, h, d, "siAbsolute", "siPivot", "siObj", "siXYZ", "", "", "", "", "", "", "", 0, "")
			Application.Translate("", x, y, z, "siAbsolute", "siPivot", "siObj", "siXYZ", "", "", "", "", "", "", "", "", "", 0, "")
	
		
		count += 1

User avatar
myara
Posts: 403
Joined: 28 Sep 2011, 10:33

Re: problem creating objects from text file

Post by myara » 04 Oct 2013, 16:59

For objects you don't need to use commands such as SetValue. Just change the object property, parameters, etc.

Ex:

Code: Select all

cube = Application.ActiveSceneRoot.AddGeometry( "Cube", "MeshSurface" )
cube.length = 1
With commands it would be:

Code: Select all

cube = Application.CreatePrim("Cube", "MeshSurface")
Application.SetValue(cube.fullname + ".cube.length", 1)
M.Yara
Character Modeler | Softimage Generalist (sort of)

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 04 Oct 2013, 17:36

thanks, it works like a champ :D

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: problem creating objects from text file

Post by csaez » 04 Oct 2013, 22:28

In addition, you can minimize the number of calls to the SDK duplicating the cubes instead of creating new ones on each iteration :)

Code: Select all

import csv
from siutils import si, simath
FILEPATH = r"C:\Users\csaez\Desktop\output.tsv"

# init cubes
with open(FILEPATH) as f:
    size = len(f.readlines())
cubes = [si.ActiveSceneRoot.AddGeometry("Cube", "MeshSurface")]
cubes[0].Parameters("Length").Value = 1.0
cubes.extend(list(si.Duplicate(cubes[0], size - 1)))

# transform cubes
tm = simath.CreateTransform()
with open(FILEPATH) as f:
    for i, line in enumerate(csv.reader(f, delimiter="\t")):
        x, y, z, w, h, d = line
        tm.SetScaling(simath.CreateVector3(w, h, d))
        tm.SetTranslation(simath.CreateVector3(x, y, z))
        cubes[i].Kinematics.Global.Transform = tm

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 05 Oct 2013, 01:23

cool thanks.
Running it now.
Did you run it? And if so how long did it take?

I also made a script to union all object so i end up with one?

Will it be faster to merge the scripts?
So that after every transfrom it will union with the previous one (which will be only one object in that case) ?

Here is the script:
(ps, this is the 2nd day is script in python)

Code: Select all

geom = Application.SelectAllUsingFilter("geometry", "siCheckComponentVisibility", "", "")

objects = []

for obj in geom :
   Application.LogMessage( obj.Name )
   
   objects.append(obj)
      
   
LogMessage("objects length: ")
LogMessage(len(objects))




#while len(objects) > 1:
for i in range(1, len(objects)):
	#do a union with the object from index 0 and index 1
	
	targetObjects = objects[0].name +";" + objects[i].name
	Application.ApplyTopoOp("BooleanUnion", targetObjects, 3, "siPersistentOperation", "")
	Application.FreezeObj(objects[0].name, "", "")
	Application.DeleteObj(objects[i])
	
	#objects.pop(0)
	
	
	
LogMessage("done")

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 05 Oct 2013, 01:41

I tried your script 2 times but it instant freezes softimage.

How can i stop the script after 20 iterations?
I tried:

Code: Select all

    for i, line in enumerate(csv.reader(f, delimiter="\t")):
		if i > 20:
			break
        x, y, z, w, h, d = line
        tm.SetScaling(simath.CreateVector3(w, h, d))
        tm.SetTranslation(simath.CreateVector3(x, y, z))
        cubes[i].Kinematics.Global.Transform = tm
But that doesn't seem to work

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: problem creating objects from text file

Post by csaez » 05 Oct 2013, 04:04

clankill3r wrote:I tried your script 2 times but it instant freezes softimage.
Perhaps it's just duplicating the cubes, How many lines of text have your csv test file?
It took 0.828 secs using the sample file on my side.
clankill3r wrote:But that doesn't seem to work
Check your indentation, python uses it to determine the grouping of statements :)

Code: Select all

for i, line in enumerate(csv.reader(f, delimiter="\t")):
    if i > 20:
        break
    x, y, z, w, h, d = line
    tm.SetScaling(simath.CreateVector3(w, h, d))
    tm.SetTranslation(simath.CreateVector3(x, y, z))
    cubes[i].Kinematics.Global.Transform = tm
Cheers!

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 05 Oct 2013, 10:45

My indentation broke with pasting here.
I have it the same as in your post but i still have an error.
Here a screenshot.
break.PNG
O yeah i have 26000 lines.

Could you take a look at the 2nd post above your last one? (about the union)

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 05 Oct 2013, 14:27

I modified the script, to load a cube and then union it cause i had to union all anyway.
On a small file it works pretty good.

On the big file with 26000 lines i get
Failed to save scene before system failure
after a while

Is there any way to change my script so it's more likely to finish?

Code: Select all

import csv
from siutils import si, simath
from time import clock
FILEPATH = r"C:\Users\Doeke\Desktop\output.tsv"

startTime = clock()

# we union with this one (I would prefer a empty polymesh)
masterCube = si.ActiveSceneRoot.AddGeometry("Cube", "MeshSurface")
masterCube.Parameters("Length").Value = 1.0


# transform cubes
tm = simath.CreateTransform()
with open(FILEPATH) as f:

    for i, line in enumerate(csv.reader(f, delimiter="\t")):
        x, y, z, w, h, d = line
        tm.SetScaling(simath.CreateVector3(w, h, d))
        tm.SetTranslation(simath.CreateVector3(x, y, z))
	newCube = si.ActiveSceneRoot.AddGeometry("Cube", "MeshSurface")
	newCube.Parameters("Length").Value = 1.0
	newCube.Kinematics.Global.Transform = tm
	# this could probably be done better
	targetObjects = masterCube.name +";" + newCube.name
	Application.ApplyTopoOp("BooleanUnion", targetObjects, 3, "siPersistentOperation", "")
	Application.FreezeObj(masterCube.name, "", "")
	Application.DeleteObj(newCube)
		

print clock() - startTime

here's the file
https://dl.dropboxusercontent.com/u/17630770/temp/output.tsv

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: problem creating objects from text file

Post by csaez » 05 Oct 2013, 17:18

26000 lines! that's a lot... For that amount of data I would parse the file as icecache.
Fortunately Bradley Gabe shared a nice python module to write icecache files (thanks Brad), you can find it at the bottom of the page.
http://softimage.wiki.softimage.com/ind ... at#Example

So, now you can easily generate the icecache file and load it on an empty pointcloud as usual :)

Code: Select all

import csv
import icecache

DATAFILE = r"C:\Users\csaez\Desktop\output.tsv"
CACHEFILE = r"C:\Users\csaez\Desktop\output.icecache"

with open(DATAFILE) as f:
    nbParticles = len(f.readlines())
ic = icecache.icecache(nbParticles)

pos = list()
scale = list()
with open(DATAFILE) as f:
    for line in csv.reader(f, delimiter="\t"):
        pos.append(line[:3])
        scale.append(line[3:])

ic.addPointPosition(pos)
ic.addVector3("scale", scale)
ic.write(CACHEFILE)
Attachments
custom_icecache.jpg

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 05 Oct 2013, 18:31

In the end i need to export it to an obj file or any other file i can open with sketchup.
And that i union is important.
Is that possible in ice?

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: problem creating objects from text file

Post by csaez » 05 Oct 2013, 19:38

Sure! there's the polygonizer or the 'create copies from polygon mesh' node (allows to use a pointcloud as template).

Anyway, Softimage didn't crash here when I tried the first script I posted on the big file (it took roughly 7 minutes)... Softimage could be crashing on you due to lack of memory (check the resource manager), if that's the case you can always process the file on small chunks and merge them all together as a final step.

tl;dr -> here is the .obj file.

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 05 Oct 2013, 21:20

Thanks, your really helpfull.

One more thing, the most important things.
Before i loaded a cube, did a boolean union with the previous one and so on untill the test file was done.
This cleaned things up really nice, unfortunatly it couldn't load the 26000 lines but it worked good on a smaller text file.
To illustrate, it would get rid of the edges i selected here:
edges_union.PNG
The reason this is important is for the penplotter. Else it would draw to many lines.
Is this in some way still possible?

I will try now how you made the .obj file so i can do it myself.

--

edit:

where do i need to put the icecache.py file?

I guess somewhere here:
C:\Program Files\Autodesk\Softimage 2014 SP2\Application\python

But i'm not sure.

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: problem creating objects from text file

Post by csaez » 05 Oct 2013, 23:25

clankill3r wrote:where do i need to put the icecache.py file?
You can...
- Put it on the same folder than the script (if you run the script within softimage it doesnt work, there's some 'black magic' under the hood changing the environment).
- Copy it to python/Lib/site-packages (which should be already set on the PYTHONPATH).
or
- Add the folder to sys.path as below:

Code: Select all

import sys
FOLDER = r"C:\Users\csaez\Desktop" #change this
if FOLDER not in sys.path:
    sys.path.append(FOLDER)

import icecache
# do stuff
About the edges: I'm not sure it can be done automagically, you can try using the 'polygon reduction' operator but I doubt that will solve all your problems... worth a try :)

clankill3r
Posts: 131
Joined: 21 Oct 2012, 15:36

Re: problem creating objects from text file

Post by clankill3r » 06 Oct 2013, 22:43

I can't get the union done so i just try python again.
I saved around 1400 tsv files. They represent groups where objects touch.
This worked well, i only have 1 file left with 6657 lines so that saves quite a lot.

However i have been testing and if i reduce the file to even 11 lines SI still crashes (maybe with even less).

Another file with a bit more then 600 lines worked fine.

any idea?

781.5 472.0 1.5 1.0 2.0 1.0
780.5 472.0 1.5 1.0 2.0 1.0
779.5 471.5 1.5 1.0 3.0 1.0
778.5 471.5 1.5 1.0 3.0 1.0
777.5 471.0 1.5 1.0 4.0 1.0
776.5 472.0 0.5 1.0 4.0 1.0
775.5 471.0 1.0 1.0 2.0 2.0
776.5 470.5 1.5 1.0 3.0 1.0
779.5 472.5 0.5 1.0 3.0 1.0
778.5 472.5 0.5 1.0 3.0 1.0

Code: Select all

import csv
from siutils import si, simath
from time import clock
#FILEPATH = r"C:\Users\Doeke\Desktop\\objectsPerGroup\output_g1448.tsv"
FILEPATH = r"C:\Users\Doeke\Desktop\\output_10.tsv"

startTime = clock()

# we union with this one (I would prefer a empty polymesh)
masterCube = si.ActiveSceneRoot.AddGeometry("Cube", "MeshSurface")
masterCube.Parameters("Length").Value = 1.0


# transform cubes
tm = simath.CreateTransform()
with open(FILEPATH) as f:

    for i, line in enumerate(csv.reader(f, delimiter="\t")):
        x, y, z, w, h, d = line
        tm.SetScaling(simath.CreateVector3(w, h, d))
        tm.SetTranslation(simath.CreateVector3(x, y, z))
	newCube = si.ActiveSceneRoot.AddGeometry("Cube", "MeshSurface")
	newCube.Parameters("Length").Value = 1.0
	newCube.Kinematics.Global.Transform = tm
	# this could probably be done better
	targetObjects = masterCube.name +";" + newCube.name
	Application.ApplyTopoOp("BooleanUnion", targetObjects, 3, "siPersistentOperation", "")
	Application.FreezeObj(masterCube.name, "", "")
	Application.DeleteObj(newCube)
		

print clock() - startTime

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests