1
$\begingroup$

I start to learn Python and Blender. But can't figure out what I do bad here. The code is :


bl_info = {
    "name" : "HELLO WORLD",
    "author" : "Author",
    "blender" : (2, 80, 0), "version" : (0, 0, 1),
}

import bpy

class LODP_CM_OT_set_cam ( bpy.types.Operator ) : 
    bl_idname       = "lodp_cm.set_cam"
    bl_label        = "Set camera brush"

    def execute( self : bpy.types.Operator , context : bpy.types.Context ) : 
        obj  = bpy.context.active_object

        bpy.context.view_layer.objects.active = None
        bpy.context.view_layer.objects.active = obj

        mod : bpy.types.DynamicPaintModifier = obj.modifiers.new( "Dynamic Paint", "DYNAMIC_PAINT" )
        mod.ui_type = "BRUSH"
        
        # CRITICAL ERROR -  EXCEPTION_ACCESS_VIOLATION - 0x00007FF73793DDB0
        bpy.ops.dpaint.type_toggle( type="BRUSH" ) 
        # CRITICAL ERROR -  EXCEPTION_ACCESS_VIOLATION - 0x00007FF73793DDB0

        return {"FINISHED"}
# end class LODP_CM_OT_set_cam

class LODP_CM_PT_main ( bpy.types.Panel ) : 
    bl_label        = "HELLO WORLD PANEL"
    bl_idname       = "LODP_PT_main"
    bl_space_type   = 'PROPERTIES'
    bl_region_type  = 'WINDOW'
    bl_context      = "scene"

    def draw(self, context):
        layout = self.layout
        layout.operator( LODP_CM_OT_set_cam.bl_idname )
#

classes = [ LODP_CM_OT_set_cam, LODP_CM_PT_main, ]

def register():
    global classes
    for klass in classes : bpy.utils.register_class( klass )
#
def unregister():
    global classes
    for klass in classes : bpy.utils.unregister_class( klass )
    classes.clear()
#
if not __package__ : register()

representative gif :

enter image description here

After trying to get the code of the error or something, get : EXCEPTION_ACCESS_VIOLATION - 0x00007FF73793DDB0, exit code 11

Is a mistake, bug, bad syntaxis, another thing? , but since I'm a newcomer, I have my doubts.

$\endgroup$
1
  • $\begingroup$ For one, don't use boy.context in an operator. Ude the context you receive in the context parameter. $\endgroup$ – dr. Sybren Feb 21 at 5:45
3
$\begingroup$

Is it as simple as it could be?.

A TL;DR comment turned into answer:

Doesn't crash for me... but does throw errors. Some of the issues as I see them.

Sure it's a simple little addon, but.. appears to be some added and IMO unnecessary complexity added too. (bling).

Is there really any need to type hint the usual suspects and familiar arguments of self, context ?

def execute( self : bpy.types.Operator , context : bpy.types.Context )

Let's look at Text Editor > Templates > Python > Operator Simple and make a small change

def execute(self, context):
    print(type(self))
    main(context)
    return {'FINISHED'}

output

<class '__main__.SimpleOperator'>
<bpy_struct, Object("Armature") at 0x7f1e0c715008>

ie the operator type is not an instance of bpy.types.Operator

Any need to toggle the active object?

There is no need to toggle the active object with

context.view_layer.objects.active = None
context.view_layer.objects.active = obj

and since context is an argument of method there is never any need to see bpy.context

Dynamic Paint Modifier.

Once a dynamic paint modifier is added to an object, it cannot be added again. If you try and do so again it will not add a modifier and return None. (NoneType) is not an instance of bpy.types.DynamicPaintModifier

Main thread.

Any benefit in using if not __package__ vs if __name__ == "__main__" ? Will it always equate?

$\endgroup$
1
  • $\begingroup$ mmm since it is a development-learning environment, in order to enable vscode autocompletion I had to put something. What you say is very good ... even so. without removing all the tracks, nor the unnecessary code. changing the order ... always crash. I already download another version of blender. Likewise. tested on clean installation of 2.91.1 st / 2.91.2 st / 2.83 lts $\endgroup$ – user70587 Feb 19 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.