Save Object reference in create plug in

Save Object reference in create plug in

Anonymous
Not applicable
765 Views
3 Replies
Message 1 of 4

Save Object reference in create plug in

Anonymous
Not applicable

We have picked an object(or more then one object) in a scene, we do some stuff with it in visualize this on the screen.

It works fine but when I save the scene and I open this scene again my new created object is empty. The picked objects are dispeared out of the list.

I have this problem also with making a copy the new created object in the same scene.

 

Is it possible to save those items too and have it in a stack somewhere ?

 

 

plugin simpleObject ActiveElements
name:"ActiveElements"
category:"ForumQuestion"
classID:#(0x1cbe0a0a, 0x277fe895)
(
	local objs=#()	
	fn validGeometry node = (iskindof node GeometryClass) 
	
	
	parameters main rollout:main 
	(
		targets type:#nodeTab tabSizeVariable:true

	)
	
	rollout main "Parameters"
	(
		group "Objects: "
		(
			listBox ObjList  items:(for o in objs collect o.name) height:5
			pickbutton pickobject_bt "Pick Object" autoDisplay:off filter:validGeometry width:144 align:#right  
		)
		
		on pickobject_bt picked obj do
		(
			if (findItem objs obj)<1 then
				(
					append objs obj
					targets =objs
					ObjList.items = for i in objs collect i.name
				)
		)
			
		on params open do
		(
			updateList()
		)	
	)
		
		fn transformMesh theMesh theTM=
		(
			for v=1 to theMesh.numverts do
				(
					setVert theMesh v ((getVert theMesh v)*theTM)
				)
			theMesh
		)
	
		fn _attachMeshes meshes = 
		(
			n = meshes.count
			while (num = n/2) > 0 do for k=1 to num do 
			(
				meshop.attach meshes[k] meshes[n]
				n -= 1
			)
			meshes[1]
		)
	
		on buildMesh do
		(
			objMesh=#()
			temp=#()
			if objs.count>0 then
			(
				for o=1 to objs.count do
				(
					objectmesh=objs[o].mesh
					TM= matrix3 1 
					
					TM =(matrix3 [1,0,0][0,1,0][0,0,1][trx=(o*10),0,0])
					
					temp[o]= (transformMesh objectmesh TM)
					append objMesh (_attachMeshes temp)
				)
				setMesh mesh (_attachMeshes objMesh)
				)
		
	)--buildmesh
		
	tool create
		(
		on mousePoint click do
			(
				case click of
				(
						1: nodeTM.translation = gridPoint
						2:#stop
				)
			)	
			
		)--end create

)-- end plugin
0 Likes
Accepted solutions (1)
766 Views
3 Replies
Replies (3)
Message 2 of 4

Swordslayer
Advisor
Advisor
Accepted solution

That example code has quite a few problems, like parameters and rollout sharing the same name, calling undeclared updateList function, calling attach on each iteration (why??), and of course using local objs variable that will never get filled with any nodes unless you add some objects in that session, and it's not needed at all anyway:

 

plugin simpleObject ActiveElements
name:"ActiveElements"
category:"ForumQuestion"
classID:#(0x1cbe0a0a, 0x277fe895)
(
	fn isGeometry node = isKindOf node GeometryClass and not isKindOf node TargetObject
	fn updateList = this.params.mlbObjList.items = for obj in this.targets collect obj.name
	
	parameters main rollout:params
	(
		targets type:#nodeTab tabSizeVariable:true
	)
	
	rollout params "Parameters"
	(
		group "Objects: "
		(
			multiListBox mlbObjList height:5
			pickButton pbPick "Pick Object" filter:isGeometry width:144 align:#right  
		)
		
		on pbPick picked obj do
		(
			appendIfUnique targets obj
			updateList()
		)
			
		on params open do updateList()
	)
		
	fn transformMesh theMesh theTM =
	(
		(#() + theMesh.vertices).pos *= theTM
		theMesh
	)

	fn _attachMeshes meshes = 
	(
		n = meshes.count
		while (num = n/2) > 0 do for k = 1 to num do 
		(
			meshop.attach meshes[k] meshes[n]
			n -= 1
		)
		meshes[1]
	)

	on buildMesh do if targets.count > 0 do
	(
		local meshes = for o = 1 to targets.count collect
			transformMesh targets[o].mesh (transMatrix [10 * o, 0, 0])

		setMesh mesh (_attachMeshes meshes)
	)
		
	tool create numPoints:1
	(
		on mousePoint click do
			nodeTM.translation = gridPoint
	)
)
0 Likes
Message 3 of 4

Anonymous
Not applicable

Swordslayer,

 

What a great advice and this nice codewriting. Your code is working great! 

I read already some codesamples from you and it has some sense from awesomness!!

 

I took my part of code out of a larger code and try to simplify it a bit for this forumquestion.

Probably that's why the updatelist-function wasn't there and some other strange things.

 

I didn't know that targets already could have a collection that's why I try this with the local objs-array.

 

Maybe this question is not related with my question here, but I really would appreciate it if  you would explain me:

 

(#() + theMesh.vertices).pos *= theTM

1.

Sometimes I have other complete TM (4x3) is there also a way to describe this in one line as above. (I think this just changed the 4th row)

Is there any help I could find about the syntax from this line of code. what about #()?

 

2.

I see you are using a 'for loop' without brackets are there any advantages?

and where does the for-loop returns?

 

Indeed the attach in the loop was a big mistake, I even don't know how this could happen. 😄

 

3.

Do you have an explanation why it saves the objects from the targets or why it's possible you can take a copy of this new object? (or why it didn't do with my code)

 

Thanks in advance

0 Likes
Message 4 of 4

Swordslayer
Advisor
Advisor

Maybe this question is not related with my question here, but I really would appreciate it if  you would explain me:

 

(#() + theMesh.vertices).pos *= theTM

That's multiplying all the vertex positions by the transformation matrix. Since VertexSelection values don't support mapped access, I'm converting it to an array which does support it.


Sometimes I have other complete TM (4x3) is there also a way to describe this in one line as above. (I think this just changed the 4th row)

Is there any help I could find about the syntax from this line of code. what about #()?

 


That's a complete matrix. The positions are multiplied by the matrix, it's not just that a vector would be added (though you can do that as well).


I see you are using a 'for loop' without brackets are there any advantages?

and where does the for-loop returns?

 


Style choice only, when it's not multiple lines, you don't need brackets.


Do you have an explanation why it saves the objects from the targets or why it's possible you can take a copy of this new object? (or why it didn't do with my code)


As I mentioned in the 1st reply, you were using local variable objs which was only initialized to an empty array on scene open. Locals can be tricky, especially when the object is copied, unless necessary (or if you know what you're getting yourself into), I'd avoid using them.