3
$\begingroup$

I know that Segmentation Fault is some sort of memory error having to do with C and C++ - beyond that I don't really know how to handle it. I read up on this error through this post on stackoverflow, and this post on multi-file registering/unregistering but still don't have much of a clue.

I am experimenting with custom properties, sliders and operators. Everything as far as I can tell is accessing what it is supposed to (as in, I adjust the slider and the appropriate object's value is updated) but I am getting a bajillion KeyError's and then Blender crashes with a segfault.

Code

# Functions/Callbacks

    def get_rotate_cam(self):
        return self["Rotate Camera"]
    
    def set_rotate_cam(self, value):
        self["Rotate Camera"] = value
    
    def update_rotate_cam(self, context):
        for obj in bpy.context.selected_objects:
            bpy.ops.opject.select_pattern(pattern="Camera")
            obj.rotation_euler[0] = get_rotate_cam_x(self)

#Property Group

    class SliderProperties(bpy.types.PropertyGroup):
        bpy.types.Scene.rotCam = FloatProperty(
            name="Rotate Camera",
            description="Rotate the camera with the slider"
            default=0,
            min=0,
            max=360,
            update=update_rotate_cam,
            get=get_rotate_cam,
            set=set_rotate_cam
        )

#Panel

    class EditStuff(bpy.types.Panel):
        bl_label = "Edit"
        bl_space_type = "VIEW_3D"
        bl_region_type = "TOOLS"
        bl_context = "objectmode"
    
        def draw(self, context):
            layout = self.layout
            layout.prop(bpy.context.scene, "rotCam")

# Registration


    def register():
        bpy.utils.register_module(__name__)
    def unregister():
        bpy.utils.unregister_module(__name__)
    
    if __name__ == "__main__":
        register()

So I am clearly doing something super inefficient here, I'm just not sure what it is. What is also throwing me off is when I use the custom slider once, clear the terminal, I don't get the KeyError any more. That has me thinking that it is some sort of initialization or context shenanegains going on. Hanging pointer maybe? Just not sure.

I also have a similar set up with compositor nodes to adjust things like shadows. Everything works correctly, I can adjust the shadows, but the KeyError makes Blender crash.

Thanks for the help!

$\endgroup$
2
  • $\begingroup$ While Blender shouldn't crash. Calling an operator from an update callback is risky business. ... not sure if its the cause but this is likely to cause problems. $\endgroup$
    – ideasman42
    May 13, 2015 at 20:58
  • $\begingroup$ Hmm didn't know that - pretty new to python scripting. I'll go review property groups again and see if I can figure out a better way to do that...unless you have a quick suggestion @ideasman42? $\endgroup$ May 13, 2015 at 21:06

1 Answer 1

3
$\begingroup$

I might be mis-interpretting your question but, this works:

import bpy
import math

def update_rotate_cam(self, context):
    v = bpy.context.scene.rotCam
    bpy.data.objects['Camera'].rotation_euler.x = math.radians(v)

class SliderProperties(bpy.types.PropertyGroup):
    bpy.types.Scene.rotCam = bpy.props.FloatProperty(
        name="Rotate Camera",
        description="Rotate the camera with the slider",
        default=0,
        min=0,
        max=360,
        update=update_rotate_cam,
    )

class EditStuff(bpy.types.Panel):
    bl_label = "Edit"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"

    def draw(self, context):
        layout = self.layout
        layout.prop(bpy.context.scene, "rotCam")

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()

In python a safe way to access items using a key is often done this way:

# returns None if key isn't found 
v = some_collection.get(key_name) 

# or provide the fallback value if not found
v = some_collection.get(key_name, fallback)  
$\endgroup$
2
  • $\begingroup$ you saved my butt again! SO much simpler than what I was trying to do. I have this great habit of making convoluted code. And thank you for the python pointer. What would I do without you? $\endgroup$ May 13, 2015 at 21:18
  • $\begingroup$ Get stuck into the Python manual, and read a lot of add-on code. The scripts directory is full of very interesting recipes. $\endgroup$
    – zeffii
    May 13, 2015 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .