Code snippet for 2.9

i started modifying this code snippet for error message
but still errors
can someone help debug this
and work in either 2.8 / 2.9 latest version



"""


error message 



code snippet 


https://web.archive.org/web/20120824033549/http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Interface#A_popup_dialog


"""


import bpy
from bpy.props import *
 
#
#   The error message operator. When invoked, pops up a dialog 
#   window with the given message.   
#


class MessageOperator(bpy.types.Operator):

	bl_idname = "error.message"
	bl_label = "Message"
	type = StringProperty()
	message = StringProperty()
 
	def execute(self, context):
	
		self.report({'INFO'}, self.message)
		print(self.message)
		return {'FINISHED'}
 
	def invoke(self, context, event):
	
		wm = context.window_manager
		return wm.invoke_popup(self, width=400, height=200)
 
	def draw(self, context):
	
		self.layout.label("A message has arrived")
		row = self.layout.split(0.25)
		row.prop(self, "type")
		row.prop(self, "message")
		row = self.layout.split(0.80)
		row.label("") 
		row.operator("error.ok")
 
#
#   The OK button in the error dialog
#
class OkOperator(bpy.types.Operator):

	bl_idname = "error.ok"
	bl_label = "OK"
	def execute(self, context):
		return {'FINISHED'}
 
#
#   Opens a file select dialog and starts scanning the selected file.
#
class ScanFileOperator(bpy.types.Operator):

	bl_idname = "error.scan_file"
	bl_label = "Scan file for return"
	filepath = bpy.props.StringProperty(subtype="FILE_PATH")
 
 
 
	def execute(self, context):
	
		scanFile(self.filepath)
		return {'FINISHED'}
 
	def invoke(self, context, event):
	
		context.window_manager.fileselect_add(self)
		return {'RUNNING_MODAL'}
 
#
#   Scan the file. If a line contains the word "return", invoke error 
#   dialog and quit. If reached end of file, display another message.
#



def scanFile(filepath):

	fp = open(filepath, "rU")
	n = 1
	
	for line in fp:
	
		words = line.split()
		
		if "return" in words:
		
			bpy.ops.error.message('INVOKE_DEFAULT', 
				type = "Error",
				message = 'Found "return" on line %d' % n)
				
			return
			
		n += 1
		
	fp.close()
	
	bpy.ops.error.message('INVOKE_DEFAULT', 
		type = "Message",
		message = "No errors found in %d lines" % n)  
		  
	return
 
 
 
#
#   Registration


	
classes = (
	MessageOperator ,
	OkOperator ,
	ScanFileOperator 
	
)
	
	
	
	
	
###
	
def register():
	
#    bpy.utils.register_class(HelloWorldPanel)
	
	
	from bpy.utils import register_class
	
	for cls in classes:
		register_class(cls) 
	

	
	
	
	
###
	
	
def unregister():
	
#    bpy.utils.unregister_class(HelloWorldPanel)
	
	from bpy.utils import unregister_class
	
	for cls in reversed(classes):
		unregister_class(cls)
	
	
	
###
	
	
if __name__ == "__main__":
	register()
	
	
	
	

thanks
happy bl

I loaded it in notepad, had to edit it a lot because I could not just copy-paste it in and run it, to many indentation errors. Got that taken care of, then when I ran it, I got this error:

Traceback (most recent call last):
  File "<blender_console>", line 2, in <module>
  File "<blender_console>", line 2, in register
NameError: name 'HelloWorldPanel' is not defined

To fact, there exists not such “HelloWorldPanel” . . . .

i think i miss part of the code in another place then this code snippet

but i finally was able to get the other parts and make it work
was not easy but now seems ok

will continue to learn more about 2.9
i wish there was more example in API
that would help a lot

but thanks for feedback
happy bl

I do not use the API very often at all, and I find it to be practically worthless without code examples.
I have tutorials that will teach you how to use the console rather than the API.

https://www.youtube.com/playlist?list=PLPssISxU9GZLU3qYbBPUgDNa3lSv2rSE6

or on my website:
https://www.archeuslore.com/blender/blender.html

You will probably be the same way after you see the power of using the console instead.

thanks will have a look

i did find some other site with examples
and slowly learning more

happy cl

You will learn enough from these tutorials to be a lot faster at coding, and probably a bit more dangerous for a while, but once it all comes together, you will be able to manage doing practically anything you want to do in Blender.

i might need some new examples
for certain functions with Bmesh for doing new primitives

i did a few but some of the Bmesh methods are not simple to do!
i will try to contact you on your site

note:

it would be nice to have a thread here showing many examples for 2.9
so other people can come and learn from it

thanks for feedback

1 Like