Poly Object to Script

Hi

Has anyone got a script that will take a custon polygon mesh and output the code to build it in a max script. ie save out all the point and polygon data?
I am trying to make my own custom helper objects.

Thanks

Gram

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Graph's picture

nope thats correct,

just add a few lines to add that stuff to the string. if itd been that easy it wouldnt have been a good exercise, now would it?! ;)

Raphael Steves

gramx's picture

Updated script

I have added a loop to check for any invisible edges and added a setEdgeVis to the string. see example below. I had to also assign the mesh to a variable so that I could reference it in the setEdgeVis function otherwise it wouldn't work! I still need to figure out how to add this as a nodeRef to the local scope of the pluginNode. I will keep trying! Thanks for being patient and all your help!

example:-

execute "(themesh = mesh vertices:#([-2.5,-2.5,0], [2.5,-2.5,0], [-2.5,2.5,0], [2.5,2.5,0], [-2.5,-2.5,5], [2.5,-2.5,5], [-2.5,2.5,5], [2.5,2.5,5]) faces:#([1,3,4], [4,2,1], [5,6,8], [8,7,5], [1,2,6], [6,5,1], [2,4,8], [8,6,2], [4,3,7], [7,8,4], [3,1,5], [5,7,3])); setEdgeVis themesh 1 3 false; setEdgeVis themesh 2 3 false; setEdgeVis themesh 3 3 false; setEdgeVis themesh 4 3 false; setEdgeVis themesh 5 3 false; setEdgeVis themesh 6 3 false; setEdgeVis themesh 7 3 false; setEdgeVis themesh 8 3 false; setEdgeVis themesh 9 3 false; setEdgeVis themesh 10 3 false; setEdgeVis themesh 11 3 false; setEdgeVis themesh 12 3 false;"

(
	--mesh vertices:<array_of_point3s> faces:<array_of_point3s> --[ materialIDs:<array_of_integers> ] \
 
	fn obj2Code obj = 
	(
		if isValidNode obj do
		(
			local tMesh = copy obj.mesh
			local nVerts = tMesh.numverts
			local nFaces = tMesh.numfaces
 
 			local edges = #()
			local verts = for v = 1 to nVerts collect getVert tMesh v
			local faces = for f = 1 to nFaces collect getFace tMesh f
 
			local str = "(themesh = mesh vertices:"
			with printAllElements true 
			(
				str += verts as string
				str += " faces:"
				str += faces as string
				str += ");"
				for f = 1 to nFaces do
				(
					if (getEdgeVis tMesh f 1) == false do
						(
							str += " setEdgeVis themesh "
							str += f as string
							str += " 1 false;"
						)
					if (getEdgeVis tMesh f 2) == false do 
						(
							str += " setEdgeVis themesh "
							str += f as string
							str += " 2 false;"
						)
					if (getEdgeVis tMesh f 3) == false do 
						(
							str += " setEdgeVis themesh "
							str += f as string
							str += " 3 false;"
						)
				)
 
			)	
			tMesh = undefined
 
			return str
		)
	)--END obj2Code FN
 
	local tString = (obj2Code selection[1])
	format "%\n "tString
	tString
)
[\code]
Garp's picture
gramx's picture

Yes, its very similar to what

Yes, its very similar to what I am doing. cheers

Graph's picture

[code] bla bla [/code]

[code] bla bla [/code]

btw every geometryNode in max is essentially a trimesh (well at least the important ones Tongue out)

why dont you try to modify the function i made to also add code for setting the edge viz for the new object? its simple and you can make your helper way dynamic by saving the string with the geoCode in a parameter of the node... that way users can either choose between preset helper shapes or pick their own geo shapes... dont forget that for that you need a temp global var to pass the nodeRef to local scope of the pluginNode

Raphael Steves

gramx's picture

edge visibility

I tried modifying your code to this to add edge visibility:

(
	--mesh vertices:<array_of_point3s> faces:<array_of_point3s> --[ materialIDs:<array_of_integers> ] \
 
	fn obj2Code obj = 
	(
		if isValidNode obj do
		(
			local tMesh = copy obj.mesh
			local nVerts = tMesh.numverts
			local nFaces = tMesh.numfaces
 
 			local edges = #()
			local verts = for v = 1 to nVerts collect getVert tMesh v
			local faces = for f = 1 to nFaces collect getFace tMesh f
 
 			for f = 1 to nFaces do
			(
				local facef = #()
				append facef (getEdgeVis tMesh f 1)
				append facef (getEdgeVis tMesh f 2)
				append facef (getEdgeVis tMesh f 3)
				append edges facef
			)
 
			local str = "(mesh vertices:"
			with printAllElements true 
			(
				str += verts as string
				str += " faces:"
				str += faces as string
				str += " edgevis:"			
				str += edges as string
				str += ");"
			)	
			tMesh = undefined
 
			return str
		)
	)--END obj2Code FN
 
	local tString = (obj2Code selection[1])
	format "%\n "tString
	tString
)

this adds edgevis: to the code but its not recognised in the mesh command. Just gets ignored when executed.
Looking at the help it appears that you can only set the Edge viz with the following command setEdgeVis

Am I missing anything?

Graph's picture

nope

nope

btw you should only create the node in memory, that way you dont have to delete anything, afaik that fn doesnt work with a mesh but hey something for you to experiment with :)

try:
meshobj = createinstance pyramid()
to understand what i mean

Raphael Steves

gramx's picture

Custom Mesh Helper Script

Not sure how to only create the node in memory. It seems to work OK so far! I adabted the code from this forum :- http://forums.cgsociety.org/archive/index.php/t-288270.html

It also seems that standard helpers can only be tri-meshes and only display as wireframe.

I have added edge visibility to the code otherwise you get a horrable triangulated mesh helper!

(
 --mesh vertices:<array_of_point3s> faces:<array_of_point3s> --[ materialIDs:<array_of_integers> ] \
 
 fn obj2Code obj =
 (
  if isValidNode obj do
  (
   local tMesh = copy obj.mesh
   local nVerts = tMesh.numverts
   local nFaces = tMesh.numfaces
 
   local edges = #()
   local verts = for v = 1 to nVerts collect getVert tMesh v
   local faces = for f = 1 to nFaces collect getFace tMesh f
    
   for f = 1 to nFaces do
   (
    local facef = #()
    append facef (getEdgeVis tMesh f 1)
    append facef (getEdgeVis tMesh f 2)
    append facef (getEdgeVis tMesh f 3)
    append edges facef
   )
    
    local str = "local vertsArray = "
   with printAllElements true
   (
    str += verts as string
    str += "\nlocal facesArray = "
    str += faces as string
    str += "\nlocal edgeVis = "
    str += edges as string
   ) 
   tMesh = undefined
 
   return str
  )
 )--END obj2Code FN
  output = newscript()
 local tString = (obj2Code selection[1])
 format "%\n "tString to: output
)

--end

 

The code now outputs a new script in the following format:-

local vertsArray = #([-2.5,-2.5,0], [2.5,-2.5,0], [-2.5,2.5,0], [2.5,2.5,0], [-2.5,-2.5,5], [2.5,-2.5,5], [-2.5,2.5,5], [2.5,2.5,5])
local facesArray = #([1,3,4], [4,2,1], [5,6,8], [8,7,5], [1,2,6], [6,5,1], [2,4,8], [8,6,2], [4,3,7], [7,8,4], [3,1,5], [5,7,3])
local edgeVis = #(#(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false))

I then drop this code into the following ---mesh--- section of the scripted helper plugin:-

plugin Helper Mesh2Helper
name:"Mesh2Helper"
classID:#(0x15424e9f, 0x5f189e30)
category:"Standard"
extends:dummy
(
 local  lastSize, meshObj, theMesh, lastpshape

 parameters pblock rollout:params
(
size type:#float animatable:true ui:size default:1.0
)

rollout params "rt Helper Parameters"
(
spinner size "Size:" range:[0, 1e9, 1]
)

 on getDisplayMesh do
 (
  if meshObj == undefined OR size != lastSize then
  (
   
--------------------------------------------------------------- MESH---------------------------------------------------------------------------------
   
local vertsArray = #([-2.5,-2.5,0], [2.5,-2.5,0], [-2.5,2.5,0], [2.5,2.5,0], [-2.5,-2.5,5], [2.5,-2.5,5], [-2.5,2.5,5], [2.5,2.5,5])
local facesArray = #([1,3,4], [4,2,1], [5,6,8], [8,7,5], [1,2,6], [6,5,1], [2,4,8], [8,6,2], [4,3,7], [7,8,4], [3,1,5], [5,7,3])
local edgeVis = #(#(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false), #(true, true, false))

--------------------------------------------------------------------------------------------------------------------------------------------------------
   
   meshObj = mesh vertices:(for v in vertsArray collect v*size) faces:facesArray
   for face = 1 to edgeVis.count do
   for i = 1 to 3 do
   setEdgeVis meshObj face i edgeVis[face][i]   
   theMesh = copy meshObj.mesh
   delete meshObj
   lastSize = size
  )
  theMesh
 )
 
tool create
(
on mousePoint click do
case click of
(
1: nodeTM.translation = gridPoint
2: #stop
)
on mouseMove click do
case click of
(
2: (size = length gridDist)
)
)
)--end

 

I have only tested it with simple geo, aslong as you give each helper a new name and a unique classID then it all works. I will try some more complex geometry next!

g

 

 

Graph's picture

lets get this going

execute "(mesh vertices:#([0,0,51.5526], [-39.0721,-40.7057,0], [39.0721,-40.7057,0], [39.0721,40.7057,0], [-39.0721,40.7057,0], [0,0,0]) faces:#([1,2,3], [1,3,4], [1,4,5], [1,5,2], [2,6,3], [3,6,4], [4,6,5], [5,6,2]));"
evaluate that snippet î to see the result of what is so far..

the current function simply creates you a string that creates a mesh. to use it as code directly use the first line printed (a=createinstance code, mesh = a.mesh .. or something alike); otherwise use the string and execute it
doesnt have smoothing groups, matIDs or tVerts but you wont need that for a helper.

(
	--mesh vertices:<array_of_point3s> faces:<array_of_point3s> --[ materialIDs:<array_of_integers> ] \
 
	fn obj2Code obj = 
	(
		if isValidNode obj do
		(
			local tMesh = copy obj.mesh
			local nVerts = tMesh.numverts
			local nFaces = tMesh.numfaces
 
 
			local verts = for v = 1 to nVerts collect getVert tMesh v
			local faces = for f = 1 to nFaces collect getFace tMesh f
 
 
			local str = "(mesh vertices:"
			with printAllElements true 
			(
				str += verts as string
				str += " faces:"
				str += faces as string
				str += ");"
			)	
			tMesh = undefined
 
			return str
		)
	)--END obj2Code FN
 
	local tString = (obj2Code selection[1])
	format "%\n "tString
	tString
)

have phun

Raphael Steves

gramx's picture

How do you put your code in a

How do you put your code in a nice box like that?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.