Register Latest Topics
 
 
 


Reply
  Author   Comment   Page 1 of 2      1   2   Next
Neo_Corex

First taste
Registered:
Posts: 8
Reply with quote  #1 

Hello Everyone,

I am an artist who needs help with the following Python script.
Below is a Maya python script which imports point cloud files into Maya.

Image 1 shows how the point cloud looks when imported into Maya.
The point cloud is not dense enough.

Image 2 is how it needs to look with updated coding.

If anyone could help update the script so the point cloud is dense like image 2,
it would greatly appreciated.

 

Much Thanks
Vincent.

Code:

# Maya .PTS to Particles Script
import maya.cmds as cmd
import maya.mel as mel
import re
# set user params
filePath = "D:\\Files\\Misc_Projects\\Projects\\Tractor.pts"
# start from nth particle
start = 1
# set particle limit
limit = 100000
# subsample by a certain factor
factor = 0.001
posDataList = []
colorDataList = []
step = 1/factor
ptc = cmd.particle()[1]
try:
    cmd.addAttr(ptc, ln='rgbPP', dt='vectorArray')
    cmd.addAttr(ptc, ln='rgbPP0', dt='vectorArray')
    cmd.setAttr(ptc + '.isDynamic', 0)
    # the following is just to speed up visualization
    cmd.setAttr(ptc + '.particleRenderType',3)
except:
    print 'error encoutered while setting up'
# lazily read file
with open(filePath) as infile:
    count = 0
    for line in infile:
        count += 1
        if count < start: continue
        if count%step == 0:
            data = re.sub(' +',' ',line).split(' ')
            posData = (float(data[0]), float(data[1]), float(data[2]))
            colorData = (float(data[3])/255, float(data[4])/255, float(data[5])/255)
            posDataList.append(posData)
            colorDataList.append(colorData)
        if (count - start) * factor > limit: break
# cmd.emit( o=ptc, pos=posDataList, at=('rgbPP'), vv=colorDataList )
# the above should work, according to Maya's Documentation, but it doesn't (Maya crashes)
cmd.emit( o=ptc, pos=posDataList )
ptcs = cmd.particle( ptc, q=1, count=1)
#print ptcs
for (id) in xrange(ptcs):
    cmd.particle(ptc, e=1, at='rgbPP', vv=colorDataList[id], order=id)
cmd.saveInitialState(ptc)

code.jpg 


0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #2 
looking at your code I see that there is a much simpler and faster way to do what you are after.
Please provide a ptc file for me to test with.
0
Neo_Corex

First taste
Registered:
Posts: 8
Reply with quote  #3 

Hello pshipkov,

Attached is a .pts file. 
Hopefully it will help you find a way to import a dense point cloud in Maya.

 

https://www.dropbox.com/s/6phtch04lqw4a0v/6221%20-%20Tractor.pts?dl=0

 

Much Regards,

0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #4 
Below is a simple snippet of code that converts the 4 million points into nParticles in couple of seconds.

You will need one SOuP node, so make sure the plugin is loaded.

Code:

import maya.OpenMaya as om
import maya.cmds as mc
# setup
xform, shape = mc.nParticle()
mc.addAttr(xform, ln="positions", dt="vectorArray")
mc.addAttr(xform, ln="colors", dt="vectorArray")
mc.addAttr(shape, ln="rgbPP", dt="vectorArray")
mc.addAttr(shape, ln="rgbPP0", dt="vectorArray")
mc.setAttr(shape+".collide", False)
mc.setAttr(shape+".forcesInWorld", False)
mc.setAttr(shape+".ignoreSolverWind", True)
mc.setAttr(shape+".ignoreSolverGravity", True)
mc.setAttr("nucleus1.subSteps", 1)
mc.setAttr("nucleus1.maxCollisionIterations", 0)
mc.dynExpression(shape, s="rgbPP = "+xform+".colors", c=True)
pctc = mc.createNode("pointCloudToCurve")
mc.connectAttr(xform+".positions", pctc+".inArray")
mc.connectAttr(pctc+".outCurve", shape+".inputGeometry")
# read
f = open("path/to/data.pts")
l = f.readlines()
f.close()
# prep
c = len(l)
pnts = om.MVectorArray(c)
clrs = om.MVectorArray(c)
for i in range(c):
  l2 = l[i].strip().split(" ")
  if len(l2) < 6: continue
  pnts[i].x = float(l2[0])
  pnts[i].y = float(l2[1])
  pnts[i].z = float(l2[2])
  clrs[i].x = float(l2[4])/255
  clrs[i].y = float(l2[5])/255
  clrs[i].z = float(l2[6])/255
# set
sl = om.MSelectionList()
sl.add(xform+".positions")
p = om.MPlug()
sl.getPlug(0, p)
vad = om.MFnVectorArrayData()
o = vad.create(pnts)
p.setMObject(o)
sl.add(xform+".colors")
sl.getPlug(1, p)
vad = om.MFnVectorArrayData()
o = vad.create(clrs)
p.setMObject(o)

0
Neo_Corex

First taste
Registered:
Posts: 8
Reply with quote  #5 

Hi pschipkov,

Thanks so much, your script did the Job!
The point cloud looks just like the original scan.

I'm new to SOuP, so I'm not sure how to get the file to open within seconds as you described. The file took about 25 minutes for me to open.

Thanks So Much!

Regards,

0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #6 
This sounds really odd.
Here it takes just few seconds.

I am using PE and VP2.
Can you confirm if you are in VP1 ?
0
Neo_Corex

First taste
Registered:
Posts: 8
Reply with quote  #7 

I've tried using Viewport 2.0 and Legacy Default Viewport with long load times.

Are there any setting changes I should try?

0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #8 
I don't know what to say really.
Just tried the script above in standard installation of Maya2016 on Windows - same as before - took about 10 seconds to load.
I believe that the approach i provided is the fastest way to load the data without going to the C++ API.
Well, additional boost can come if one uses Python API2 calls, but i think the potential performance improvement will be small enough to ignore.
0
Neo_Corex

First taste
Registered:
Posts: 8
Reply with quote  #9 

I'm using Maya 2017, so perhaps that is the issue.
I tried using the script in Maya 2016, but receive and error message

"// Error: Line 3.1: Syntax error"

Might be something wrong l'm doing on my side.

Much thanks for all your help.

Regards,

 

0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #10 
Can you provide the Python stack trace ?
You need to turn-on "show stack trace" in Script Editor.
0
Neo_Corex

First taste
Registered:
Posts: 8
Reply with quote  #11 

Great, It's importing in 2016.

Thanks so much!

 

0
cgdept

First taste
Registered:
Posts: 12
Reply with quote  #12 
# Error: IOError: file <maya console> line 20: 2 #
0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #13 
wrong file path perhaps ?
0
cgdept

First taste
Registered:
Posts: 12
Reply with quote  #14 
Im just , copy / paste script editor , and start to selected object
0
pshipkov

SOuP Jedi
Registered:
Posts: 4,711
Reply with quote  #15 
Python tab in the script editor, correct ?
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation: