Hello everybody,
I created my first (simple but, to me, very useful) addon. Since I think other people might find it useful I’d like to publish it somewhere./ But since I’m an absolute beginner in Blender-land I don’t want to spread bad code
Would someone be willing to check out my (simple, 1 file ) add on and comment on it/ point out obvious errors and the like?
The add on adds an operator to blender to project the current active camera background image onto the mesh, and it also adds an extra button to the ‘options’ panel when texture painting.
Things I’d like to know:
- in bl_info there should also be a “location” field. But I have no clue how to know what to put there.
- in the operator class I used “camera.projectcamerabackgroundimage” for the bl_id. I just made this up on the spot and it seems to work, but I don’t really have a clue if the “camera.” part is correct, and what it means.
- for bl_options in the operator I used {“UNDO”}, should I also add {“REGISTER”}, it’s not really clear to me what that means. Also, can I just set the “UNDO” option without implementing anything for it? Why is it even optional?
- anything other which is bad/wrong
Known issues
- The (already existing) ‘Apply Camera Image’ gives a nice ‘missing textures detected’ warning if there is no target texture. I couldn’t be arsed to add that because Then I’d need to find out how to detect that situation and it took me long enough to get this far. Maybe later.
Code
bl_info = {
"name" : "Project active camera background image.",
"author" : "Martijn Versteegh",
"version" : (1, 0),
"blender" : (2, 80, 0),
"description" : "Project the active camera background image onto the mesh from the camera.",
"warning" :"",
"doc_url" : "",
"tracker_url" : "",
"category" : "Paint",
}
import bpy
class ProjectActiveCameraBackgroundImage(bpy.types.Operator) :
"""Project the active Camera's Background Image onto the current mesh."""
bl_idname = "camera.projectcamerabackgroundimage"
bl_label = "Project Active Camera Background Image"
bl_options = { "UNDO" }
def execute(self, context) :
if context.mode != 'PAINT_TEXTURE' :
return { "CANCELLED" }
if not context.scene.camera :
return { "CANCELLED" }
camera = context.scene.camera
if camera.data.background_images.items() :
bgimage = camera.data.background_images[0]
else:
return { "CANCELLED" }
bpy.ops.paint.project_image(image=bgimage.image.name)
return { "FINISHED" }
#end execute
def invoke(self, context, events) :
return self.execute(context)
#end invoke()
@classmethod
def poll(cls, context) :
if not context.mode == 'PAINT_TEXTURE':
return False
if not context.scene.camera :
return False
if context.scene.camera.data.background_images.items() :
return True
return False
#end poll
#end class
def draw_button(self, context) :
layout = self.layout
layout.operator("camera.projectcamerabackgroundimage", text = "Apply Camera BG Image")
def register() :
bpy.utils.register_class(ProjectActiveCameraBackgroundImage)
bpy.types.VIEW3D_PT_tools_imagepaint_options_external.append(draw_button)
#end register()
def unregister() :
bpy.types.VIEW3D_PT_tools_imagepaint_options_external.remove(draw_button)
bpy.utils.unregister_class(ProjectActiveCameraBackgroundImage)
#end unregister
#if we are run as a script, register out class
if __name__ == "__main__":
#unregister()
register()