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

# 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]
    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)
    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)
        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)


Quote 0 0
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.
Quote 0 0

Hello pshipkov,

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


Much Regards,

Quote 0 0
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.

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()
# 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()
p = om.MPlug()
sl.getPlug(0, p)
vad = om.MFnVectorArrayData()
o = vad.create(pnts)
sl.getPlug(1, p)
vad = om.MFnVectorArrayData()
o = vad.create(clrs)

Quote 0 0

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!


Quote 0 0
This sounds really odd.
Here it takes just few seconds.

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

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

Are there any setting changes I should try?

Quote 0 0
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.
Quote 0 0

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.



Quote 0 0
Can you provide the Python stack trace ?
You need to turn-on "show stack trace" in Script Editor.
Quote 0 0

Great, It's importing in 2016.

Thanks so much!


Quote 0 0
# Error: IOError: file <maya console> line 20: 2 #
Quote 0 0
wrong file path perhaps ?
Quote 0 0
Im just , copy / paste script editor , and start to selected object
Quote 0 0
Python tab in the script editor, correct ?
Quote 0 0