Advertisement
Lucas_3D

Untitled

Jan 25th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. --defined by a tri-mesh and simpleObject plugin is similar to all the Standard and Extended primitives
  2. -- numTimesBuilt = 0 --I'm interested in how many times this is happening
  3. -- numTimesRot = 0
  4. plugin simpleObject helperSample --the 'scripted simpleObject' is it's own type of plugin, simpleObject is the superclass, it creates an object that whose superclass is GeometryClass
  5. name:"HelperSample"
  6. category:"HowTo"
  7. classID:#(0x453e608, 0x40dda998)
  8. --usePBValidity:on --this is for when you are extending an existing class, default is false and does not include the validity of the scripted controllers - whatever that means
  9. (
  10. parameters main rollout:params --can add rollout here
  11. (
  12. p1 type:#maxObject
  13. p2 type:#maxObject
  14. p3 type:#maxObject
  15. stairWidth type:#worldUnits ui:spnStairWidth default:5.0 --the height param, it won't be visible until you run the rollout params below
  16. )
  17.  
  18. rollout params "Parameters"
  19. (
  20. spinner spnStairWidth "Stair Width" range:[-100.0,100.0,5.0] type:#worldunits
  21. --
  22. on spnStairWidth changed theVal do
  23. (
  24. p2UnitVector = p2.node.transform[1]
  25. rotatedMatrix = rotateZMatrix 90
  26. rotatedVector = p2UnitVector * rotatedMatrix
  27. p3.node.pos = p2.node.pos + (rotatedVector * this.stairWidth)
  28. )
  29. )
  30.  
  31. fn isValid= isValidObj p1 and isValidObj p2 and isValidObj p3 --make sure the correct objects exist
  32.  
  33. fn rot=
  34. (
  35. --numTimesRot += 1
  36. --p2 orientation
  37. --local owner = refs.dependentNodes this firstOnly:on --this is the object being created.
  38. --owner.pivot = p1.node.pos --ERROR this creates a recursive operation!
  39. p2Vec = [p1.node.pos.x, p1.node.pos.y, 0] - [p2.node.pos.x, p2.node.pos.y, 0] --the vector is just the xy to the xy, the z will be equal. minus points away, plus points towards
  40. p2Angle = atan2 p2Vec.y p2Vec.x
  41. p2Matrix = rotate (matrix3 1) (eulerangles 0.0 0.0 p2Angle as quat)
  42. scale p2Matrix p2.node.scale
  43. translate p2Matrix p2.node.pos
  44. p2.node.transform = p2Matrix
  45. --p3 orientation
  46. p3Vec = [p2.node.pos.x, p2.node.pos.y, 0] - [p3.node.pos.x, p3.node.pos.y, 0] --the vector is just the xy to the xy, the z will be equal. minus points away, plus points towards
  47. p3Angle = atan2 p3Vec.y p3Vec.x
  48. p3Matrix = rotate (matrix3 1) (eulerangles 0.0 0.0 p3Angle as quat)
  49. scale p3Matrix p3.node.scale
  50. translate p3Matrix p3.node.pos
  51. p3.node.transform = p3Matrix
  52. )
  53.  
  54. on load do when transform #(p1, p2, p3) changes id:#forceUpdate do notifyDependents this
  55.  
  56. on buildMesh do if isValid() do --I love quickly stacking up a few conditionals in this way.
  57. (
  58. /*
  59. when p1 moves the build is not taking it's position into account...
  60. */
  61. --numTimesBuilt += 1
  62. --print "BUILD MESH!!"
  63. local owner = refs.dependentNodes this firstOnly:on --this is the object being created.
  64. --owner.pivot = [0,0,0]--p1.node.pos --ERROR this creates a recursive operation!
  65. /*
  66. try using p1.node.posInParent, there is a p3.node.pos * inverse owner.transform being used too...that's interesting.
  67. */
  68. lDir = [p2.node.pos.x, p2.node.pos.y, 0] - [p1.node.pos.x, p1.node.pos.y, 0] --length direction
  69. lUVec = normalize lDir
  70. wDir = [p3.node.pos.x, p3.node.pos.y, 0] - [p2.node.pos.x, p2.node.pos.y, 0]--p3.pos - p2.pos --width direction in x and y only
  71. wUVec = normalize wDir
  72. totalStairLength = distance [p1.node.pos.x,p1.node.pos.y] [p2.node.pos.x,p2.node.pos.y]
  73. amountOfStairs = (totalStairLength / 1) as integer
  74. stepLength = 1--totalStairLength / amountOfStairs
  75. totalStairDrop = p2.node.pos.z - p1.node.pos.z
  76. dropDist = totalStairDrop / amountOfStairs
  77. stepWidth = distance [p2.node.pos.x,p2.node.pos.y] [p3.node.pos.x,p3.node.pos.y]
  78.  
  79. fn stairVec _pnt3=
  80. (
  81. --why do I have to set these variables again as local?
  82. local lDir = [p2.node.pos.x, p2.node.pos.y, 0] - [p1.node.pos.x, p1.node.pos.y, 0] --length direction
  83. local lUVec = normalize lDir
  84. local wDir = [p3.node.pos.x, p3.node.pos.y, 0] - [p2.node.pos.x, p2.node.pos.y, 0] --width direction
  85. local wUVec = normalize wDir
  86. --
  87. --width,length,drop
  88. local x = wUVec * _pnt3[1] --width
  89. local y = lUVec * _pnt3[2]--length
  90. local z = _pnt3[3] --drop
  91. return (x + y + [0,0,z])
  92. )
  93.  
  94. vert_array =#()
  95. face_array =#()
  96. vert_count = 0
  97. vertPos = undefined
  98. for i = 1 to amountOfStairs do
  99. (
  100. if i == 1 then
  101. (
  102. vertPos = [0,0,0]--p1.node.pos
  103. --6 verts, the first 2 verts are created uniquely, after that the final 2 are shared for the next step, so there'll only be 4 verts.
  104. append vert_array vertPos
  105. append vert_array (vertPos + (stairVec [stepWidth,0,0]))
  106. append vert_array (vertPos + (stairVec [stepWidth,stepLength,0]))
  107. append vert_array (vertPos + (stairVec [0,stepLength,0]))
  108. append vert_array (vertPos + (stairVec [stepWidth,stepLength,dropDist]))
  109. append vert_array (vertPos + (stairVec [0,stepLength,dropDist]))
  110. --4 faces
  111. append face_array [vert_count+1, vert_count+2, vert_count+3]
  112. append face_array [vert_count+1, vert_count+3, vert_count+4]
  113. append face_array [vert_count+4, vert_count+3, vert_count+5]
  114. append face_array [vert_count+4, vert_count+5, vert_count+6]
  115. vert_count += 6
  116. ) else
  117. (
  118. --4 verts
  119. append vert_array (vertPos + (stairVec [stepWidth,stepLength,0]))
  120. append vert_array (vertPos + (stairVec [0,stepLength,0]))
  121. append vert_array (vertPos + (stairVec [stepWidth,stepLength,dropDist]))
  122. append vert_array (vertPos + (stairVec [0,stepLength,dropDist]))
  123. --4 faces
  124. append face_array [vert_count,vert_count-1,vert_count+1]
  125. append face_array [vert_count,vert_count+1,vert_count+2]
  126. append face_array [vert_count+2,vert_count+3,vert_count+4]
  127. append face_array [vert_count+2,vert_count+1,vert_count+3]
  128.  
  129. vert_count += 4
  130. )
  131. vertPos = vert_array[vert_array.count]
  132. )
  133. setMesh mesh verts:vert_array faces:face_array smGroup:0
  134. for i = 1 to (getNumFaces mesh) do
  135. (
  136. setFaceSmoothGroup mesh i 0
  137. )
  138.  
  139. )--end buildMesh
  140.  
  141. tool create numPoints:2 -- the 3 points are: first click, drag and unclick I believe?...
  142. (
  143. local p3Pos
  144. on mousePoint click do if click == 1 do --didn't need to split this across lines or parenthesis.
  145. (
  146. --print "click 1 - first point placed!"
  147. p3Pos = undefined --at the first click, p3's position is reset here and the scope is set for use.
  148. nodeTM.translation = gridPoint --set the simple object (node) to the first position clicked.
  149. local owner = refs.dependentNodes this firstOnly:on --this is the object being created.
  150. --print ("click owner: "+owner as string)
  151. --print (refs.dependentNodes this) --returns: $helperSample:helperSample001 @ [0.000000,0.000000,0.000000]
  152. print (refs.dependentNodes this firstOnly:on) --returns: $helperSample:helperSample002 @ [0.000000,0.000000,0.000000]
  153. --the following are created
  154. p1 = NodeTransformMonitor node:(Point prefix:"p1-" parent:owner showLinks:on size:10 cross:off box:on constantscreensize:on wirecolor:yellow) --1st pnt
  155. p2 = NodeTransformMonitor node:(Point prefix:"p2-" parent:p1.node showLinks:on size:10 cross:off box:on constantscreensize:on wirecolor:yellow) --2nd pnt, a child of the first
  156. p3 = NodeTransformMonitor node:(Point prefix:"p3-" parent:p2.node showLinks:on size:10 cross:off box:on constantscreensize:on wirecolor:yellow) --3rd point
  157.  
  158. p1.node.pos = owner.position
  159.  
  160. --NodeTransformMonitor is an object that holds a reference to a node. I think it will stop recursion in the call back.
  161.  
  162. when transform #(p1, p2, p3) changes id:#forceUpdate do notifyDependents this --executes the forceUpdate on this object, it is the buildMesh operation
  163. when transform #(p2, p3) changes id:#stairsPointDirection do rot() --orients the p2 and p3 correctly
  164. )
  165. on mouseMove click do if click == 2 and lbutton do
  166. (
  167. --2nd action, 1st unclick, worldPoint is the unclick point
  168. --print "drag 1"
  169. p2.node.pos = worldPoint
  170. p2UnitVector = p2.node.transform[1]
  171. rotatedMatrix = rotateZMatrix 90
  172. rotatedVector = p2UnitVector * rotatedMatrix
  173. p3.node.pos = p2.node.pos + (rotatedVector * this.stairWidth)
  174. )
  175. )
  176. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement