ZBrushCentral

Memory Blocks

Thank you,Hello, I am not able to master the grammar of the memory memory. Can you give me a simple example for ordering?Snipaste_2020-12-30_16-00-29

Can you explain more what you want to do?

Hello, learning from your reply, I just don’t know how to use this grammar and what it does! But I feel I need it very much. I have learned variables and arrays, but I don’t understand this.

you should take a look at the samples provided with the zfileutils.

Memory blocks are useful because they persist for the whole of a ZBrush session, whereas ZScript variables are reset every time the plugin is reloaded. As only one zscript plugin or macro can be loaded at a time, reloading can happen often.

Memory blocks are also necessary if you want to load a file into memory and examine or manipulate its data.

When naming memory blocks try to use a name which is unlikely to be chosen by someone else, to reduce the chance of conflicts. For example, I tend to use “MC_” followed by the name of the plugin.

The simplest way to create and use a memory block is by using the [MVarDef],[MVarSet] and [MVarGet] commands. These are very similar to [VarDef],[VarGet] and [VarSet].

[MVarDef,MC_ExampleMem,9,0]//creates a memblock called "MC_ExampleMem", storing 9 values, each initialized to 0

When using [MVarDef], the values are stored as floating point numbers with each value taking 4 bytes of memory, so the “MC_ExampleMem” memblock will be 36 bytes in size.

You can get the size in bytes using the [MemGetSize] command. And you can use this command to test if the memblock exists:

[If,[MemGetSize,MC_ExampleMem],//memory block size > 0 so it exists
	[Note,[StrMerge,"Memory block has ",[MemGetSize,MC_ExampleMem]," bytes"]]
,//else no memblock
	[Note,"Memory block does not exist!"]
]

Having created a memory block we can use it to store values. For example, there’s a special command for storing the current transform values:

[MTransformGet,MC_ExampleMem,0]//stores the current transform values in the "MC_ExampleMem" memblock

(Note that you need room for 9 values when using this command but that’s OK, we created our memblock with 9 values.)

After creating a memblock and getting the transform values, this code will move the model to the center of the canvas:

[VarSet,docWcenter,[IGet,Document:Width]/2]
[VarSet,docHcenter,[IGet,Document:Height]/2]
[MVarSet,MC_ExampleMem,0,docWcenter]
[MVarSet,MC_ExampleMem,1,docHcenter]
[MTransformSet,MC_ExampleMem,0]

The values can be copied to another memblock if required:

    [If,[MemGetSize,MC_ExampleMem] == 36,//memblock exists and has 36 bytes
		[MemCreate,MC_DuplicateMem,36,0]//creates memblock of 36 bytes (for 9 floats)
		[MemCopy,MC_ExampleMem,0,MC_DuplicateMem,0]//copies data from MC_ExampleMem
	]

Complete examples of the code above in the attachment.
Memblocks_examples.txt (1.6 KB)

-Marcus

1 Like

Thank you very much for your answers, you are too patient, thank you. May I ask you another question, how do I calculate the volume of the model in zbrush? Is there a formula to calculate it?Snipaste_2021-01-02_11-41-11

Yes, as Nicolas has said in your other post, there are commands for this that aren’t in the online documentation:

[IButton,"Analyze SubTool","Get Area and Volume of selected SubTool",
	[VarSet,meshIsWatertight,[IsPolyMesh3DSolid]]
	[If,meshIsWatertight,	
		[Note,"\CFFFFFFMesh is \CFF9000Watertight\n",,-1]
		[VarSet,meshVolume,[GetPolyMesh3DVolume]]	
		[Note,[StrMerge,"\CFFFFFFMesh Volume is \CFF9000",meshVolume,"\n"],,-1]
		,
		[Note,"\CFFFFFFMesh is \CFF9000Not Watertight\n",,-1]
		[Note,"\CFFFFFFMesh Volume is \CFF9000N/A\n",,-1]
	]
	[VarSet,meshArea,[GetPolyMesh3DArea]]
	[Note,[StrMerge,"\CFFFFFFMesh Area is \CFF9000",meshArea,"\n"]]
]

-Marcus

Thank you very much, so the function of the memory block is to store multiple variables and use them together, right?

Yes, that is one of the functions of memory blocks but they have other uses. For example, you could write lines of text to a memory block and then save the memory block as a file.
But you can write complex plugins without ever using a memory block so don’t worry too much about them.

thank you very much

Hello, hello teacher, can zbrush create curves and points similar to rhino? Or is it possible to implement it using a plug-in?

I’ve answered this question in your other post. It’s better to start a new thread for each different question because then it is easier for other people who are searching to find the answers.