Notifications
Clear all

[Closed] Translating data between object classes

For a few of the scripts I’ve been working on recently, I’ve been getting frustrated with constantly having to write multiple versions of the same code to deal with essentially the same data between multiple types of scene nodes. So I’m hoping to put together a library of functions that will allow me (or other users) to control multiple object classes using the same code.

I’ve started with some simple functions to work with sub-object identification and position data for both editable poly and editable spline objects. Nothing too fancy really, but it’s allowed me to do some serious streamlining. I will worry about getting this to work with editable meshes a bit later; for my purposes in the meantime they are easy enough to convert to and from poly objects.

If anyone has any suggestions for improving these functions I would be thrilled to hear them. And if anyone has any functions of their own written in a similar vein, I’d like to make this thread the place for sharing them.

fn numberOfVertices sel theSet:1 =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getNumVerts sel
  		line: numKnots sel theSet
  		SplineShape: numKnots sel theSet
  	)
  )
  fn numberOfSegments sel theSet:1 =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getNumEdges sel 
  		line: numSegments sel theSet
  		SplineShape: numSegments sel theSet
  	)
  )
  fn numberOfPolygons sel =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getNumFaces sel
  		line: numSplines sel
  		SplineShape: numSplines sel
  	)
  )
  
  
  fn selectedVertices sel theSet:1 =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getVertSelection sel as array
  		line: getKnotSelection sel theSet
  		SplineShape: getKnotSelection sel theSet
  	)
  )
  fn selectedSegments sel theSet:1 =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getEdgeSelection sel as array
  		line: getSegSelection sel theSet
  		SplineShape: getSegSelection sel theSet
  	)
  )
  fn selectedPolygons sel =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getFaceSelection sel as array
  		line: getSplineSelection sel
  		SplineShape: getSplineSelection sel
  	)
  )
  
  
  fn setSelectedVertices sel v theSet:1 =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.setVertSelection sel v
  		line: setKnotSelection sel theSet v
  		SplineShape: setKnotSelection sel theSet v
  	)
  )
  fn setSelectedSegments sel s theSet:1 =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.setEdgeSelection sel s
  		line: setSegSelection sel theSet s
  		SplineShape: setSegSelection sel theSet s
  	)
  )
  fn setSelectedPolygons sel p =
  (
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.setFaceSelection sel p
  		line: setSplineSelection sel p
  		SplineShape: setSplineSelection sel p
  	)
  )
  
  
  fn getVerticesFromSegment sel s theSet:1 =
  	case (classOf sel) of
  	(
  		Editable_Poly: polyOp.getEdgeVerts sel s
  		line: #(s, ( mod s (numberOfVertices sel theSet:theSet) + 1 ) as integer) 
  		SplineShape: #(s, ( mod s (numberOfVertices sel theSet:theSet) + 1 ) as integer)
  	)
  fn getVerticesFromPolygon sel p theSet:1 =
  	case (classOf sel) of
  	(
  		Editable_Poly: sort (polyOp.getFaceVerts sel p)
  		line: for v = 1 to (numberOfVertices sel theSet:p) collect v
  		SplineShape: for v = 1 to (numberOfVertices sel theSet:p) collect v
  	)


  fn vertexPosition sel v theSet:1 =
	case (classOf sel) of
	(
		Editable_Poly: polyOp.getVert sel v
		line: getKnotPoint sel theSet v
		SplineShape: getKnotPoint sel theSet v
	)
fn setVertexPosition sel v thePos theSet:1 =
	case (classOf sel) of
	(
		Editable_Poly: polyOp.setVert sel v thePos
		line: setKnotPoint sel theSet v thePos
		SplineShape: setKnotPoint sel theSet v thePos
	)	
  


  -- Sample script utilizing the above functions
  

  fn getVertArrayFromSegments theSet:1 targetArray:vertArray =
  	for s = 1 to (selectedSegments $ theSet:theSet).count do
  	(
  		for i = 1 to 2 do
  		(
  			v = (getVerticesFromSegment $ (selectedSegments $ theSet:theSet)[s])[i]
  			
  			if (for w = 1 to targetArray.count where targetArray[w][1] == v collect w).count == 0 do
  			(
  				append targetArray #(v, vertexPosition $ v theSet:theSet)
  			)
  		)
  	)
  fn getVertArrayFromPolygons theSet:1 targetArray:vertArray =
  	if (superClassOf $ != shape) or (findItem (selectedPolygons $) theSet != 0) do
  	(
  		for p = 1 to (selectedPolygons $).count do
  		(
  			for i = 1 to (getVerticesFromPolygon $ (selectedPolygons $)[p]).count do
  			(
  				v = (getVerticesFromPolygon $ (selectedPolygons $)[p])[i]
  				
  				if (for w = 1 to targetArray.count where targetArray[w][1] == v collect w).count == 0 do
  				(
  					append targetArray #(v, vertexPosition $ v theSet:theSet)
  				)
  			)
  		)
  	)
  
  
  fn vertexOrder a b = case of ( (a[1] < b[1]): -1; (a[1] > b[1]): 1; default: 0 )
  
  
  fn buildVertArray theSet:1 =
  (
  	vertArray = #()
  
  	case subObjectLevel of
  	(
  		0: vertArray = for v = 1 to (numberOfVertices $ theSet:theSet) collect #(v, vertexPosition $ v theSet:theSet)
  
  		1:
  		(
  			theVerts = selectedVertices $ theSet:theSet
  			vertArray = (for v = 1 to theVerts.count collect #(theVerts[v], vertexPosition $ theVerts[v] theSet:theSet))
  		)
  		
  		2:	getVertArrayFromSegments theSet:theSet
  
  		3:	case (classOf $) of
  				(
  					Editable_Poly: getVertArrayFromSegments()
  					line: getVertArrayFromPolygons theSet:theSet
  					SplineShape: getVertArrayFromPolygons theSet:theSet
  				)
  				
  		4:	getVertArrayFromPolygons()
  		5:	getVertArrayFromPolygons()
  	)
  	
  	qSort vertArray vertexOrder
  	format "vertArray (%): %

" theSet vertArray
  )
  
  
  if superClassOf $ == shape then setCount = numberOfPolygons $ else setCount = 1
  for i = 1 to setCount do (buildVertArray theSet:i)
1 Reply

Hi Kevin, my solution isn’t exactly the same as yours. I needed to create a wrapper for Editable Poly Objects and Edit Poly Modifiers functions to write code disregarding the object type I was working with.

Following code is the backbone of the wrapper I use in IC.Shape. Three sample functions are added to get elements count. After structure instantiation and initialization, the same wrapper functions can work either on Editable Poly Objects or Edit Poly Modifiers.

struct PolyWrapper
(
    theNode = undefined,
    theEditObj = undefined,
    theMesh = undefined,

    _baModViewStatus = #{},
    _bNurmsStatus = false,

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

    function pwModViewOff =
    (
        if (isValidNode theNode) do
        (
            _baModViewStatus = #{}

            with redraw off
            (
                if ((classOf theEditObj) == Editable_Poly) then
                (
                    for i = 1 to theNode.modifiers.count do
                    (
                        if ( (theNode.modifiers[i].enabled == true) and (theNode.modifiers[i].enabledInViews == true) ) do
                        (
                            _baModViewStatus[i] = true
                            theNode.modifiers[i].enabledInViews = false
                        )
                    )

                    if (isProperty theNode #surfSubdivide) do
                    (
                        _bNurmsStatus = theNode.baseObject.surfSubdivide
                        theNode.baseObject.surfSubdivide = false
                    )
                )
                else if ((classOf theEditObj) == Edit_Poly) then
                (
                    local iModIndex = modPanel.getModifierIndex theNode theEditObj

                    for i = 1 to (iModIndex-1) do
                    (
                        if ( (theNode.modifiers[i].enabled == true) and (theNode.modifiers[i].enabledInViews == true) ) do
                        (
                            _baModViewStatus[i] = true
                            theNode.modifiers[i].enabledInViews = false
                        )
                    )
                )
            )
        )
    ),

    function pwModViewOn =
    (
        if (isValidNode theNode) do
        (
            with redraw off
            (
                for i = 1 to theNode.modifiers.count do
                (
                    if ( (theNode.modifiers[i].enabled == true) and (_baModViewStatus[i] == true) ) do
                    (
                        theNode.modifiers[i].enabledInViews = true
                    )
                )

                if ((classOf theEditObj) == Editable_Poly) do
                (
                    if (isProperty theNode #surfSubdivide) do
                    (
                        theNode.baseObject.surfSubdivide = _bNurmsStatus

                        _bNurmsStatus = false
                    )
                )
            )

            _baModViewStatus = #{}
        )
    ),

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

    function initStruct initNode initEditObj =
    (
        theEditObj = initEditObj

        if ( (theEditObj != undefined) and ((superClassOf theEditObj) == Modifier) ) do
        (
            if ((findItem initNode.modifiers theEditObj) == 0) do
            (
                select initNode
                theEditObj = modPanel.getCurrentObject()
            )
        )

        if ( ((classOf theEditObj) == Editable_Poly) or ((classOf theEditObj) == Edit_Poly) ) then
        (
            theNode = initNode

            pwModViewOff()
            theMesh = theNode.mesh
            pwModViewOn()
        )
        else
        (
            theEditObj = undefined
            theNode = undefined
            theMesh = undefined
        )
    ),

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

    function getNumVerts =
    (
        local iNumVerts = 0

        if ((classOf theEditObj) == Editable_Poly) then
            iNumVerts = polyOp.getNumVerts theEditObj
        else if ((classOf theEditObj) == Edit_Poly) then
            iNumVerts = theEditObj.getNumVertices node:theNode

        return iNumVerts
    ),

    function getNumEdges =
    (
        local iNumEdges = 0

        if ((classOf theEditObj) == Editable_Poly) then
            iNumEdges = polyOp.getNumEdges theEditObj
        else if ((classOf theEditObj) == Edit_Poly) then
            iNumEdges = theEditObj.getNumEdges node:theNode

        return iNumEdges
    ),

    function getNumFaces =
    (
        local iNumFaces = 0

        if ((classOf theEditObj) == Editable_Poly) then
            iNumFaces = polyOp.getNumFaces theEditObj
        else if ((classOf theEditObj) == Edit_Poly) then
            iNumFaces = theEditObj.getNumFaces node:theNode

        return iNumFaces
    )

) -- end struct PolyWrapper

--------------------------------------------------------------------------------
-- Example

(
    -- Instance the PolyWrapper structure
    local pw = PolyWrapper()

    -- Create a Sphere and convert it to an Editable Poly
    local theSphere = convertToPoly (Sphere())

    -- Create an instance of an Edit Poly Modifier and assign it to the Sphere
    local theMod = EditPolyMod()
    addModifier theSphere theMod

    -- Set Command Panel to Modify mode and select the Sphere
    setCommandPanelTaskMode #Modify
    select theSphere

    -- Standard Edit Poly Modifier operations to modify the Sphere topology
    theMod.setEPolySelLevel #Vertex
    theMod.select #Vertex #{1..33} select:true node:theSphere
    theMod.ButtonOp #RemoveVertex

    -- Set the Editable Poly as active in Modifier Stack (get a Warning)
    modPanel.setCurrentObject theSphere.baseObject
    -- Init the PolyWrapper to the Current Active Edit Object in the Stack
    pw.initStruct theSphere (modPanel.getCurrentObject())
    -- Call the PolyWrapper Methods
    format "The Editable Poly has % verts, % edges, % faces
" (pw.getNumVerts()) (pw.getNumEdges()) (pw.getNumFaces())

    -- Set the Edit Poly Modifier as active in Modifier Stack
    modPanel.setCurrentObject theSphere.modifiers[1]
    -- Init the PolyWrapper to the Current Active Edit Object in the Stack
    pw.initStruct theSphere (modPanel.getCurrentObject())
    -- Call the PolyWrapper Methods
    format "The Edit Poly Modifier has % verts, % edges, % faces
" (pw.getNumVerts()) (pw.getNumEdges()) (pw.getNumFaces())
)
  • Enrico