I am developing a custom publishing addon that adds a new traypublisher creator and I am wondering what the best way is to pass attributes to publish context. Specifically I would like to have a BoolDef pre-create attribute that can be set by user, which would then be passed onto publish context to be read by a contextplugin during the publish process. I know I can include it in the instance data, however in my case I am creating many instances and would like for this to apply to all of them, instead of just a specific instance. Any ideas?
You can define attributes in creators and make them show on publish tab, implement this method def get_instance_attr_defs(self)
in your creator.
example:
To make instance_attr
appear in creator tab and publish tab,
def get_instance_attr_defs(self):
return [
BoolDef(
"add_review_family",
default=True,
label="Review"
)
]
def get_pre_create_attr_defs(self):
# Use same attributes as for instance attributes
return self.get_instance_attr_defs() + [
.
.
]
To pass the pre-create attributes to instance data.
# Transfer settings from pre create to instance
creator_attributes = instance_data.setdefault(
"creator_attributes", dict())
for key in ["render_target", "review"]:
if key in pre_create_data:
creator_attributes[key] = pre_create_data[key]
Thanks! But I was looking for a way to set an attribute on the whole publish context, rather than just individual instance. Something that’s not attached to an individual instance if I’m publishing multiple instances at a time.
I suppose I could just set this attribute on all instances and then read the first one in the publish plugin, but that feels like a hacky way to do it, so I’m checking if there’s something better.
TL;DR
- You can add attr defs in creator plugins. where you use
get_pre_create_attr_defs
andget_instance_attr_defs
. attrs will be available for each instance of your creator plugin. - You can add attr defs in publish plugins. where you use
get_attribute_defs
. there are two types, instance and context. - instance plugin: attrs will be available for all instances of the specified families.
- context plugin: attrs will be available for the whole context.
Examples
- Add attrs to creators, see how
get_pre_create_attr_defs
andget_instance_attr_defs
are used in this houdini creator plugin. - Add attrs to a publish plugin, see this example collect_something.py
This is achieved by implementing a publish context plugin.
Thank you, this is very helpful to know! One thing that is still missing to me is a way to set this context attribute from the pre-create interface. It seemed that when I make a context attribute it’s a little hidden, as I had to select the context in the list of instances to see it, but the pre-create attributes are easily visible. What I ended up discovering is that I can set a custom property on the CreateContext
object and then later access it in the context pyblish plugin.
In creator’s create function:
self.create_context.custom_attribute = pre_create_data['custom_attribute']
Later in context plugin I can access it like this:
context.data['create_context'].custom_attribute
Now this feels very hacky, but it works. If anyone has any alternative better solution, or if you see some issue with my method that I will run into in the future, please let me know. Thanks!
As far as I can tell,
what you are describing is a form of caching the value of your custom attribute in one creator plugin.
This can have unexpected results.
if you want to define the attr defs once and reuse them in your creator plugins. you can achieve it by implementing a function in the lib.py
and call it in your creator plugins.
I think it’s not possible to have creator attributes in the context. but you can definitely add publish attributes to the context as mentioned earlier in What is the best way to pass pre-create attributes to publish context? - #4 by mustafa_jafar.
By definition, pre-create and create attributes are per instance, not contextual.
But publish plugins can show attributes too. On instances, or for “context”. Look at get_attr_defs_for_context
on AYONPyblishPluginMixin
. But that would mean artist would have to select Context
in instances list.