Thursday, August 10, 2023

ZBrush zscript doodle reading a text file

i'm new to zscript in ZBrush but it seems like square brackets are mostly functions.  first thing in brackets is like function name.  after first comma are command arguments. (if's and loops are kindof like exceptions as those aren't functions)

here are a couple doodles printing a note, importing an obj, and reading a very simple text file. (there might be bugs so please use/modify at your own risk)

Thanks for looking


[IButton,Click,"helper hint",
	[Note, "a note"]
]//end button


//print path want to import
[IButton,ShowPath,"",
	[VarDef, fPath, ""]
	[VarSet, fPath, [FileNameAsk,"OBJ(*.obj)",,"Select File Hint"] ]
	[Note, fPath]
]//end button


//import an obj
[IButton,ImportPath,"",
	[VarDef, fPath, ""]
	[VarSet, fPath, [FileNameAsk,"OBJ(*.obj)",,"Select File Hint"] ]
	[If, [StrLength, fPath],
		[FileNameSetNext, fPath]
		[IPress, Tool:Import]	
	
	]//end if fpath
]//end button



//print all lines of a text file - possible objs to import
[IButton,PrintPaths,"",
	[VarSet, offset, 0] //used for traversing memory
	[VarSet, bytesRead, 0]
	[VarSet, lineText, ""] //will hold a single model file path
	[VarSet, allLinesText, ""] //will hold all file paths
	[VarSet, modelsFile, [FileNameAsk, "TXT(*.txt)",,"Please select text file one line per model"] ]
	[MemDelete, TextMem]
	[MemCreateFromFile, TextMem, modelsFile]
	[VarSet, txtMemSize, [MemGetSize, TextMem] ]
	//[Note, txtMemSize]
	[Loop, 10000,
		[If, offset < (txtMemSize-1),
			[VarSet, bytesread, [MemReadString, TextMem, lineText, offset, 1] ]
			[VarSet, offset, offset+bytesRead]
			//[Note, lineText]
			[VarSet, allLinesText, [StrMerge, allLinesText, lineText, "\n"] ]
			,
			[LoopExit]
		]//end if offset
	]//end loop
	[MemDelete, TextMem]
	[Note, allLinesText] //show all model paths
]//end print path button



//inspired by
//https://www.zbrushcentral.com/t/question-how-to-use-zscript-to-load-an-obj-from-a-file-path/419867
//https://github.com/NWoolridge/NeuronBuild/blob/master/NeuronBuild_Zbrush_1.2